-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules_implement.py
executable file
·733 lines (668 loc) · 26 KB
/
rules_implement.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import NE_extractor as named_entity
import nltk
import os
#import sentiment_mod as s
from textblob import TextBlob
from nltk.parse.stanford import StanfordDependencyParser
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import re
os.environ['CLASSPATH'] = "/Users/pradeesh/Downloads/stanford-parser-full-2018-10-17/" # add path where is the StandfordDep
dep_parser=StanfordDependencyParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
analyser = SentimentIntensityAnalyzer()
p=open('positive_sent_words.txt','r' ,encoding = "ISO-8859-1") #positive sentiment words lexicon
n=open('negative_sent_words.txt','r', encoding = "ISO-8859-1") #negatve sentiment words lexicon
p=list(p)
n=list(n)
p=[x[:-1] for x in p]
n=[q[:-1] for q in n]
#produce pos tags of sentence for use in various rule implementations
def tag(sent):
words=nltk.word_tokenize(sent)
tagged=nltk.pos_tag(words)
return tagged
def sentiment_analyzer_scores(sentence):
score = analyser.polarity_scores(sentence)
return(score['compound'])
#implement the nine rule in nine separate functions (R1-R9)
# pronoun and pronoun based adjectives
def R1(sent):
t=tag(sent)
w=nltk.word_tokenize(sent)
l=[]
just_tags=[]
for i in range(len(t)):
just_tags.append(t[i][1])
#get all words which are pronouns
if t[i][1]=='PRP':
l.append(t[i][0])
if 'WRB' in just_tags or 'WP' in just_tags or 'WDT' in just_tags:
l.append(t[i][0])
if '.' in just_tags and '?' in w[i:]:
l.append(t[i][0])
# below adds the pronoun again to list if it is being praised
if i+1 < len(t):
if t[i+1][1]=='JJ' and sentiment_analyzer_scores(t[i][0]+' '+t[i+1][0])>=0.05:
l.append(t[i][0])
if i+2 < len(t):
if t[i+2][1]=='JJ' and sentiment_analyzer_scores(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0])>=0.05:
l.append(t[i][0])
if i+3 < len(t):
if t[i+3][1]=='JJ' and sentiment_analyzer_scores(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0])>=0.05:
l.append(t[i][0])
if i+4 < len(t):
if t[i+4][1]=='JJ' and sentiment_analyzer_scores(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0]+' '+t[i+4][0])>=0.05:
l.append(t[i][0])
if i>0:
if sentiment_analyzer_scores(t[i-1][0]+' '+t[i][0])>0.05:
l.append(t[i][0])
if i>1:
if sentiment_analyzer_scores(t[i-2][0]+' '+t[i-1][0]+' '+t[i][0])>0.05:
l.append(t[i][0])
#possesive pronouns along with their object
if t[i][1]=='PRP$':
if i+1==len(t):
l.append(t[i][0])
if i+1 < len(t):
if t[i+1][1]=='NN' or t[i+1][1]=='NNP' or t[i+1][1]=='NNS' or t[i+1][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+1][0])
l.append(t[i][0]+' '+t[i+1][0])
if i+2 < len(t):
if t[i+2][1]=='NN' or t[i+2][1]=='NNP' or t[i+2][1]=='NNS' or t[i+2][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+2][0])
l.append(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0])
if i+3 < len(t):
if t[i+3][1]=='NN' or t[i+3][1]=='NNP' or t[i+3][1]=='NNS' or t[i+3][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+3][0])
l.append(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0])
if i>0:
if sentiment_analyzer_scores(t[i-1][0]+' '+t[i][0])>0.05:
l.append(t[i][0])
if i>1:
if sentiment_analyzer_scores(t[i-2][0]+' '+t[i-1][0]+' '+t[i][0])>0.05:
l.append(t[i][0])
return l
def R1_ori(sent):
t=tag(sent)
w=nltk.word_tokenize(sent)
l=[]
just_tags=[]
for i in range(len(t)):
just_tags.append(t[i][1])
#get all words which are pronouns
if t[i][1]=='PRP':
l.append(t[i][0])
if 'WRB' in just_tags or 'WP' in just_tags or 'WDT' in just_tags:
l.append(t[i][0])
if '.' in just_tags and '?' in w[i:]:
l.append(t[i][0])
# below adds the pronoun again to list if it is being praised
if i+1 < len(t):
if t[i+1][1]=='JJ' and TextBlob(t[i][0]+' '+t[i+1][0]).sentiment.polarity>=0:
l.append(t[i][0])
if i+2 < len(t):
if t[i+2][1]=='JJ' and TextBlob(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]).sentiment.polarity>=0:
l.append(t[i][0])
if i+3 < len(t):
if t[i+3][1]=='JJ' and TextBlob(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0]).sentiment.polarity>=0:
l.append(t[i][0])
if i+4 < len(t):
if t[i+4][1]=='JJ' and TextBlob(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0]+' '+t[i+4][0]).sentiment.polarity>=0:
l.append(t[i][0])
if i>0:
if TextBlob(t[i-1][0]+' '+t[i][0]).sentiment.polarity>0:
l.append(t[i][0])
if i>1:
if TextBlob(t[i-2][0]+' '+t[i-1][0]+' '+t[i][0]).sentiment.polarity>0:
l.append(t[i][0])
#possesive pronouns along with their object
if t[i][1]=='PRP$':
if i+1==len(t):
l.append(t[i][0])
if i+1 < len(t):
if t[i+1][1]=='NN' or t[i+1][1]=='NNP' or t[i+1][1]=='NNS' or t[i+1][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+1][0])
l.append(t[i][0]+' '+t[i+1][0])
if i+2 < len(t):
if t[i+2][1]=='NN' or t[i+2][1]=='NNP' or t[i+2][1]=='NNS' or t[i+2][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+2][0])
l.append(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0])
if i+3 < len(t):
if t[i+3][1]=='NN' or t[i+3][1]=='NNP' or t[i+3][1]=='NNS' or t[i+3][1]=='NNPS':
l.append(t[i][0])
l.append(t[i+3][0])
l.append(t[i][0]+' '+t[i+1][0]+' '+t[i+2][0]+' '+t[i+3][0])
if i>0:
if TextBlob(t[i-1][0]+' '+t[i][0]).sentiment.polarity>0:
l.append(t[i][0])
if i>1:
if TextBlob(t[i-2][0]+' '+t[i-1][0]+' '+t[i][0]).sentiment.polarity>0:
l.append(t[i][0])
return l
#named entities
def R2(sent):
l=named_entity.ne(sent)
l=list(set(l))
if 'Wow' in l:
l.remove('Wow')
if 'wow' in l:
l.remove('wow')
return l
#object of sentiment verbs
def R3_ori(sent):
l=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
jt=[x[1] for x in t]
for i in range(len(t)-1):
if t[i][1]=='VB' or t[i][1]=='VBD' or t[i][1]=='VBG' or t[i][1]=='VBZ' or t[i][1]=='VBN' or t[i][1]=='VBP':
#print(0)
if t[i][0] in p:
#print(1)
if ',' in jt[i+1:] and (jt[i+1:].index(',')+i+1)!=i+1:
k=jt[i+1:].index(',')+i+1
o=' '.join(w[i+1:k])
l.append(o)
elif ':' in jt[i+1:] and (jt[i+1:].index(':')+i+1)!=i+1:
k=jt[i+1:].index(':')+i+1
o=' '.join(w[i+1:k])
l.append(o)
elif '.' in jt[i+1:] and (jt[i+1:].index('.')+i+1)!=i+1:
#print(2)
k=jt[i+1:].index('.')+i+1
#print(k)
#print(w[(i+1):k])
o=' '.join(w[i+1:k])
#print(o)
l.append(o)
try:
x=jt[i+1:].index('NN')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNS')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNP')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNPS')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
pass
if t[i][0] in n:
if TextBlob(' '.join(w[i+1:])).sentiment.polarity >=0:
# print(' '.join(w[i+1:]))
l.append('OUTSIDE/LISTENER')
return l
def R3(sent):
l=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
jt=[x[1] for x in t]
for i in range(len(t)-1):
if t[i][1]=='VB' or t[i][1]=='VBD' or t[i][1]=='VBG' or t[i][1]=='VBZ' or t[i][1]=='VBN' or t[i][1]=='VBP':
#print(0)
if t[i][0] in p:
#print(1)
if ',' in jt[i+1:] and (jt[i+1:].index(',')+i+1)!=i+1:
k=jt[i+1:].index(',')+i+1
o=' '.join(w[i+1:k])
l.append(o)
elif ':' in jt[i+1:] and (jt[i+1:].index(':')+i+1)!=i+1:
k=jt[i+1:].index(':')+i+1
o=' '.join(w[i+1:k])
l.append(o)
elif '.' in jt[i+1:] and (jt[i+1:].index('.')+i+1)!=i+1:
#print(2)
k=jt[i+1:].index('.')+i+1
#print(k)
#print(w[(i+1):k])
o=' '.join(w[i+1:k])
#print(o)
l.append(o)
try:
x=jt[i+1:].index('NN')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNS')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNP')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
try:
x=jt[i+1:].index('NNPS')+i+1
o=' '.join(w[i+1:x+1])
l.append(o)
l.append(w[x])
except:
pass
if t[i][0] in n:
if sentiment_analyzer_scores(' '.join(w[i+1:])) >=0.05:
# print(' '.join(w[i+1:]))
l.append('OUTSIDE/LISTENER')
return l
#gerunds and infinitives
def R5(sent):
l=[]
flag=0
t=tag(sent)
w=nltk.word_tokenize(sent)
jt=[x[1] for x in t]
fct=[y[0] for y in jt]
for i in range(len(jt)-1):
if jt[i]=='VBG' and jt[i+1]!='TO' and w[i+1]!='as':
for k in range(i+1,len(jt)):
if (fct[k]=='V' and jt[k]!='VBN') or fct[k]=='W' or jt[k]==',' or jt[k]=='.' or jt[k]==':' or jt[k]=='CC':
l.append(' '.join(w[i:k]))
flag=1
break
if(flag==0):
l.append(' '.join(w[i:]))
flag=0
for e in range(len(t)-1):
if jt[e]=='TO' and fct[e+1]=='V':
if e<len(t)-2:
for k in range(e+2,len(t)):
if (fct[k]=='V' and jt[k]!='VBN') or fct[k]=='W' or jt[k]==',' or jt[k]=='.' or jt[k]==':' or jt[k]=='CC':
l.append(' '.join(w[e:k]))
flag=1
break
if(flag==0):
l.append(' '.join(w[e:]))
else:
l.append(w[e]+' '+w[e+1])
if e>0:
if w[e-1]=='not' or w[e-1]=='Not':
s=w[e-1]+' '+l[-1]
l[-1]=s
return l
#nouns subject to positive adjective
def R6_ori(sent):
l=[]
nn=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
for q in range(len(t)):
if t[q][1]=='NN' or t[q][1]=='NNS' or t[q][1]=='NNP' or t[q][1]=='NNPS':
nn.append(q)
if len(nn)==1:
l.append(w[nn[0]])
return l
if len(nn)>1:
for x in nn:
if x>1:
if TextBlob(' '.join(w[x-2:x])).sentiment.polarity > 0:
l.append(w[x])
elif w[x-1] in p:
l.append(w[x])
elif x==1:
if w[0] in p:
l.append(w[x])
return l
def R6(sent):
l=[]
nn=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
for q in range(len(t)):
if t[q][1]=='NN' or t[q][1]=='NNS' or t[q][1]=='NNP' or t[q][1]=='NNPS':
nn.append(q)
if len(nn)==1:
l.append(w[nn[0]])
return l
if len(nn)>1:
for x in nn:
if x>1:
if sentiment_analyzer_scores(' '.join(w[x-2:x])) > 0:
l.append(w[x])
elif w[x-1] in p:
l.append(w[x])
elif x==1:
if w[0] in p:
l.append(w[x])
return l
#subject of interrogative sentence
def R7(sent):
l=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
jt=[x[1] for x in t]
for i in range(len(jt)):
if jt[i]=='WDT' or jt[i]=='WP'or jt[i]=='WRB' or w[i]=='Could' or w[i]=='Would':
if i+1<len(jt):
try:
x=jt[i+1:].index('NN')+i+1
l.append(w[x])
l.extend(R1(' '.join(w[i+1:])))
except:
try:
x=jt[i+1:].index('NNS')+i+1
l.append(w[x])
l.extend(R1(' '.join(w[i+1:])))
except:
if i>1:
l.extend(R6(' '.join(w[:(i-1)])))
if 'this' in w[i+1:]:
l.append('this')
if 'that' in w[i+1:]:
l.append('that')
return l
#demonstrative adjective + following noun
def R9(sent):
l=[]
t=tag(sent)
jt=[x[1] for x in t]
w=nltk.word_tokenize(sent)
for i in range(len(w)):
if w[i]=='this' or w[i]=='that' or w[i]=='these' or w[i]=='those' or w[i]=='This' or w[i]=='That' or w[i]=='These' or w[i]=='Those':
if i==len(w)-1:
l.append(w[i])
else:
try:
x=jt[i+1:].index('NN')+i+1
if x==i+1:
l.append(w[i]+' '+w[x])
l.append(w[x])
elif x==i+2:
if jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS':
l.append(w[i]+' '+w[i+1]+' '+w[i+2])
l.append(w[i+2])
elif x==i+3:
if (jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS') and (jt[i+2]=='JJ' or jt[i+2]=='JJR' or jt[i+2]=='JJS' or jt[i+2]=='RB' or jt[i+2]=='RBR' or jt[i+2]=='RBS'):
l.append(w[i]+' '+w[i+1]+' '+w[i+2]+' '+w[i+3])
l.append(w[i+3])
except:
try:
x=jt[i+1:].index('NNP')+i+1
if x==i+1:
l.append(w[i]+' '+w[x])
l.append(w[x])
elif x==i+2:
if jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS':
l.append(w[i]+' '+w[i+1]+' '+w[i+2])
l.append(w[i+2])
elif x==i+3:
if (jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS') and (jt[i+2]=='JJ' or jt[i+2]=='JJR' or jt[i+2]=='JJS' or jt[i+2]=='RB' or jt[i+2]=='RBR' or jt[i+2]=='RBS'):
l.append(w[i]+' '+w[i+1]+' '+w[i+2]+' '+w[i+3])
l.append(w[i+3])
except:
try:
x=jt[i+1:].index('NNS')+i+1
if x==i+1:
l.append(w[i]+' '+w[x])
l.append(w[x])
elif x==i+2:
if jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS':
l.append(w[i]+' '+w[i+1]+' '+w[i+2])
l.append(w[i+2])
elif x==i+3:
if (jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS') and (jt[i+2]=='JJ' or jt[i+2]=='JJR' or jt[i+2]=='JJS' or jt[i+2]=='RB' or jt[i+2]=='RBR' or jt[i+2]=='RBS'):
l.append(w[i]+' '+w[i+1]+' '+w[i+2]+' '+w[i+3])
l.append(w[i+3])
except:
try:
x=jt[i+1:].index('NNPS')+i+1
if x==i+1:
l.append(w[i]+' '+w[x])
l.append(w[x])
elif x==i+2:
if jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS':
l.append(w[i]+' '+w[i+1]+' '+w[i+2])
l.append(w[i+2])
elif x==i+3:
if (jt[i+1]=='JJ' or jt[i+1]=='JJR' or jt[i+1]=='JJS' or jt[i+1]=='RB' or jt[i+1]=='RBR' or jt[i+1]=='RBS') and (jt[i+2]=='JJ' or jt[i+2]=='JJR' or jt[i+2]=='JJS' or jt[i+2]=='RB' or jt[i+2]=='RBR' or jt[i+2]=='RBS'):
l.append(w[i]+' '+w[i+1]+' '+w[i+2]+' '+w[i+3])
l.append(w[i+3])
except:
pass
return l
#subjects involved in comparison
def R8(sent):
l=[]
t=tag(sent)
jt=[x[1] for x in t]
w=nltk.word_tokenize(sent)
for i in range(len(w)-1):
if w[i]=='as' or w[i]=='As':
if w[i+1]=='if' and i<len(w)-2:
l.extend(R1(' '.join(w[i+2:])))
l.extend(R3(' '.join(w[i+2:])))
l.extend(R5(' '.join(w[i+2:])))
l.extend(R6(' '.join(w[i+2:])))
l.extend(R7(' '.join(w[i+2:])))
l.extend(R9(' '.join(w[i+2:])))
else:
try:
x=w[i+1:].index('as')+i+1
#print(x)
l.extend(R1(' '.join(w[x+1:])))
l.extend(R3(' '.join(w[x+1:])))
l.extend(R5(' '.join(w[x+1:])))
l.extend(R6(' '.join(w[x+1:])))
l.extend(R2(' '.join(w[x+1:])))
l.extend(R9(' '.join(w[x+1:])))
if i>0:
l.extend(R1(' '.join(w[:i])))
l.extend(R3(' '.join(w[:i])))
l.extend(R5(' '.join(w[:i])))
l.extend(R6(' '.join(w[:i])))
l.extend(R2(' '.join(w[:i])))
l.extend(R9(' '.join(w[:i])))
except:
pass
l=list(set(l))
return l
# Just changing it , since we are calling a better sentiment analysis anyway - this is not optimized again
def R4_on_single_sent(s):
r=[]
l= [parse.tree() for parse in dep_parser.raw_parse(s)]
root=l[0].label()
#print(root)
t=tag(s)
w=nltk.word_tokenize(s)
try:
x=w.index(root)
jt=[z[1] for z in t]
fct=[y[0] for y in jt]
if x>0 and x<len(w)-1:
if fct[x]=='V' and w[x] not in p:
s1=' '.join(w[:x])
s2=' '.join(w[x+1:])
# print(s1)
# print(s2)
#sentiment_analyzer_scores
#p1=abs(TextBlob(s1).sentiment.polarity) #old implementation
#p2=abs(TextBlob(s2).sentiment.polarity) #old implementation
p1=abs(sentiment_analyzer_scores(s1))
p2=abs(sentiment_analyzer_scores(s2))
if p1<p2:
r.append(s1)
r.extend(R6(s1))
if p1>p2:
r.append(s2)
r.extend(R6(s2))
except:
pass
return r
#uses above implementation for single sentence to implement rule 4 - lower sentimental side of verb - to text
def R4_ori(sent):
l=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
if(len(w))<3:
return l
jt=[z[1] for z in t]
try:
x=jt.index('.')
#print(x)
c1=len(w[:x])
c2=len(w[x+1:])
#print(c2)
if c1>2 and c2>2:
s1=' '.join(w[:x+1])
s2=' '.join(w[x+1:])
p1=abs(TextBlob(s1).sentiment.polarity)
p2=abs(TextBlob(s2).sentiment.polarity)
if p1<p2:
l.append(s1)
l.extend(R6(s1))
if p1>p2:
l.append(s2)
l.extend(R6(s2))
elif c1>2:
s1=' '.join(w[:x+1])
#print(s1)
l.extend(R4_on_single_sent(s1))
elif c2>2:
s2=' '.join(w[x+1:])
l.extend(R4_on_single_sent(s2))
except:
try:
x=jt.index(':')
c1=len(w[:x])
c2=len(w[x+1:])
s1=' '.join(w[:x+1])
s2=' '.join(w[x+1:])
if c1>1 and c2>1:
p1=abs(TextBlob(s1).sentiment.polarity)
p2=abs(TextBlob(s2).sentiment.polarity)
if p1<p2:
l.append(s1)
l.extend(R6(s1))
if p1>p2:
l.append(s2)
l.extend(R6(s2))
elif c1>1:
l.extend(R4_on_single_sent(s1))
elif c2>1:
l.extend(R4_on_single_sent(s2))
except:
pass
return l
def R4(sent):
l=[]
t=tag(sent)
w=nltk.word_tokenize(sent)
if(len(w))<3:
return l
jt=[z[1] for z in t]
try:
x=jt.index('.')
#print(x)
c1=len(w[:x])
c2=len(w[x+1:])
#print(c2)
if c1>2 and c2>2:
s1=' '.join(w[:x+1])
s2=' '.join(w[x+1:])
p1=abs(sentiment_analyzer_scores(s1))
p2=abs(sentiment_analyzer_scores(s2))
if p1<p2:
l.append(s1)
l.extend(R6(s1))
if p1>p2:
l.append(s2)
l.extend(R6(s2))
elif c1>2:
s1=' '.join(w[:x+1])
#print(s1)
l.extend(R4_on_single_sent(s1))
elif c2>2:
s2=' '.join(w[x+1:])
l.extend(R4_on_single_sent(s2))
except:
try:
x=jt.index(':')
c1=len(w[:x])
c2=len(w[x+1:])
s1=' '.join(w[:x+1])
s2=' '.join(w[x+1:])
if c1>1 and c2>1:
p1=abs(sentiment_analyzer_scores(s1))
p2=abs(sentiment_analyzer_scores(s2))
if p1<p2:
l.append(s1)
l.extend(R6(s1))
if p1>p2:
l.append(s2)
l.extend(R6(s2))
elif c1>1:
l.extend(R4_on_single_sent(s1))
elif c2>1:
l.extend(R4_on_single_sent(s2))
except:
pass
return l
def R10(sent):
return(re.findall('"([^"]*)"', sent))
def capitalizeWords(s):
return re.sub(r'\w+', lambda m:m.group(0).capitalize(), s)
def runThemAll(sent):
wolo = []
wolo2 = []
outside = "OUTSIDE/LISTENER" # look for outside things
wolo.extend(R1(sent))
wolo.extend(R2(sent))
wolo.extend(R3(sent))
wolo.extend(R4(sent))
wolo.extend(R5(sent))
wolo.extend(R6(sent))
wolo.extend(R7(sent))
wolo.extend(R8(sent))
wolo.extend(R9(sent))
wolo.extend(R10(sent))
wolo = list(set(wolo))
#We have an outside case for this. This is not likely
if any(outside in s for s in wolo):
return "OUTSIDE"
# Else , we will convert them everything first and then remove the bloody duplicates
wolo = list(map(lambda x:x.lower(),wolo))
#test1 = nltk.word_tokenize(wolo)
for x in wolo:
if re.match("^[a-zA-Z0-9_]*$", x):
wolo2.extend(x.split())
return(list(set(wolo2)))
# for i in wolo:
# print(i)
#print(R4_on_single_sent('So happy to just find out it has been decided to reschedule all my lectures and tutorials for me to night classes at the exact same times!'))
#print(R4("Got rejected in a job interview today. I am the happiest person alive!"))
# f=open('sam2.txt','r')
# for line in f:
# print('R1 ',R1(line))
# print('R2 ',R2(line))
# print('R3 ',R3(line))
# print('R4 ',R4(line))
# print('R5 ',R5(line))
# print('R6 ',R6(line))
# print('R7 ',R7(line))
# print('R8 ',R8(line))
# print('R9 ',R9(line))
# print('\n')
#print(sentiment_analyzer_scores("I love being ignored"))
#print(TextBlob("I love being ignored").sentiment.polarity)
print(runThemAll("I love being ignored"))
#print(R1("He is as good at coding as Tiger Woods is at contro."))