-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayes.py
353 lines (273 loc) · 9.61 KB
/
bayes.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
import string
from os import listdir
from os.path import isfile, join
def text_cleaning(a):
clean = [char for char in a if char not in string.punctuation]
clean = ''.join(clean)
return [word.lower() for word in clean.split()]
def sort(d):
sorted_dict = {}
sorted_keys = sorted(d, reverse=1, key=d.get)
for w in sorted_keys:
sorted_dict[w] = d[w]
return sorted_dict
def total_freq(vocabulary):
tf = 0
for freq in vocabulary.values():
tf += freq
return tf
def probability_count(input, vocabulary, vl, tf, apriori): #input: review, vocabulary: negative or positive, vl: vocabulary length, tf: total_freq, apriori: initialized as 0.5
probability = 1
for word in input:
if input[word] == 1:
probability *= (vocabulary[word] + 1) / (tf+vl)
else:
probability *= 1 / (tf+vl)
probability *= apriori
return probability
def testNegatives(folder, p, negDict, nlength, posDict, plength): #folder: train, dev or test, p: percentage of data to check
print("Starting negative test reading")
files = [f for f in listdir(folder + "/neg") if isfile(join(folder + "/neg", f))]
target = p/100*len(files)
total = target
check = 0
ctr = 0
print(total)
trueNegative = 0
falsePositive = 0
tfn = total_freq(negDict)
tfp = total_freq(posDict)
for file in files:
if check >= target:
break
file = open(folder + "/neg/" + file, errors = "ignore")
text = file.read()
file.close()
text = text_cleaning(text)
nreview = {}
preview = {}
for word in text:
if word in negDict.keys():
nreview[word] = 1
else:
nreview[word] = 0
if word in posDict.keys():
preview[word] = 1
else:
preview[word] = 0
neg = probability_count(nreview, negDict, nlength, tfn, 0.5)
pos = probability_count(preview, posDict, plength, tfp, 0.5)
if neg >= pos:
trueNegative += 1
else:
falsePositive += 1
ctr += 1
check += 1
print("{} out of {} negative reviews".format(ctr, total))
results = [total, trueNegative, falsePositive]
return results
def testPositives(folder, p, negDict, nlength, posDict, plength): #folder: train, dev or test, p: percentage of data to check
print("Starting positive test reading")
files = [f for f in listdir(folder + "/pos") if isfile(join(folder + "/pos", f))]
target = p/100*len(files)
total = target
check = 0
ctr = 0
truePositive = 0
falseNegative = 0
tfn = total_freq(negDict)
tfp = total_freq(posDict)
for file in files:
if check >= target:
break
file = open(folder + "/pos/" + file, errors = "ignore")
text = file.read()
file.close()
text = text_cleaning(text)
nreview = {}
preview = {}
for word in text:
if word in negDict.keys():
nreview[word] = 1
else:
nreview[word] = 0
if word in posDict.keys():
preview[word] = 1
else:
preview[word] = 0
neg = probability_count(nreview, negDict, nlength, tfn, 0.5)
pos = probability_count(preview, posDict, plength, tfp, 0.5)
if neg <= pos:
truePositive += 1
else:
falseNegative +=1
ctr += 1
check += 1
print("{} out of {} positive reviews".format(ctr, total))
results = [total, truePositive, falseNegative]
return results
def train(p):
negDict = {}
posDict = {}
nlength = plength = 0
files = [f for f in listdir("train/neg") if isfile(join("train/neg", f))]
check = 0
target = p/100*len(files)
for file in files:
if (check >= target):
break
file = open("train/neg" + "/" + file, errors="ignore")
text = file.read()
file.close()
review = text_cleaning(text)
for word in review:
if word in negDict:
negDict[word] += 1
else:
negDict[word] = 1
nlength += 1
check+=1
sorted_neg = sort(negDict)
files = [f for f in listdir("train/pos") if isfile(join("train/pos", f))]
check = 0
target = p/100*len(files)
for file in files:
if (check >= target):
break
file = open("train/pos" + "/" + file, errors="ignore")
text = file.read()
file.close()
review = text_cleaning(text)
for word in review:
if word in posDict:
posDict[word] += 1
else:
posDict[word] = 1
plength += 1
check+=1
sorted_pos = sort(posDict)
nr = testNegatives("train", p, sorted_neg, nlength, sorted_pos, plength)
pr = testPositives("train", p, sorted_neg, nlength, sorted_pos, plength)
TN = nr[1] #true negative
TP = pr[1] #true positive
totalN = nr[0] #total negative
totalP = pr[0] #total positive
total = totalN+totalP #total reviews
FN = pr[2] #false negative
FP = nr[2] #false positive
train_accuracy = (TN + TP) / total
results = [sorted_neg, sorted_pos, train_accuracy, FN, FP]
with open("train_results.txt", "a") as tr:
tr.write("p% = {}\nFN = {}\nFP = {}\nTrain Accuracy = {}\n------------------\n".format(p, FN, FP, train_accuracy))
with open("tr.txt", "a") as t:
t.write("{}\n".format(train_accuracy))
return results
def dev(p, m, n, negDict, posDict):
nlength = plength = 0
newNegDict = {}
newPosDict = {}
ctr = 0
for w in negDict.keys():
if (ctr < n):
ctr += 1
continue
if(nlength > m):
break
nlength +=1
newNegDict[w] = negDict[w]
ctr = 0
for w in posDict.keys():
if (ctr < n):
ctr += 1
continue
if(plength > m):
break
plength +=1
newPosDict[w] = posDict[w]
nr = testNegatives("dev", p, newNegDict, nlength, newPosDict, plength)
pr = testPositives("dev", p, newNegDict, nlength, newPosDict, plength)
print("dev round {} completed".format(p/10))
TN = nr[1]
TP = pr[1]
totalN = nr[0]
totalP = pr[0]
total = totalN+totalP
FN = pr[2]
FP = nr[2]
dev_accuracy = (TN + TP) / total
print("Results counting...")
with open("dev_results.txt", "a") as results:
results.write("m = {}\nn = {}\np% = {}\nFN = {}\nFP = {}\nDev Accuracy = {}\n------------------\n".format(m, n, p, FN, FP, dev_accuracy))
with open("dr_{}_{}.txt".format(m,n), "a") as d:
d.write("{}\n".format(dev_accuracy))
return dev_accuracy
def test(p, m, n, negDict, posDict):
nlength = plength = 0
newNegDict = {}
newPosDict = {}
ctr = 0
for w in negDict.keys():
if (ctr < n):
ctr += 1
continue
if(nlength > m):
break
nlength +=1
newNegDict[w] = negDict[w]
ctr = 0
for w in posDict.keys():
if (ctr < n):
ctr += 1
continue
if(plength > m):
break
plength +=1
newPosDict[w] = posDict[w]
nr = testNegatives("test", p, newNegDict, nlength, newPosDict, plength)
pr = testPositives("test", p, newNegDict, nlength, newPosDict, plength)
print("test round {} completed".format(p/10))
TN = nr[1]
TP = pr[1]
totalN = nr[0]
totalP = pr[0]
total = totalN+totalP
FN = pr[2]
FP = nr[2]
test_accuracy = (TN + TP) / total
precision = TP / (TP + FP)
recall = TP / (TP + FN)
f1 = (2*precision * recall)/(precision + recall)
print("Results counting...")
with open("test_results.txt", "a") as results:
results.write("p% = {}\nFN = {}\nFP = {}\nTest Accuracy = {}\nPrecision = {}\nRecall = {}\nF1 = {}\n------------------\n".format
(p, FN, FP, test_accuracy, precision, recall, f1))
with open("precision.txt", "a") as f:
f.write("{}\n".format(precision))
with open("recall.txt", "a") as f:
f.write("{}\n".format(recall))
with open("f1.txt", "a") as f:
f.write("{}\n".format(f1))
return test_accuracy
#################################################################################################################
#start main loop
best_m = best_n = best_acc = 0
for p in range(10, 101, 10):
tr = train(p)
negDict = tr[0]
posDict = tr[1]
print("Train completed for p = {}%".format(p))
for m in range(100, 301, 100):
for n in range(100, 301, 100):
new_acc = dev(p, m, n, negDict, posDict)
if (new_acc > best_acc):
best_acc = new_acc
best_m = m
best_n = n
with open("best_m_n.txt", "a") as d:
d.write("{}\n{}\n{}\n".format(best_m, best_n, best_acc))
for p in range(10, 101, 10):
tr = train(p)
negDict = tr[0]
posDict = tr[1]
test(p, best_m, best_n, negDict, posDict)
print("Execution Finished")