-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPELL_CHECKER.py
447 lines (400 loc) · 14.9 KB
/
SPELL_CHECKER.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import re
import math
import time
import pickle
import operator
import numpy as np
from colorama import init
from termcolor import colored
from prettytable import PrettyTable
init()
print('\n IMPORTING NLTK MODULE...')
t1 = time.time()
import nltk
t2 = time.time()
print('\t[ IMPORTED IN {0:.3f} SECS ]'.format(t2-t1))
file = open( 'SAMPLE_CORPUS.txt' , 'r' )
print(' IMPORTING SAMPLE CORPUS...')
t1 = time.time()
CORPUS = file.read()
file.close()
t2 = time.time()
print('\t[ IMPORTED IN {0:.3f} SECS ]'.format(t2-t1))
print(' LOADING TRIMMED DICTIONARY WORDS...')
TRIMMED = dict()
t1 = time.time()
for i in range(1,16):
file = open( 'TRIMMED_WORDS_'+str(i), 'rb' )
TRIMMED.update(pickle.load(file))
file.close()
t2 = time.time()
print('\t[ LOADED IN {0:.3f} SECS ]'.format(t2-t1))
print(' IMPORTING VOCABULARY...')
t1 = time.time()
VOCAB = list()
file = open( "DICTIONARY.txt" , "r" )
words = list()
for x in file:
VOCAB.append(x[:-1])
file.close()
t2 = time.time()
print('\t[ IMPORTED IN {0:.3f} SECS ]'.format(t2-t1))
V = len( VOCAB )
print(' LOADING CONFUSION MATRICES...')
t1 = time.time()
file = open( "CONFUSION_MATRICES/CONFUSION_MATRIX_DEL.txt" , "r" )
raw = list()
for x in file:
x = int(x[:-1])
raw.append(x+1)
file.close()
CMD = np.array(raw).reshape(27,26)
file = open( "CONFUSION_MATRICES/CONFUSION_MATRIX_INS.txt" , "r" )
raw = list()
for x in file:
x = int(x[:-1])
raw.append(x+1)
file.close()
CMI = np.array(raw).reshape(27,26)
file = open( "CONFUSION_MATRICES/CONFUSION_MATRIX_SUB.txt" , "r" )
raw = list()
for x in file:
x = int(x[:-1])
raw.append(x+1)
file.close()
CMS = np.array(raw).reshape(27,26)
file = open( "CONFUSION_MATRICES/CONFUSION_MATRIX_TRAN.txt" , "r" )
raw = list()
for x in file:
x = int(x[:-1])
raw.append(x+1)
file.close()
CMT = np.array(raw).reshape(27,26)
t2 = time.time()
print('\t[ LOADED IN {0:.3f} SECS ]'.format(t2-t1))
print(' LOADING CONTRACTIONS...')
t1 = time.time()
file = open( "WORDS_WITH_APOSTROPHE" , "rb" )
WORDS_W_APOS = pickle.load( file )
file.close()
t2 = time.time()
print('\t[ LOADED IN {0:.3f} SECS ]'.format(t2-t1))
APOS_OMITTED_WORDS = list()
for pair in WORDS_W_APOS:
APOS_OMITTED_WORDS.append(pair[0])
DELIMITERS = [ '"' , '.' , '!' , '?' ]
N = 2699132
V = 370104
U = 59613
S = 118697
INFINITY = 10000
def log( x ):
if not x:
return -1*float('inf')
return math.log10(x)
def getEditDistance( x , y ):
m = len(x)
n = len(y)
arr = np.array([0]*((m+1)*(n+1))).reshape( m+1 , n+1 )
for a in range(m):
arr[a+1][0] = a+1
for a in range(n):
arr[0][a+1] = a+1
for i in range(1,m+1):
for j in range(1,n+1):
v1 = arr[i-1][j] + 1
v2 = arr[i][j-1] + 1
v3 = arr[i-1][j-1]
if x[i-1] != y[j-1]:
v3 += 1
v4 = INFINITY
if ( i > 1 and j > 1 and x[i-1] == y[j-2] and x[i-2] == y[j-1] ):
v4 = arr[i-2][j-2] + 1
v = [ v1 , v2 , v3 , v4 ]
arr[i][j] = INFINITY
for each in v:
if each < arr[i][j]:
arr[i][j] = each
return arr
def getCandidates( word , root , s , lev ):
if ( not len(word) ):
return
for x in range(len(word)):
st = word[:x] + word[x+1:]
if ( st in TRIMMED.keys() ):
for each in TRIMMED[st]:
s.add(each.lower())
if ( st in VOCAB ):
s.add(st)
if ( lev == 1 ):
getCandidates( st , root , s , 2 )
def getEditOperation( arr , x , y ):
edits = list()
i = len(x)
j = len(y)
while( i>=0 and j>=0 ):
if ( not i and not j ):
break
p = 0
if i>0 and j>0 and x[i-1] != y[j-1]:
p = 1
if i>0 and j>0 and arr[i][j] == arr[i-1][j-1] + p:
if p:
if i == 1:
edits.append(('S','#'+x[i-1]+x[i],'#'+y[j-1]+x[i]))
elif i < len(x):
edits.append(('S',x[i-2]+x[i-1]+x[i],x[i-2]+y[j-1]+x[i]))
else:
edits.append(('S',x[i-2]+x[i-1]+'#',x[i-2]+y[j-1]+'#'))
i = i-1
j = j-1
continue
elif i>0 and arr[i][j] == arr[i-1][j] + 1:
if i == 1:
edits.append(('D','#'+x[i-1]+x[i],'#'+x[i]))
elif i < len(x):
edits.append(('D',x[i-2]+x[i-1]+x[i],x[i-2]+x[i]))
else:
edits.append(('D',x[i-2]+x[i-1]+'#',x[i-2]+'#'))
i = i-1
continue
elif j>0 and arr[i][j] == arr[i][j-1] + 1:
if not i:
edits.append(('I','#'+x[i],'#'+y[j-1]+x[i]))
elif i < len(x) :
edits.append(('I',x[i-1]+x[i],x[i-1]+y[j-1]+x[i]))
else:
edits.append(('I',x[i-1]+'#',x[i-1]+y[j-1]+'#'))
j = j-1
continue
elif ( i > 1 and j > 1 and x[i-1] == y[j-2] and x[i-2] == y[j-1] and arr[i-2][j-2]+1==arr[i][j] ):
edits.append(('T',x[i-2]+x[i-1],x[i-1]+x[i-2]))
i = i-2
j = j-2
return edits
def h( s , axis ):
if axis == 'h':
return ord(s[0])-ord('a')
if s[0] == '#':
return 0
return ord(s[0])-ord('a')+1
def getLikelihood( edits ):
L = 0
for edit in edits:
if edit[0] == 'D':
if edit[1][0] == '#':
length = N
else:
length = len( re.findall(edit[1][0],CORPUS) )
L = L + log(CMI[h(edit[1][0],'v')][h(edit[1][1],'h')]) - log(length)
elif edit[0] == 'I':
if edit[1][0] == '#':
length = len( re.findall(' '+edit[2][1],CORPUS) )
else:
length = len( re.findall(edit[2][0]+edit[2][1],CORPUS) )
L = L + log(CMD[h(edit[2][0],'v')][h(edit[2][1],'h')]) - log(length)
elif edit[0] == 'S':
length = len( re.findall(edit[2][1],CORPUS) )
L = L + log(CMS[h(edit[1][1],'v')][h(edit[2][1],'h')]) - log(length)
elif edit[0] == 'T':
length = len( re.findall(edit[2][0]+edit[2][1],CORPUS) )
L = L + log(CMT[h(edit[2][0],'v')][h(edit[2][1],'h')]) - log(length)
return L
def getUnigramProb( w ):
if ( w == '' or w == '#' ):
return log( S + 0.1 ) - log( N + 0.1*V )
co = len( re.findall( ' ' + w + '[.\s]' , CORPUS ) )
ca = log( co + 0.1 ) - log( N + 0.1*V )
return ca
def getBigramProb( w1 , w2 ):
if ( w1 == '' or w1 == '#' ):
c12 = len( re.findall( '. ' + w2 + '[.\s]' , CORPUS ) ) + len( re.findall( '^' + w2 + '[.\s]' , CORPUS ) )
c1 = S
return log( c12 + math.pow(10,getUnigramProb(w2)) ) - log( c1 + 1 )
if ( w2 == '' or w2 == '#' ):
c12 = len( re.findall( ' ' + w1 + '.' , CORPUS ) )
c1 = len( re.findall( ' ' + w1 + '[.\s]' , CORPUS ) )
return log( c12 + math.pow(10,getUnigramProb(w2)) ) - log( c1 + 1 )
c12 = len( re.findall( ' ' + w1 + ' ' + w2 + '[.\s]' , CORPUS ) )
c1 = len( re.findall( ' ' + w1 + '[.\s]' , CORPUS ) )
return log( c12 + math.pow(10,getUnigramProb(w2)) ) - log( c1 + 1 )
def getSequenceProb( words , pos ):
l = len( words )
if l == 1 and not pos:
return getBigramProb( '#' , words[pos] ) + getBigramProb( words[pos] , '#' )
if pos == l-1:
if words[pos-1] in VOCAB:
return getBigramProb( words[pos-1] , words[pos] ) + getBigramProb( words[pos] , '#' )
return getBigramProb( words[pos] , '#' )
if not pos:
if words[pos+1] in VOCAB:
return getBigramProb( '#' , words[pos] ) + getBigramProb( words[pos] , words[pos+1] )
return getBigramProb( '#' , words[pos] )
if words[pos-1] in VOCAB:
if words[pos+1] in VOCAB:
return getBigramProb( words[pos-1] , words[pos] ) + getBigramProb( words[pos] , words[pos+1] )
return getBigramProb( words[pos-1] , words[pos] )
if words[pos+1] in VOCAB:
return getBigramProb( words[pos] , words[pos+1] )
return getUnigramProb( words[pos] )
def bestCandidate( words , pos , choice , CHANGES , sen ):
ORG = words[pos]
print('\tNON-WORD ERROR : ' , ORG.upper())
print('\t FINDING SUITABLE CANDIDATES...')
time.sleep(1)
t1 = time.time()
cand = set()
if ( words[pos] in TRIMMED.keys() ):
cand = cand.union(TRIMMED[words[pos]])
getCandidates( ORG , ORG , cand , 1 )
t2 = time.time()
print( '\t [ {0} CANDIDATES FOUND IN {1:.3f} SECS ]'.format(len(cand),t2-t1) )
print( '\t PROCESSING EACH CANDIDATE...' )
t1 = time.time()
rows = list()
for c in cand:
arr = getEditDistance( ORG , c )
ed = arr[len(ORG)][len(c)]
if ( ed > 2 ):
continue
log_pc = getUnigramProb(c)
words[pos] = c
seq_pr = getSequenceProb( words , pos )
edits = getEditOperation( arr , ORG , c )
log_likel = getLikelihood( edits )
rows.append([c, ed, edits, round(-1*log_pc,5), round(-1*seq_pr,5), round(-1*log_likel,5), round(-1*seq_pr-log_likel,5)])
t2 = time.time()
print( '\t [ ALL CANDIDATES PROCESSED IN {0:.3f} SECS ]'.format(t2-t1) )
rows.sort( key = lambda x: (x[1],x[6]) )
print( '\t [ {} CANDIDATES SHORT-LISTED ]'.format(len(rows)) )
words[pos] = rows[0][0]
CHANGES[(sen,pos)] = ( ORG, words[pos] )
print( '\t *[ BEST CANDIDATE : {} ]*'.format(words[pos].upper()) )
print( '\n' , end = '' )
if choice == 'y':
table = PrettyTable(["Correct Word Candidate (C)", "Edit Distance",
"Edit Operations(s) [I->C]", "-log[P(C)]", "-log[P(SEQ)]", "-log[P(I|C)]", "-log[P(SEQ)P(I|C)]"])
for row in rows:
table.add_row(row)
print(table)
print( '\n' , end='' )
def putApostrophe( w ):
u = w.lower()
for pair in WORDS_W_APOS:
if u == pair[0]:
if w[0].capitalize() == w[0]:
return pair[1].capitalize()
return pair[1]
return w
def correctQuery( text , edits ):
print( '\n ORIGINAL TEXT : ' , colored( text , 'red' ) )
query = text.replace('-',' - ').replace("'",'')
print( ' EDITED TEXT :' , end = ' ' )
SENT = nltk.tokenize.sent_tokenize(query)
ORG_SENT = nltk.tokenize.sent_tokenize(text.replace('-',' - ').replace("'",'-'))
KEYS = edits.keys()
if not len( KEYS ):
for s in range(len(ORG_SENT)):
words = nltk.tokenize.word_tokenize(ORG_SENT[s])
flag = True
space = True
for w in words:
if w == 'i':
w = 'I'
if flag:
w = w.capitalize()
flag = not flag
if re.search( '^[a-zA-Z0-9]+' , w ):
if space:
print( ' ' , end='' )
if not '-' in w:
print( colored( putApostrophe(w) , 'green' ) , end = '' )
else:
if w[:2] == 'i-': w = 'I' + w[1:]
w = w.replace( '-' , "'" )
print( colored( w , 'green' ) , end = '' )
space = True
else:
if w in DELIMITERS: flag = True
if w == '-': space = False
else: space = True
print( colored( w , 'green' ) , end = '' )
return
sentences = set(list(zip(*edits.keys()))[0])
for s in range(len(SENT)):
N = -1
p = -1
words = nltk.tokenize.word_tokenize(SENT[s])
ORG_WORDS = nltk.tokenize.word_tokenize(ORG_SENT[s])
if s+1 in sentences:
for w in nltk.tokenize.word_tokenize(SENT[s]):
p = p + 1
if re.search( '^[a-zA-Z]+$' , w ):
N = N + 1
if ( s+1 , N ) in KEYS:
if words[p][0] >= 'a':
words[p] = edits[(s+1,N)][1]
else:
words[p] = edits[(s+1,N)][1].capitalize()
elif '-' in ORG_WORDS[p]:
w = ORG_WORDS[p]
if ORG_WORDS[p][:2] == 'i-': w = 'I' + w[1:]
words[p] = w.replace( '-' , "'" )
else:
for w in nltk.tokenize.word_tokenize(SENT[s]):
p = p + 1
if re.search( '^[a-zA-Z]+$' , w ):
if '-' in ORG_WORDS[p]:
w = ORG_WORDS[p]
if ORG_WORDS[p][:2] == 'i-': w = 'I' + w[1:]
words[p] = w.replace( '-' , "'" )
flag = True
space = True
for w in words:
if w == 'i':
w = 'I'
if flag:
w = w.capitalize()
flag = not flag
if re.search( '^[a-zA-Z0-9]+' , w ):
if space: print( ' ' , end='' )
if "'" in w: print( colored( w , 'green' ) , end = '' )
else: print( colored( putApostrophe(w) , 'green' ) , end = '' )
space = True
else:
if w in DELIMITERS: flag = True
if w == '-': space = False
else: space = True
print( colored( w , 'green' ) , end = '' )
def main( text , choice ):
query = text.replace('-',' - ').replace("'",'')
CHANGES = dict()
counter = 1
print( '\n' )
for sentence in nltk.tokenize.sent_tokenize(query):
flag = True
print( ' SENTENCE' , counter )
print( ' SEARCHING NON-WORD ERRORS...' )
counter = counter + 1
words = list()
for w in nltk.tokenize.word_tokenize(sentence):
if re.search( '^[a-zA-Z]+$' , w ):
words.append(w.lower())
for x in range(len(words)):
if not (words[x] in VOCAB or words[x] in APOS_OMITTED_WORDS):
bestCandidate( words , x , choice , CHANGES , counter-1 )
flag = False
if flag:
print( '\n' , end='' )
correctQuery( text , CHANGES )
print('\n' , end='')
while True:
print( '\n' + '-'*200 + '\n' )
query = input( ' ENTER QUERY : ' )
if not re.search( '[a-zA-Z]' , query ):
continue
if ( query[:5].lower() == '#quit' ):
break
choice = input( ' SHOW THE CALCULATIONS (Y/N) : ' )
main( query , choice[0].lower() )
print( colored('\n\n CLOSING...','magenta') )