-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaivebayes.py
More file actions
402 lines (344 loc) · 16 KB
/
Copy pathnaivebayes.py
File metadata and controls
402 lines (344 loc) · 16 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import time
from scipy.misc import imread
from scipy.misc import imresize
import matplotlib.image as mpimg
from scipy.ndimage import filters
import urllib
from numpy import random
import os
import shutil
import string
import operator
import tensorflow as tf
# ======================================================================================================================
# ========================================= Running the code ===========================================================
# ======================================================================================================================
prepare_data = False #download dataset (just pull from github, don't need to run this)
run_part1 = False #prints each word and its frequency
run_part2 = False #prints naive bayes classification performance
tuning_mk = False #tuning m and k for naive bayes
run_part3 = False #printing 10 most important words for negative and positive reviews
# ======================================================================================================================
# ======================================================================================================================
# ======================================================================================================================
def split_dataset(neg_index, pos_index, set_type, size):
'''
Extracts a sample of size specified for the purpose of testing/validation or training
:param neg_index: list of random numbers for positive reviews
:param pos_index: list of random numbers for negative reviews
:param set_type: extracting validation/test or training
:param size: size of the set
'''
#969 in neg, 948 in pos
filename = "/review_polarity/txt_sentoken"
# Get current directory
curr_directory = os.getcwd()
# Make the folders if they don't exist
if not os.path.exists(os.getcwd()+"/training_set"):
os.makedirs(os.getcwd()+"/training_set/neg")
os.makedirs(os.getcwd()+"/training_set/pos")
if not os.path.exists(os.getcwd()+"/test_set"):
os.makedirs(os.getcwd()+"/test_set/neg")
os.makedirs(os.getcwd()+"/test_set/pos")
if not os.path.exists(os.getcwd()+"/validation_set"):
os.makedirs(os.getcwd()+"/validation_set/neg")
os.makedirs(os.getcwd()+"/validation_set/pos")
# Generate the training set, saving 200 images for testing set, 200 for training set
for j in range(2):
for i in range(int(size/2)):
if j==0: # j=0 => neg reviews; else, pos reviews
rand_num = neg_index[i]
file_source = os.getcwd()+"/review_polarity/txt_sentoken/neg/"
review_filename = os.listdir(file_source)[rand_num]
file_source = file_source + review_filename
file_destination = os.getcwd() + set_type + "/neg"
else:
rand_num = pos_index[i]
file_source = os.getcwd()+"/review_polarity/txt_sentoken/pos/"
review_filename = os.listdir(file_source)[rand_num]
file_source = file_source + review_filename
file_destination = os.getcwd() + set_type + "/pos"
shutil.copy2(file_source, file_destination)
# Process the text file
f = open(file_destination + "/"+ review_filename, "r+")
data = f.read()
exclude = set(string.punctuation)
file_text = ''.join(ch for ch in data if ch not in exclude).lower()
f.seek(0)
f.write(file_text)
f.truncate()
f.close()
return
def merge(d1, d2, merge_fn=lambda x,y:y):
'''
Merges 2 dictionaries together accodring to merge_fn
:param d1: first dictionary
:param d2: second dictionary
:param merge_fn: function according to which merge will be performed
:return: merged dictionary
'''
result = dict(d1)
for k,v in d2.items():
if k in result:
result[k] = merge_fn(result[k], v)
else:
result[k] = v
return result
def generate_count_dict(path):
'''
Generates word occurence dictionaries for positive and negative reviews
:param path: the location of the training set
:return: 2 dictionaries for positive and negative reviews
'''
pos_dict = {} #format = (key, value) = (word, frequency)
neg_dict = {}
# Go through the negative reviews
for review in os.listdir(path + "/neg"):
with open(os.getcwd()+"/"+path+"/neg/"+review) as f:
unique_words = set(f.read().split())
for word in unique_words:
if not neg_dict.get(word): #check if the word is in dictionary
neg_dict[word] = 1
else: #the word is in dictionary
neg_dict[word] += 1
# Go through the positive reviews
for review in os.listdir(path + "/pos"):
with open(os.getcwd()+"/"+path+"/pos/"+review)as f:
unique_words = set(f.read().split())
for word in unique_words:
if not pos_dict.get(word): #check if the word is in dictionary
pos_dict[word] = 1
else: #the word is in dictionary
pos_dict[word] += 1
return neg_dict, pos_dict
def generate_frequency_dict(path): #same as above, but normalizing by length (Piazza)
'''
Generates word frequency dictionaries for positive and negative reviews
(normalizing by length)
:param path: the location of the training set
:return: 2 dictionaries for positive and negative reviews
'''
pos_dict = {} #format = (key, value) = (word, frequency)
neg_dict = {}
# Go through the negative reviews
for review in os.listdir(path + "/neg"):
with open(os.getcwd()+"/"+path+"/neg/"+review) as f:
unique_words = set(f.read().split())
for word in unique_words:
if not neg_dict.get(word): #check if the word is in dictionary
neg_dict[word] = 1. / len(unique_words)
else: #the word is in dictionary
neg_dict[word] += 1. / len(unique_words)
# Go through the positive reviews
for review in os.listdir(path + "/pos"):
with open(os.getcwd()+"/"+path+"/pos/"+review)as f:
unique_words = set(f.read().split())
for word in unique_words:
if not pos_dict.get(word): #check if the word is in dictionary
pos_dict[word] = 1. / len(unique_words)
else: #the word is in dictionary
pos_dict[word] += 1. / len(unique_words)
return neg_dict, pos_dict
def smoothen_probability(dict, m, k, size = 800):
'''
Applies delta smoothing to probabilities
:param dict: word frequency dictionary
:param m: parameter m required for delta smoothing
:param k: parameter k required for delta smoothing
:param size: size can be specified (default 800)
:return: smoothened word frequency dictionary
'''
denominator_pos = k + np.sum(dict.values()) * len(dict.keys())
for word in dict:
dict[word] = (dict[word]* len(dict.keys()) + m*k) / denominator_pos
return dict
def classify(path, m, k, neg_dict, pos_dict, size):
'''
Performe the Naive Bayes classification of reviews specified in neg_dict/pos_dict
:param path: location of the dataset
:param m: parameter m required for delta smoothing
:param k: parameter k required for delta smoothing
:param neg_dict: dictionary of negative reviews
:param pos_dict: dictionary of positive reviews
:param size: size of the training set
:return: scores obtained on negative and positive reviews
'''
neg_score = 0.
pos_score = 0.
# Go through the negative reviews
for review in os.listdir(path + "/neg"):
with open(os.getcwd()+"/"+path+"/neg/"+review) as f:
unique_words = set(f.read().split())
neg_sum = 0. #compute P(ai|negative)
pos_sum = 0. #compute P(ai|positive) [then take the largest]
denominator_neg = k + sum(neg_dict.values()) * len(unique_words)
denominator_pos = k + sum(pos_dict.values()) * len(unique_words)
for word in unique_words:
if neg_dict.get(word): #if word is in dictionary
neg_sum += log((neg_dict[word] * len(unique_words) + m * k) / denominator_neg)
else:
neg_sum += log((m * k) / denominator_neg)
if pos_dict.get(word):
pos_sum += log((pos_dict[word] * len(unique_words) + m * k) / denominator_pos)
else:
pos_sum += log((m * k) / denominator_pos)
if neg_sum > pos_sum:
# Guessed correcctly
neg_score += 1
# Go through the positive reviews
for review in os.listdir(path + "/pos"):
with open(os.getcwd()+"/"+path+"/pos/"+review) as f:
unique_words = set(f.read().split())
neg_sum = 0. #compute P(ai|negative)
pos_sum = 0. #compute P(ai|positive) [then take the largest]
denominator_neg = k + sum(neg_dict.values()) * len(unique_words)
denominator_pos = k + sum(pos_dict.values()) * len(unique_words)
for word in unique_words:
if neg_dict.get(word): # if word is in dictionary
neg_sum += log((neg_dict[word] * len(unique_words) + m * k) / denominator_neg)
else:
neg_sum += log((m * k) / denominator_neg)
if pos_dict.get(word):
pos_sum += log((pos_dict[word] * len(unique_words) + m * k) / denominator_pos)
else:
pos_sum += log((m * k) / denominator_pos)
if pos_sum > neg_sum:
# Guessed correcctly
pos_score += 1
return neg_score / (size/2.), pos_score / (size/2.)
def max_validation():
'''
Performs classification on different values of m and k for finding the optimal parameters
'''
combined_score = 0.
mk = 0
for m in np.arange(0.1,0.9,0.1):
for k in np.arange(5.,20.,1.):
dicts = generate_frequency_dict('training_set')
score = classify('validation_set', m, k, dicts[0], dicts[1], 200)
if ((score[0] + score[1])/2.) > combined_score:
combined_score = (score[0] + score[1])/2
mk = (m,k)
print("validation set: " + "m = " + str(m) + " k = " + str(k), score)
print("=====================================================")
print (combined_score,mk)
def get_numwords(neg_dict, pos_dict):
'''
Combines dictionary keys into one list (vocabulary)
:param neg_dict: dictionary of negative reviews
:param pos_dict: dictionary of positive reviews
:return: list containing all words
'''
combined_list = []
# Convert the keys of the dicts into a list
for word in neg_dict:
combined_list.append(word)
for word in pos_dict:
combined_list.append(word)
# Remove duplicates in the list
combined_list = list(set(combined_list))
num_words = len(combined_list)
return num_words, combined_list
def most_predictive(neg_dict, pos_dict):
'''
Obtains and prints the top 10 words that distinguish a review polarity
:param neg_dict: dictionary of negative reviews
:param pos_dict: dictionary of positive reviews
'''
neg_words = {}
pos_words = {}
#take the difference of the probabilities
for word in neg_dict:
if word not in pos_dict:
neg_words[word] = neg_dict[word]
else:
neg_words[word] = neg_dict[word] - pos_dict[word]
for word in pos_dict:
if word not in neg_dict:
pos_words[word] = pos_dict[word]
else:
pos_words[word] = pos_dict[word] - neg_dict[word]
neg_words = sorted(neg_words.items(), key=lambda x: x[1], reverse=True)[:10]
pos_words = sorted(pos_words.items(), key=lambda x: x[1], reverse=True)[:10]
print("Top 10 negative words:")
for i in neg_words:
print(i[0])
print("Top 10 positive words:")
for i in pos_words:
print(i[0])
return neg_words, pos_words
# ======================================================================================================================
# ========================================== Definitions end ===========================================================
# ======================================================================================================================
if prepare_data:
#generate 2000 unique numbers
random.seed(0)
neg_index = random.choice(range(1000), 1000, replace=False) #i.e 0-size
random.seed(1)
pos_index = random.choice(range(1000), 1000, replace=False) #i.e 0-size
#generate training set (size = 1600)
split_dataset(neg_index, pos_index, "/training_set", 1600)
print("Please wait while the training set is being processed...")
#generate the testing set (size = 200)
split_dataset(neg_index[800:], pos_index[800:], "/test_set", 200)
print("Please wait while the testing set is being processed...")
#generate the validation set (size = 200)
split_dataset(neg_index[900:], pos_index[900:], "/validation_set", 200)
print("Please wait while the validation set set is being processed...")
if run_part1:
#combine all the dictionaries together, computing word frequency
dicts1 = generate_count_dict('training_set')
dicts2 = generate_count_dict('test_set')
dicts3 = generate_count_dict('validation_set')
neg_dict = merge(dicts1[0], dicts2[0], lambda x,y: x+y)
neg_dict = merge(neg_dict , dicts3[0], lambda x,y: x+y)
pos_dict = merge(dicts1[1], dicts2[1], lambda x,y: x+y)
pos_dict = merge(pos_dict , dicts3[1], lambda x,y: x+y)
print("=======================================================")
words = ['annoying', 'terrible', 'stupid', 'wasted']
for word in words:
print("Occurrence of " + word + " in the negative dataset is " + str(neg_dict[word]))
for word in words:
print("Occurrence of " + word + " in the positive dataset is " + str(pos_dict[word]))
print("=======================================================")
words = ['like', 'liked', 'enjoy', 'enjoyed', 'love', 'loved', 'good']
for word in words:
print("Occurrence of " + word + " in the negative dataset is " + str(neg_dict[word]))
for word in words:
print("Occurrence of " + word + " in the positive dataset is " + str(pos_dict[word]))
print("=======================================================")
words = ['hilarious', 'terrific']
for word in words:
print("Occurrence of " + word + " in the negative dataset is " + str(neg_dict[word]))
for word in words:
print("Occurrence of " + word + " in the positive dataset is " + str(pos_dict[word]))
if run_part2:
dicts = generate_frequency_dict('training_set')
m = 0.5
k = 16.
neg_dict = dicts[0]
pos_dict = dicts[1]
score1 = classify('validation_set', m, k, neg_dict, pos_dict, 200)
score2 = classify('test_set', m, k, neg_dict, pos_dict, 200)
score3 = classify('training_set', m, k, neg_dict, pos_dict, 1600)
print("Performances (negative, positive)")
print("Training performance: " + str(score3))
print("Validation performance: " + str(score1))
print("Test performance: "+ str(score2))
print("Overall:")
print("Training performance: " + str((score3[0]+score3[1])/2.))
print("Validation performance: " + str((score1[0]+score1[1])/2.))
print("Test performance: " + str((score2[0]+score2[1])/2.))
if tuning_mk:
max_validation()
if run_part3:
dicts = generate_frequency_dict('training_set')
m = 0.5
k = 16.
neg_dict = smoothen_probability(dicts[0], m, k)
pos_dict = smoothen_probability(dicts[1], m, k)
most_predictive(neg_dict, pos_dict)