-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sentence_check_context.py
131 lines (115 loc) · 3.59 KB
/
Sentence_check_context.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
import re
import commands
import pickle
import math
import text
contextWords = pickle.load(open('data/brown/ContextWords.dict', 'rb'))
confusionSets = pickle.load(open('data/brown/ConfusionSets.dict', 'rb'))
fp = open("data/all-words-cleaned.txt", 'r')
data = fp.read()
fp.close()
splitdata = data.split('\n')
numOfWords = len(splitdata) - 1
dicti = []
for i in range(0, numOfWords):
temp = splitdata[i].lower()
dicti.append(temp)
cWords = {}
confCounts = {}
priorConf = {}
for cSet in confusionSets:
sum_set = 0
for cWord in confusionSets[cSet]:
sum_set += confusionSets[cSet][cWord]
cWords[cWord] = confusionSets[cSet]
for cWord in confusionSets[cSet]:
priorConf[cWord] = float(confusionSets[cSet][cWord])/float(sum_set)
confCounts[cWord] = float(confusionSets[cSet][cWord])
#for w in priorConf:
# print(w+ ' '),
# print(priorConf[w])
#print(confCounts)
#print(priorConf)
def context(line):
line = line.lower()
line = line.replace('\n', '')
line = re.sub('[^0-9a-zA-Z ]+', '', line)
words_ = line.split(' ')
set_of_words = []
minprob = 1
minidx = 0
poss_list = {}
for i in range(0, len(words_)):
temp = ''
maxval = 0
poss = {}
if words_[i] not in dicti:
poss = text.correctWord(words_[i])
else:
poss[words_[i]] = 1
for p in poss:
if poss[p] > maxval:
maxval = poss[p]
temp = p
words_[i] = temp
if minprob > poss[temp]:
minidx = i
minprob = poss[temp]
poss_list = poss
kter = 0
if len(poss_list) == 0:
set_of_words.append([])
for wter in words_:
set_of_words[kter].append(wter)
else:
for w in poss_list:
if (poss_list[w] > 0.2):
set_of_words.append([])
for wter in words_:
set_of_words[kter].append(wter)
set_of_words[kter][minidx] = w
kter += 1
#print(poss_list)
for words in set_of_words:
#print(words)
for i in range(0, len(words)):
words[i] = words[i].lower()
for i in range(0, len(words)):
if words[i] in cWords:
start = max(0,i-3)
end = min(i+3, len(words)-1)
context = set()
for j in range(start, end+1):
context.add(words[j])
prob = {}
confuse = cWords[words[i]]
for w in confuse:
prob[w] = priorConf[w]
for c in context:
val = 1
if c in contextWords[w]:
val = (float(contextWords[w][c])+1)/(1.0*(confCounts[w] + len(contextWords[w])))
#val = (float(contextWords[w][c]))
else:
val = 1
prob[w] *= val
maxval = 0
idx = ''
#for p in prob:
# print(p + ' '),
# print(prob[p])
for k in prob:
if prob[k] > maxval:
maxval = prob[k]
idx = k
# print(k)
normalize = 0
for k in prob:
normalize += prob[k]
for k in prob:
prob[k] /= normalize
#print(prob)
words[i] = idx
for i in words:
print i+' ',
print