-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_readfile.py
More file actions
227 lines (188 loc) · 6.12 KB
/
final_readfile.py
File metadata and controls
227 lines (188 loc) · 6.12 KB
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
import numpy as np
import re
from spellchecker import SpellChecker
import nltk
import pickle
#Global Variables
fp = open('final_model.dat', 'rb')
model = pickle.load(fp)
#print(model)
A = model[0]
B = model[1]
T = model[2]
#return lists of list of sentences with wrong words and a dictionary of correct words
def getlines(file):
text = []
f = open(file, 'r')
for line in f:
sentence = line.split()
text.append(sentence)
errordic = {}
errornum = 0
for sentence in text:
# move ERR
for x in range(2): #check again as some error will occur when a <ERR> next to another <ERR>
for word in sentence:
if word[0] == '<':
sentence.remove(word)
# save all correct spellchecker result
for word in sentence:
if word[0:5] == 'targ=':
errornum += 1
if text.index(sentence) not in errordic:
errordic[text.index(sentence)] = [word[5:(len(word)-1)]]
else:
errordic[text.index(sentence)].append(word[5:(len(word)-1)])
#errors.append([text.index(sentence), word[5:(len(word)-1)]])
sentence.remove(word)
# deal with punctuation
for word in sentence:
if word[-1] in ".,!>?'" and word.count('.') < 2:
if len(word) == 1:
sentence.remove(word)
else:
sentence[sentence.index(word)] = word[:(len(word)-1)]
#print(sentence)
#print(errordic)
return text, errordic, errornum
#calculate the possibility of one possible sentence
def prob(W):
vit = {}
#bp = [0 for i in range(len(W))]
#N = len(T)
#v_tw = 0
max_prob = float("-inf")
for i in range(len(W)):
if i == 0:
for t in T:
vit[t] = {}
try:
vit[t][W[0]] = A['<s>'][t] + B[t][W[0]]
#bp[0] = t
except KeyError:
vit[t][W[0]] = float("-inf")
#print(vit)
else:
#bp[W[i]] = {}
for t in T:
vit[t][W[i]] = float("-inf")
#print(vit)
for tprev in T:
try:
v_tw = vit[tprev][W[i-1]] + A[tprev][t] + B[t][W[i]]
except KeyError:
v_tw = float("-inf")
if v_tw > vit[t][W[i]]:
vit[t][W[i]] = v_tw
if vit[t][W[i]] > max_prob:
max_prob = vit[t][W[i]]
return max_prob
def findError(sentence):
#print(sentence)
newline = []
spell = SpellChecker()
for word in sentence:
if word.islower() == True:
newline.append(word)
#print(newline)
errors = spell.unknown(newline)
#print(errors)
index = []
for word in errors:
index.append(sentence.index(word))
return index
def get_possibles(sentence):
spell = SpellChecker()
newline = []
for word in sentence:
if word.islower() == True:
newline.append(word)
errors = spell.unknown(newline)
if len(errors) == 0:
return [sentence]
else:
first = errors.pop()
checkword = spell.candidates(first)
check = [sentence]*len(checkword)
index = sentence.index(first)
newlist = []
for element in check:
element[index] = checkword.pop()
newlist.append(element[:])
ret_list = []
for sent in newlist:
ret_list.extend(get_possibles(sent))
#print(ret_list)
return ret_list
#import a list of words and return a list of corrected words with highest possibilities and a list of corrected text
def correction(sentence):
indexes = findError(sentence)
#incorrect_word_count = len(indexes)
possible_sents = get_possibles(sentence)
#print(possible_sents)
#print(indexes)
max_prob = float("-inf")
max_prob_sent = []
corrected_words = []
for sent in possible_sents:
probability = prob(sent)
#print(probability)
if probability > max_prob:
max_prob = probability
#print(max_prob)
max_prob_sent = sent[:]
#print(max_prob_sent)
for i in indexes:
corrected_words.append(max_prob_sent[i])
#print(max_prob_sent)
#print(corrected_words)
if max_prob == float("-inf"):
max_prob_sent = sentence
return max_prob_sent,corrected_words
#this function is used when text is more than one sentence
#import the text as lists of list of words return a list of correct words and corrected text
def correction_text(text):
correct_words = {}
corrected_text = []
for i in range(len(text)):
sentence,wordlist = correction(text[i])
#print(sentence,wordlist)
correct_words[i] = wordlist
corrected_text.append(sentence)
#print(correct_words)
#print("The corrected text is:",corrected_text)
return correct_words,corrected_text
def accuracy(hypothesis,real,wordcount):
correct_words = 0
#print(hypothesis)
#print(real)
for index in hypothesis:
#print(index)
#print(hypothesis[index])
for ans in hypothesis[index]:
try:
for ans2 in real[index]:
if ans2 == ans:
correct_words += 1
except KeyError:
correct_words = correct_words
correct_rate = correct_words / wordcount
print(correct_rate)
return correct_rate
def main():
text, errors, errornum = getlines('holbrook-tagged.dat')
corrected_words, corrected_text = correction_text(text[:20])
accuracy(corrected_words, errornum)
#print(text[3])
#correction(text[3])
if __name__ == '__main__':
main()
# text, errors = getlines('holbrook-tagged.dat')
# findError(text)
# text, errors = getlines('holbrook-tagged.dat')
# i = 0
# for sentence in text:
# i = i + 1
# findError(sentence)
# if i == 20:
# break