-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentimentPract2.py
512 lines (388 loc) · 17.4 KB
/
SentimentPract2.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env python
import re, random, math, collections, itertools
from textblob import TextBlob
PRINT_ERRORS=0
#------------- Function Definitions ---------------------
def readFiles(sentimentDictionary,sentencesTrain,sentencesTest,sentencesNokia):
#reading pre-labeled input and splitting into lines
posSentences = open('rt-polarity.pos', 'r', encoding="ISO-8859-1")
posSentences = re.split(r'\n', posSentences.read())
negSentences = open('rt-polarity.neg', 'r', encoding="ISO-8859-1")
negSentences = re.split(r'\n', negSentences.read())
posSentencesNokia = open('nokia-pos.txt', 'r')
posSentencesNokia = re.split(r'\n', posSentencesNokia.read())
negSentencesNokia = open('nokia-neg.txt', 'r', encoding="ISO-8859-1")
negSentencesNokia = re.split(r'\n', negSentencesNokia.read())
posDictionary = open('positive-words.txt', 'r', encoding="ISO-8859-1")
posWordList = posDictionary.readlines()
posWordList = [line.strip() for line in posWordList if not line.startswith(";") and not line == '\n']
#posWordList = re.findall(r"[a-z\-]+", posDictionary.read())
negDictionary = open('negative-words.txt', 'r', encoding="ISO-8859-1")
negWordList = negDictionary.readlines()
negWordList = [line.strip() for line in negWordList if not line.startswith(";") and not line == '\n']
#negWordList = re.findall(r"[a-z\-]+", negDictionary.read())
for i in posWordList:
sentimentDictionary[i] = 1
for i in negWordList:
sentimentDictionary[i] = -1
#create Training and Test Datsets:
#We want to test on sentences we haven't trained on, to see how well the model generalses to previously unseen sentences
#create 90-10 split of training and test data from movie reviews, with sentiment labels
for i in posSentences:
if random.randint(1,10)<2:
sentencesTest[i]="positive"
else:
sentencesTrain[i]="positive"
for i in negSentences:
if random.randint(1,10)<2:
sentencesTest[i]="negative"
else:
sentencesTrain[i]="negative"
#create Nokia Datset:
for i in posSentencesNokia:
sentencesNokia[i]="positive"
for i in negSentencesNokia:
sentencesNokia[i]="negative"
#----------------------------End of data initialisation ----------------#
#calculates p(W|Positive), p(W|Negative) and p(W) for all words in training data
def trainBayes(sentencesTrain, pWordPos, pWordNeg, pWord):
posFeatures = [] # [] initialises a list [array]
negFeatures = []
freqPositive = {} # {} initialises a dictionary [hash function]
freqNegative = {}
dictionary = {}
posWordsTot = 0
negWordsTot = 0
allWordsTot = 0
#iterate through each sentence/sentiment pair in the training data
for sentence, sentiment in sentencesTrain.items():
wordList = re.findall(r"[\w']+", sentence)
#TO DO:
#Populate bigramList (initialised below) by concatenating adjacent words in the sentence.
#You might want to seperate the words by _ for readability, so bigrams such as:
#You_might, might_want, want_to, to_seperate....
bigramList=wordList.copy() #initialise bigramList
for x in range(len(wordList)-1):
bigramList.append(wordList[x]+"_" + wordList[x+1])
#-------------Finish populating bigramList ------------------#
#TO DO: when you have populated bigramList, uncomment out the line below and , and comment out the unigram line to make use of bigramList rather than wordList
for word in bigramList: #calculate over bigrams
# for word in wordList: #calculate over unigrams
allWordsTot += 1 # keeps count of total words in dataset
if not (word in dictionary):
dictionary[word] = 1
if sentiment=="positive" :
posWordsTot += 1 # keeps count of total words in positive class
#keep count of each word in positive context
if not (word in freqPositive):
freqPositive[word] = 1
else:
freqPositive[word] += 1
else:
negWordsTot+=1# keeps count of total words in negative class
#keep count of each word in positive context
if not (word in freqNegative):
freqNegative[word] = 1
else:
freqNegative[word] += 1
for word in dictionary:
#do some smoothing so that minimum count of a word is 1
if not (word in freqNegative):
freqNegative[word] = 1
if not (word in freqPositive):
freqPositive[word] = 1
# Calculate p(word|positive)
pWordPos[word] = freqPositive[word] / float(posWordsTot)
# Calculate p(word|negative)
pWordNeg[word] = freqNegative[word] / float(negWordsTot)
# Calculate p(word)
pWord[word] = (freqPositive[word] + freqNegative[word]) / float(allWordsTot)
#---------------------------End Training ----------------------------------
#implement naive bayes algorithm
#INPUTS:
# sentencesTest is a dictonary with sentences associated with sentiment
# dataName is a string (used only for printing output)
# pWordPos is dictionary storing p(word|positive) for each word
# i.e., pWordPos["apple"] will return a real value for p("apple"|positive)
# pWordNeg is dictionary storing p(word|negative) for each word
# pWord is dictionary storing p(word)
# pPos is a real number containing the fraction of positive reviews in the dataset
def testBayes(sentencesTest, dataName, pWordPos, pWordNeg, pWord,pPos):
pNeg=1-pPos
#These variables will store results (you do not need them)
total=0
correct=0
totalpos=0
totalpospred=0
totalneg=0
totalnegpred=0
correctpos=0
correctneg=0
#for each sentence, sentiment pair in the dataset
for sentence, sentiment in sentencesTest.items():
wordList = re.findall(r"[\w']+", sentence)#collect all words
#TO DO: Exactly what you did in the training function:
#Populate bigramList by concatenating adjacent words in wordList.
bigramList=wordList.copy() #initialise bigramList
for x in range(len(wordList)-1):
bigramList.append(wordList[x]+"_" + wordList[x+1])
#------------------finished populating bigramList--------------
pPosW=pPos
pNegW=pNeg
for word in bigramList: #calculate over bigrams
# for word in wordList: #calculate over unigrams
if word in pWord:
if pWord[word]>0.00000001:
pPosW *=pWordPos[word]
pNegW *=pWordNeg[word]
prob=0;
if pPosW+pNegW >0:
prob=pPosW/float(pPosW+pNegW)
total+=1
if sentiment=="positive":
totalpos+=1
if prob>0.5:
correct+=1
correctpos+=1
totalpospred+=1
else:
correct+=0
totalnegpred+=1
#print("POSITIVE tagged as NEGATIVE")
#print(sentence)
if PRINT_ERRORS:
print ("ERROR (pos classed as neg %0.2f):" %prob + sentence)
else:
totalneg+=1
if prob<=0.5:
correct+=1
correctneg+=1
totalnegpred+=1
else:
correct+=0
totalpospred+=1
#print("NEGATIVE tagged as POSITIVE")
#print(sentence)
if PRINT_ERRORS:
print ("ERROR (neg classed as pos %0.2f):" %prob + sentence)
acc=correct/float(total)
print (dataName + " Accuracy (All)=%0.2f" % acc + " (%d" % correct + "/%d" % total + ")\n")
precision_pos=correctpos/float(totalpospred)
recall_pos=correctpos/float(totalpos)
precision_neg=correctneg/float(totalnegpred)
recall_neg=correctneg/float(totalneg)
f_pos=2*precision_pos*recall_pos/(precision_pos+recall_pos);
f_neg=2*precision_neg*recall_neg/(precision_neg+recall_neg);
print (dataName + " Precision (Pos)=%0.2f" % precision_pos + " (%d" % correctpos + "/%d" % totalpospred + ")")
print (dataName + " Recall (Pos)=%0.2f" % recall_pos + " (%d" % correctpos + "/%d" % totalpos + ")")
print (dataName + " F-measure (Pos)=%0.2f" % f_pos)
print (dataName + " Precision (Neg)=%0.2f" % precision_neg + " (%d" % correctneg + "/%d" % totalnegpred + ")")
print (dataName + " Recall (Neg)=%0.2f" % recall_neg + " (%d" % correctneg + "/%d" % totalneg + ")")
print (dataName + " F-measure (Neg)=%0.2f" % f_neg + "\n")
# This is a simple classifier that uses a sentiment dictionary to classify
# a sentence. For each word in the sentence, if the word is in the positive
# dictionary, it adds 1, if it is in the negative dictionary, it subtracts 1.
# If the final score is above a threshold, it classifies as "Positive",
# otherwise as "Negative"
def testDictionary(sentencesTest, dataName, sentimentDictionary, threshold):
total=0
correct=0
totalpos=0
totalneg=0
totalpospred=0
totalnegpred=0
correctpos=0
correctneg=0
for sentence, sentiment in sentencesTest.items():
Words = re.findall(r"[\w']+", sentence)
score=0
for word in Words:
if word in sentimentDictionary:
score+=sentimentDictionary[word]
total+=1
if sentiment=="positive":
totalpos+=1
if score>=threshold:
correct+=1
correctpos+=1
totalpospred+=1
else:
correct+=0
totalnegpred+=1
else:
totalneg+=1
if score<threshold:
correct+=1
correctneg+=1
totalnegpred+=1
else:
correct+=0
totalpospred+=1
acc=correct/float(total)
print (dataName + " Accuracy (All)=%0.2f" % acc + " (%d" % correct + "/%d" % total + ")\n")
precision_pos=correctpos/float(totalpospred)
recall_pos=correctpos/float(totalpos)
precision_neg=correctneg/float(totalnegpred)
recall_neg=correctneg/float(totalneg)
f_pos=2*precision_pos*recall_pos/(precision_pos+recall_pos);
f_neg=2*precision_neg*recall_neg/(precision_neg+recall_neg);
print (dataName + " Precision (Pos)=%0.2f" % precision_pos + " (%d" % correctpos + "/%d" % totalpospred + ")")
print (dataName + " Recall (Pos)=%0.2f" % recall_pos + " (%d" % correctpos + "/%d" % totalpos + ")")
print (dataName + " F-measure (Pos)=%0.2f" % f_pos)
print (dataName + " Precision (Neg)=%0.2f" % precision_neg + " (%d" % correctneg + "/%d" % totalnegpred + ")")
print (dataName + " Recall (Neg)=%0.2f" % recall_neg + " (%d" % correctneg + "/%d" % totalneg + ")")
print (dataName + " F-measure (Neg)=%0.2f" % f_neg + "\n")
# This is a simple classifier that uses a sentiment dictionary to classify
# a sentence. For each word in the sentence, if the word is in the positive
# dictionary, it adds 1, if it is in the negative dictionary, it subtracts 1.
# If the final score is above a threshold, it classifies as "Positive",
# otherwise as "Negative"
def testImprovedDictionary(sentencesTest, dataName, sentimentDictionary, threshold):
total=0
correct=0
totalpos=0
totalneg=0
totalpospred=0
totalnegpred=0
correctpos=0
correctneg=0
for sentence, sentiment in sentencesTest.items():
score=0
positiveMultiplier = 1
negativeMultiplier = 1
blob = TextBlob(sentence)
for phrase in blob.sentences:
#print(phrase)
#print(phrase.sentiment.polarity)
if phrase.sentiment.polarity > 0:
positiveMultiplier = 2
#score += len(sentence) / 2
elif phrase.sentiment.polarity < 0:
negativeMultiplier = 4
#score -= len(sentence) / 2
Words = re.findall(r"[\w']+", sentence)
for word in Words:
if word in sentimentDictionary:
if sentimentDictionary[word] > 0:
score+=sentimentDictionary[word]*positiveMultiplier
elif sentimentDictionary[word] < 0:
score+=sentimentDictionary[word]*negativeMultiplier
else:
score+=sentimentDictionary[word]
total+=1
if sentiment=="positive":
totalpos+=1
if score>=threshold:
correct+=1
correctpos+=1
totalpospred+=1
else:
correct+=0
totalnegpred+=1
#print("Should be +")
#print(sentence)
#print(positiveMultiplier)
#print(negativeMultiplier)
else:
totalneg+=1
if score<threshold:
correct+=1
correctneg+=1
totalnegpred+=1
else:
correct+=0
totalpospred+=1
#print("Should be -")
#print(sentence)
#print(positiveMultiplier)
#print(negativeMultiplier)
acc=correct/float(total)
print (dataName + " Accuracy (All)=%0.2f" % acc + " (%d" % correct + "/%d" % total + ")\n")
precision_pos=correctpos/float(totalpospred)
recall_pos=correctpos/float(totalpos)
precision_neg=correctneg/float(totalnegpred)
recall_neg=correctneg/float(totalneg)
f_pos=2*precision_pos*recall_pos/(precision_pos+recall_pos);
f_neg=2*precision_neg*recall_neg/(precision_neg+recall_neg);
print (dataName + " Precision (Pos)=%0.2f" % precision_pos + " (%d" % correctpos + "/%d" % totalpospred + ")")
print (dataName + " Recall (Pos)=%0.2f" % recall_pos + " (%d" % correctpos + "/%d" % totalpos + ")")
print (dataName + " F-measure (Pos)=%0.2f" % f_pos)
print (dataName + " Precision (Neg)=%0.2f" % precision_neg + " (%d" % correctneg + "/%d" % totalnegpred + ")")
print (dataName + " Recall (Neg)=%0.2f" % recall_neg + " (%d" % correctneg + "/%d" % totalneg + ")")
print (dataName + " F-measure (Neg)=%0.2f" % f_neg + "\n")
#Print out n most useful predictors
def mostUseful(pWordPos, pWordNeg, pWord, n, sentimentDictionary):
positiveCounter = 0
negativeCounter = 0
totalKnown = 0
predictPower={}
for word in pWord:
if pWordNeg[word]<0.0000001:
predictPower[word]=1000000000
else:
predictPower[word]=pWordPos[word] / (pWordPos[word] + pWordNeg[word])
if word in sentimentDictionary:
totalKnown +=1
sortedPower = sorted(predictPower, key=predictPower.get)
head, tail = sortedPower[:n], sortedPower[len(predictPower)-n:]
'''
print ("NEGATIVE:")
print (head)
print ("\nPOSITIVE:")
print (tail)
'''
for word in head:
if word in sentimentDictionary:
negativeCounter +=1
for word in tail:
if word in sentimentDictionary:
positiveCounter +=1
#print(negativeCounter)
#print(positiveCounter)
print("Known words: "+str(totalKnown)+"/"+str(len(pWord)))
#We use the sentiwordnet library to get the score of a word. Negative numbers are for
def getScore(word):
output = list(swn.senti_synsets(word))
if output:
#print("GOOD WORD")
analysedWord = output[0]
#print(analysedWord)
#print(analysedWord.pos_score())
#print(analysedWord.neg_score())
#print(analysedWord.pos_score() - analysedWord.neg_score())
totalScore = analysedWord.pos_score() - analysedWord.neg_score()
if totalScore > 0:
return 1
if totalScore <0:
return -1
else:
return 0
else:
return 0
#---------- Main Script --------------------------
sentimentDictionary={} # {} initialises a dictionary [hash function]
sentencesTrain={}
sentencesTest={}
sentencesNokia={}
#initialise datasets and dictionaries
readFiles(sentimentDictionary,sentencesTrain,sentencesTest,sentencesNokia)
pWordPos={} # p(W|Positive)
pWordNeg={} # p(W|Negative)
pWord={} # p(W)
#build conditional probabilities using training data
trainBayes(sentencesTrain, pWordPos, pWordNeg, pWord)
#run naive bayes classifier on datasets
print ("********Naive Bayes********")
testBayes(sentencesTrain, "Films (Train Data, Naive Bayes)\t", pWordPos, pWordNeg, pWord,0.5)
testBayes(sentencesTest, "Films (Test Data, Naive Bayes)\t", pWordPos, pWordNeg, pWord,0.5)
testBayes(sentencesNokia, "Nokia (All Data, Naive Bayes)\t", pWordPos, pWordNeg, pWord,0.7)
print("********Original Rule-Based********")
testDictionary(sentencesTrain, "Films (Train Data, Rule-Based)\t", sentimentDictionary, 1)
testDictionary(sentencesTest, "Films (Test Data, Rule-Based)\t", sentimentDictionary, 1)
testDictionary(sentencesNokia, "Nokia (All Data, Rule-Based)\t", sentimentDictionary, 1)
print("********Improved Rule-Based********")
testImprovedDictionary(sentencesTrain, "Films (Train Data, Rule-Based)\t", sentimentDictionary, 1)
testImprovedDictionary(sentencesTest, "Films (Test Data, Rule-Based)\t", sentimentDictionary, 1)
testImprovedDictionary(sentencesNokia, "Nokia (All Data, Rule-Based)\t", sentimentDictionary, 0)
# print most useful words
mostUseful(pWordPos, pWordNeg, pWord, 200, sentimentDictionary)