-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellcheck.py
190 lines (147 loc) · 6.25 KB
/
spellcheck.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
#! /opt/python/2.7.5/bin/python
import argparse
import io
import math
import re
"""NOTE ON SETTINGS:
Best Hypothesis Text on the Training file
n-gram order: 2
smoothing/backoff: used backoff
weight to the channel model: .8
"""
def parseArpaFile(arpafile,n): #parses arpafile (with n-grams up to 2) and returns loaded dictionaries
sourceModel = open(arpafile, 'r')
bigramDic = {'<s>': {}}
unigramDic = {}
sourceModel.next() #skips first two lines
sourceModel.next()
unigramNumber = sourceModel.next().split('=') #gets how many unigrams are in arpa file.
unigramNumber = int(unigramNumber[1].strip('\n'))
bigramNumber = sourceModel.next().split('=')
bigramNumber = int(bigramNumber[1].strip('\n'))
sourceModel.next()
sourceModel.next()
for x in range(0, unigramNumber):
line = sourceModel.next().split('\t')
unigramDic[line[1].strip()] = (float(line[0]), float(line[2].strip())) #tuple with unigramProb, and unigramBackOffProb
sourceModel.next() #skips two lines separating 1-grams and 2-grams.
sourceModel.next()
if n == 2:
for x in range(0, bigramNumber):
line = sourceModel.next()
line = line.split()
word = line[1].strip()
nextWord = line[2].strip()
prob = float(line[0])
if (word not in bigramDic):
bigramDic[word] = {}
bigramDic[word][nextWord] = prob
#check if it reiterates
sourceModel.close()
if n == 2:
return bigramDic, unigramDic
else:
return {}, unigramDic #empty dictionary
def parseChannelModel(txtfile):
model = {}
channelModel = open(txtfile, 'r')
letters = channelModel.next().strip('\n').split() #the first line - letters
for line in channelModel:
line = line.split()
model[line[0]] = {}
for i in range(1,len(line)):
model[line[0]][letters[i-1]] = float(line[i])
return model
def guessCorrectWord(word, prevWord, nextWord, weight):
maxscore = ('none', float("-inf")) #initializes the max score - to be overwritten
guessWord = ""
for x in unigramDic:
if re.match('.*[^a-zA-Z]+', x):
continue #skip over any word in the dictionary with non-alphabetic characters
#editDistance = calculateEditDistance(word, x)
editDistance = shorterEditDistance(word, x)
probC = editDistance * -1 * weight #multiply by weight
if args.n == 1:
probC += unigramDic[x][0]
if args.n == 2:
if prevWord in bigramDic and x in bigramDic[prevWord]:
probC += bigramDic[prevWord][x]
else:
if prevWord not in unigramDic: #must put in <unk> probability
prevWord = '<unk>'
probC += unigramDic[prevWord][1] + unigramDic[x][0] #backoff(c-1) and P(c)
if x in bigramDic and nextWord in bigramDic[x]:
probC += bigramDic[x][nextWord]
else:
if nextWord not in unigramDic: #must put in <unk> probability
nextWord = '<unk>'
probC += unigramDic[x][1] + unigramDic[nextWord][0] #backoff(c-1) and P(c)
if probC > maxscore[1]:
maxscore = (x, probC) #replaces maxscore if new Probability is higher
guessWord = maxscore[0]
return guessWord
def calculateEditDistance(word, dicWord):
wordChars = list(word)
dicWordChars = list(dicWord)
matrix = [[0 for x in xrange(len(dicWordChars)+1)] for x in xrange(len(wordChars)+1)]
matrix[0][0] = 0 #start
for y in range(1, len(matrix[0])): #fill in deletion costs (1st row)
matrix[0][y] = matrix[0][y-1] + -math.log(costsMatrix[dicWordChars[y-1]]['eps'],10)
for x in range(1, len(matrix)): #fill in insertion costs (1st col)
matrix[x][0] = matrix[x-1][0] + -math.log(costsMatrix['eps'][wordChars[x-1]],10)
for x in range(1, len(matrix)):
for y in range(1, len(matrix[0])):
delCost = matrix[x][y-1] + -math.log(costsMatrix[dicWordChars[y-1]]['eps'],10) #left row
insCost = matrix[x-1][y] + -math.log(costsMatrix['eps'][wordChars[x-1]],10) #up a row
subCost = matrix[x-1][y-1] + -math.log(costsMatrix[wordChars[x-1]][dicWordChars[y-1]],10) #corner
matrix[x][y] = min(delCost, insCost, subCost)
editDistance = matrix[len(wordChars)][len(dicWordChars)]
return editDistance
def shorterEditDistance(word, dicWord):
"""I implememented the Levenshtein_distance algorithm here, calculating edit distance faster than method above."""
wordChars = list(word)
dicWordChars = list(dicWord)
row1 = [0 for x in xrange(len(dicWord) + 1)]
row2 = [0 for x in xrange(len(dicWord) + 1)]
row1[0] = 0
for y in range(1,len(row1)): #initializes first row
row1[y] = row1[y-1] - math.log(costsMatrix[dicWordChars[y-1]]['eps'],10) #deletes
for x in range(0, len(wordChars)):
row2[0] = row1[0]-math.log(costsMatrix['eps'][wordChars[x]],10) #insert 1 char
for y in range(0,len(dicWordChars)):
subCost = row1[y] + -math.log(costsMatrix[wordChars[x]][dicWordChars[y]],10)
delCost = row2[y] + -math.log(costsMatrix[dicWordChars[y]]['eps'],10) #left row
insCost = row1[y+1] + -math.log(costsMatrix['eps'][wordChars[x]],10) #up a row
row2[y+1] = min(delCost, insCost, subCost)
#copy row2 (current row) to row1 (previous row) for next iteration
row1 = row2 * 1
return row2[len(dicWord)]
if __name__=='__main__':
parser = argparse.ArgumentParser(description='test')
parser.add_argument('-lmfile', help='arpa file', required=True)
parser.add_argument('-n', type=int, help='number for n-gram (2, bi-gram is recommended)', required=True)
parser.add_argument('-infile', help='file with spelling errors', required=True)
parser.add_argument('-channel', help='file with edit distance probabilities', required=True)
parser.add_argument('-o', help='specify an output file hypothesized correct spellings', required=True)
parser.add_argument('-w', type=float, default=1.0, dest='weight', help='optional weight for channelModel:sourceModel ratio')
args = parser.parse_args()
bigramDic, unigramDic = parseArpaFile(args.lmfile, args.n)
costsMatrix = parseChannelModel(args.channel)
words = []
errorFile = open(args.infile, 'r')
outfile = open(args.o, 'w')
n = 100 #reading in first 100 lines of the training
for x in range(0,n):
line = errorFile.next()
line = line.rstrip('\n').split()
for y in line:
words.append(y)
for i, s in enumerate(words):
if '<ERROR>' in s:
index = i
word = s[len('<ERROR>'):-len('</ERROR>')].strip() #strips of the Errors on each side
prevWord = words[i-1].strip()
nextWord = words[i+1].strip()
outfile.write(guessCorrectWord(word,prevWord,nextWord,args.weight) + '\n')
errorFile.close()
outfile.close()