forked from abijith-kp/nlp-sentiment-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
405 lines (327 loc) · 9.24 KB
/
test.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
# please do not remove this comment
# Bird, Steven, Edward Loper and Ewan Klein (2009). Natural Language Processing
# with Python. O’Reilly Media Inc
#!/usr/bin/python
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.tag import pos_tag
from nltk.chunk import RegexpParser
from sentiwordnet import SentiWordNetCorpusReader, SentiSynset
import re, string
####################### CONSTANTS #############################################
NP = 0
VP = 1
NN = 2
VB = 3
ADV = 4
ADJ = 5
PRD = 6
CLS = 7
####################### FUNCTIONS #############################################
def addNounPhrase(tree): #returns tuple with <[ADJ] [NN]>
adj = []
nn = []
#if len(tree) < 2:
#try:
# if tree[0][1] == "JJ":
# adj.append(t[0])
# elif t[0][1] == "NN" or t[0][1] == "NNP" or t[0][1] == "NNS":
# nn.append(t[0])
# elif t[0][1]=="NNPS":
# nn.append(t[0])
# elif t[0][1]=="PRP" or t[0][1] == "PRP$":
# nn.append(t[0])
#except:
# pass
#return (adj, nn)
for t in tree:
try:
if t[1] == "JJ":
adj.append(t[0])
elif t[1] == "NN" or t[1] == "NNP" or t[1] == "NNS" or t[1]=="NNPS":
nn.append(t[0])
elif t[1]=="PRP" or t[1] == "PRP$":
nn.append(t[0])
except:
continue
#print (adj , nn)
return (adj , nn)
def addVerbPhrase(tree):
adv = []
vb = []
for t in tree:
#print t[1]
try:
if t[1] == "RB" or t[1] == "RBR" or t[1] == "RBS":
adv.append(t[0])
except:
if t.node == "V":
vb.append(t[0][0])
#print (adv , vb)
return (adv , vb)
def addPredicate(tree):
tmp = (addNounPhrase(tree[0]), addVerbPhrase(tree[1]))
return tmp
def addClause1(tree):
tmp = (addNounPhrase(tree[0]), addPredicate(tree[1]))
return tmp
def addClause2(tree):
#print tree
tmp = (addNounPhrase(tree[1]), addPredicate(tree[0]))
#print tmp, ">>"
return tmp
################################################################################
##### Evaluation of values #####################################################
def findScoreWord(word, dType):
swn_filename = 'SentiWordNet_3.0.0_20130122.txt'
swn = SentiWordNetCorpusReader(swn_filename)
word = re.sub('[%s]' % re.escape(string.punctuation), ' ', word)
word = word.lower()
#print word
wS = 0
for w in word.split():
#print w
if dType == NN:
test = swn.senti_synsets(w, 'n')
elif dType == ADJ:
test = swn.senti_synsets(w, 'a')
elif dType == VB:
test = swn.senti_synsets(w, 'v')
elif dType == ADV:
test = swn.senti_synsets(w, 'r')
try:
if test[0].pos_score < 0.1:
wS += -test[0].neg_score
elif test[0].neg_score < 0.1:
wS += test[0].pos_score
else:
wS = test[0].pos_score
except:
continue
#print word, wS
if len(word.split()) == 0:
return 0
return wS/len(word.split())
def evalScore(one , two, three):
t = 0
if three == NP:
if one >= 0 and two >= 0:
t = +(abs(two) + (1-abs(two)) * abs(one))
elif one >= 0 and two < 0:
t = -(abs(two) + (1-abs(two)) * abs(one))
elif one < 0 and two >= 0:
t = one
elif one < 0 and two < 0:
t = -(abs(two) + (1-abs(two)) * abs(one))
elif three == VP:
if one >= 0 and two >= 0:
t = +(abs(one) * (1-abs(two)))
elif one >= 0 and two < 0:
t = -(abs(one) * (1-abs(two)))
elif one < 0 and two >= 0:
t = -(abs(one) * (1-abs(two)))
elif one < 0 and two < 0:
t = +(abs(one) * (1-abs(two)))
#print t, " ", one ," ", two
elif three == PRD:
if one >= 0 and two >= 0:
t = +(abs(two) * (1-abs(one)))
elif one < 0 and two >= 0:
t = -(abs(two) * (1-abs(one)))
elif one >= 0 and two < 0:
t = -(abs(two) * (1-abs(one)))
elif one < 0 and two < 0:
t = +(abs(two) * (1-abs(one)))
elif three == CLS:
#print one, ">>>>>", two
if abs(one) > abs(two):
t = one;
else:
t = two
return t
################################################################################
def findScoreSO(tmpNP): #tmpNP = [(<[ADJ] [NN]>) , ...]
scoreAdj = 0
scoreNn = 0
totalScore = 0
for temp in tmpNP:
scoreAdj = 0
scoreNn = 0
for t1 in temp[0]:
scoreAdj += findScoreWord(t1, ADJ)
for t1 in temp[1]:
scoreNn += findScoreWord(t1, NN)
if len(temp[0]) != 0 and len(temp[1]) != 0:
totalScore += evalScore((scoreAdj/len(temp[0])),
(scoreNn/len(temp[1])), NP)
elif len(temp[0]) == 0 and len(temp[1]) != 0:
totalScore += (scoreNn/len(temp[1]))
elif len(temp[0]) != 0 and len(temp[1]) == 0:
totalScore += (scoreAdj/len(temp[0]))
else:
totalScore += 0
if len(tmpNP) != 0:
return totalScore/len(tmpNP)
else:
return 0
def findScoreVP(tmpVP):
scoreAdv = 0
scoreVb = 0
totalScore = 0
for temp in tmpVP:
scoreAdv = 0
scoreVb = 0
for t1 in temp[0]:
scoreAdv += findScoreWord(t1, ADV)
for t1 in temp[1]:
scoreVb += findScoreWord(t1, VB)
if len(temp[0]) != 0 and len(temp[1]) != 0:
totalScore += evalScore((scoreAdv/len(temp[0])),
(scoreVb/len(temp[1])), VP)
elif len(temp[0]) == 0 and len(temp[1]) != 0:
totalScore += (scoreVb/len(temp[1]))
elif len(temp[0]) != 0 and len(temp[1]) == 0:
totalScore += (scoreAdv/len(temp[0]))
else:
totalScore += 0
#print totalScore, temp
#print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
if len(tmpVP) != 0:
return totalScore/len(tmpVP)
else:
return 0
def findScorePredicate(tmpPrd):
totalScore = 0
tmpNP = []
tmpVP = []
#print tmpPrd
for temp in tmpPrd:
tmpNP.append(temp[0])
tmpVP.append(temp[1])
#print type(tmpPrd), " ", type(temp[1])
totalScore += evalScore(findScoreSO(tmpNP), findScoreVP(tmpVP), PRD)
#print findScoreSO(tmpNP), "..", findScoreVP(tmpVP), "..", totalScore
if len(tmpPrd) != 0:
return totalScore/len(tmpPrd)
else:
return 0
def findScoreClause(tmpCls):
totalScore = 0
tmpNP = []
tmpPrd = []
#print tmpPrd
for temp in tmpCls:
tmpNP.append(temp[0])
tmpPrd.append(temp[1])
#print tmpCls, " ", tmpCls
totalScore +=evalScore(findScoreSO(tmpNP),findScorePredicate(tmpPrd) ,
CLS)
#print findScoreSO(tmpNP), "..", findScorePredicate(tmpPrd), ".."
if len(tmpCls) != 0:
return totalScore/len(tmpPrd)
else:
return 0
################################################################################
################################################################################
review = " "
punctuation = [",",";",".",":",","]
grammer = '''
NP: {<DT>? <JJ>* <NN.*|PR.*>*}
P: {<IN>}
V: {<V.*>}
PP: {<P> <NP>}
'''
print "Loading input file..."
fileReview = open("/home/abijith/aclImdb/test/neg/0_2.txt", "r") #open
#file to retrive movie
#reviews
arr = fileReview.read()
while arr: #removes all trilling white spaces and
review += arr.strip() #adds it to var review
arr = fileReview.read()
fileReview.close()
print "Sentence tokenization..."
review_dict = sent_tokenize(review) #tokenizes sentences
arr_pos = []
removed = []
print "POS tagging for words..."
#arr_sent = pos_tag(review_dict) #tagging words for semantic
#annotation
for sent in review_dict: #adding individual sentences after tagging
arr_pos.extend([pos_tag(sent.split())].__iter__())
################################################################################
################################################################################
print "Loading Parser..."
npc = RegexpParser(grammer)
#t = npc.parse(tmp_arr_pos[0])
print "Finished loading..."
#print len(t)
#t.draw()
#help(t)
sentCount = 1
sentScore = [] #tuple with (Subj-Obj , Verb-P , )
totalS = []
print "Processing input..."
print "Number of sentences to process: ", len(arr_pos)
for i in arr_pos:
print "Reading sentence ", sentCount
sentCount += 1
t = npc.parse(i)
#print t
tmpVP = []
tmpNP = []
tmpPrd = []
tmpCls = []
x1 = ""
for x in t:
#print x.node
try:
if x.node == "VP":
#print x
x1 = addVerbPhrase(x)
tmpVP.append(x1)
if x.node == "NP":
x1 = addNounPhrase(x)
tmpNP.append(x1)
elif x.node == "PRD":
x1 = addPredicate(x)
tmpPrd.append(x1)
elif x.node == "CLS1":
x1 = addClause1(x)
tmpCls.append(x1)
#print x1
elif x.node == "CLS2":
x1 = addClause2(x)
tmpCls.append(x1)
#print x1
except:
continue
#print findScorePredicate(tmpPrd), "\n"
#sentScore.append((findScoreSO(tmpNP), findScoreVP(tmpVP),
#findScorePredicate(tmpPrd), findScoreClause(tmpCls)))
#totalS.append(1)
tNp = findScoreSO(tmpNP)
tVp = findScoreVP(tmpVP)
tPrd = findScorePredicate(tmpPrd)
tCls = findScoreClause(tmpCls)
#print tNp, tVp, tPrd, tCls
#print totalS
#print tNp != 0
########################## EVALUATE SENTENCE SCORE #############################
if tNp == 0 and tVp == 0 and tPrd == 0 and tCls == 0:
totalS.append(0)
continue
totalS.append(1)
if tNp != 0:
totalS.append(totalS.pop() * tNp)
#print totalS
if tVp != 0:
totalS.append(totalS.pop() * tVp)
#print totalS
if tPrd != 0:
totalS.append(totalS.pop() * tPrd)
#print totalS
if tCls != 0:
totalS.append(totalS.pop() * tCls)
#print totalS
for i in totalS:
print i