-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sentence_check_collocations.py
173 lines (148 loc) · 4.35 KB
/
Sentence_check_collocations.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
import re
import commands
import pickle
import math
import nltk
import text
import time
start = time.time()
collocs_left = pickle.load(open('data/brown/Collocs_left_pruned.dict', 'rb'))
collocs_right = pickle.load(open('data/brown/Collocs_right_pruned.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])
end = time.time()
print end - start
#for w in priorConf:
# print(w+ ' '),
# print(priorConf[w])
#print(confCounts)
#print(priorConf)
line = raw_input('Enter line:')
start = time.time()
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 = {}
end = time.time()
print "1 " + str(end - start)
start = time.time()
for i in range(0, len(words_)):
temp = ''
maxval = 0
poss = {}
start_cor = time.time()
if words_[i] not in dicti:
print words_[i]
poss = text.correctWord(words_[i])
else:
poss[words_[i]] = 1
continue
end_cor = time.time()
print "cor " + str(end_cor - start_cor)
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
print poss_list
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
end = time.time()
print "3 " + str(end - start)
#print(poss_list)
def collocations(set_of_words):
correct = {}
line = ""
for eve in set_of_words[0]:
line = line + " " + eve
word_tok = nltk.word_tokenize(line)
pos_tags = nltk.pos_tag(word_tok)
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)
left_seq = ''
for j in range(start,i):
left_seq = left_seq + pos_tags[j][1] + ','
right_seq = ''
for j in range(i+1,end+1):
right_seq = right_seq + pos_tags[j][1] + ','
prob = {}
confuse = cWords[words[i]]
for w in confuse:
prob[w] = priorConf[w]
if left_seq in collocs_left[w]:
val_l = float(collocs_left[w][left_seq]+1)/(confCounts[w] + len(collocs_left[w]))
else:
val_l = 1
if right_seq in collocs_right[w]:
val_r = float(collocs_right[w][right_seq]+1)/(confCounts[w] + len(collocs_right[w]))
else:
val_r = 1
prob[w]*=(val_r*val_l)
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
for eve in prob:
if eve in correct:
correct[eve] += prob[eve]
else:
correct[eve] = prob[eve]
for i in words:
print i+' ',
print
return correct