-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsentiment analysis based on opinion target and opinion word mining.py
1275 lines (1014 loc) · 47.2 KB
/
sentiment analysis based on opinion target and opinion word mining.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pip install -U textblob
# python -m textblob.download_corpora
import json
from collections import defaultdict
from textblob import TextBlob
import re
#############################################################################
# Step1 : data preprocessing
def decontracted(phrase):
# specific
phrase = re.sub(r"won't", "will not", phrase)
phrase = re.sub(r"can\'t", "can not", phrase)
# general
phrase = re.sub(r"n\'t", " not", phrase)
phrase = re.sub(r"\'re", " are", phrase)
phrase = re.sub(r"\'s", " is", phrase)
phrase = re.sub(r"\'d", " would", phrase)
phrase = re.sub(r"\'ll", " will", phrase)
phrase = re.sub(r"\'t", " not", phrase)
phrase = re.sub(r"\'ve", " have", phrase)
phrase = re.sub(r"\'m", " am", phrase)
phrase = re.sub(r" v", " very", phrase)
return phrase
reviewerID =[]
productID = []
reviewerName=[]
liked_and_seen = []
reviewText = []
rating = []
summary = []
unixTime = []
date = []
with open('Cell_Phones_and_Accessories_5.json') as json_data:
d = json.load(json_data)
d = d[0:300]
for i in range(len(d)):
reviewText.append(decontracted(d[i]['reviewText']))
rating.append(d[i]['overall'])
reviewerID.append(d[i]['reviewerID'])
productID.append(d[i]['asin'])
# reviewerName.append(d[i]['reviewerName'])
liked_and_seen.append(d[i]['helpful'])
summary.append(d[i]['summary'])
unixTime.append(d[i]['unixReviewTime'])
date.append(d[i]['reviewTime'])
# create dataset
from pandas import DataFrame
dataset = DataFrame({'reviewerID': reviewerID, 'productID': productID, 'liked_and_seen': liked_and_seen, 'reviewText': reviewText, 'rating': rating, 'summary': summary, 'unixTime': unixTime, 'date': date})
#cleaning unwanted symbols
#cleaning unwanted symbols
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
comment_dict = defaultdict(list)
for i in range(len(dataset)):
sentence = re.sub('[^a-zA-Z.]',' ',dataset['reviewText'][i])
sentence = sentence.lower()
sentence = sentence.split('.')
for k in range(len(sentence)):
review = sentence[k].split()
review = [word for word in review if not word in set(stopwords.words('english'))]
sentence[k] = ' '.join(review)
comment_dict[i].append(sentence[k])
#delete unwanted '' words
for j in range(len(comment_dict)):
comment_dict[j] = [comment_dict[j][i] for i in range(len(comment_dict[j])) if comment_dict[j][i] not in '']
for i in range(len(comment_dict)):
reviewText[i] = ('. '.join(comment_dict[i][j] for j in range(len(comment_dict[i]))))
# spelling correction
for i in range(len(reviewText)):
b = TextBlob(reviewText[i])
reviewText[i] = b.correct()
dataset_corrected= DataFrame({'reviewerID': reviewerID, 'productID': productID, 'liked_and_seen': liked_and_seen, 'reviewText': reviewText, 'rating': rating, 'summary': summary, 'unixTime': unixTime, 'date': date})
# creating corpus
corpus = defaultdict(set)
for i in range(len(reviewText)):
wiki = reviewText[i]
corpus[i] = wiki.sentences
corpus_key = corpus.keys()
corpus_list = defaultdict(list)
for i in corpus_key:
for j in range(len(corpus[i])):
word = ' '.join(corpus[i][j].words)
corpus_list[i].append(word)
####################################################################################
# Step 2: adding biwords and triwords for generating patterns
length = defaultdict(list)
for i in corpus_key:
length[i] = list(corpus_list[i])
# triwords
for i in corpus_key:
for j in range(len(length[i])):
text = TextBlob(length[i][j])
text = text.ngrams(n=3)
for k in range(len(text)):
triword = [' '.join([text[k][l] for l in range(len(text[k]))])]
triword = triword[0]
corpus_list[i].append(triword)
#biwords
for i in corpus_key:
for j in range(len(length[i])):
text = TextBlob(length[i][j])
text = text.ngrams(n=2)
for k in range(len(text)):
triword = [' '.join([text[k][l] for l in range(len(text[k]))])]
triword = triword[0]
corpus_list[i].append(triword)
# corpus_list creates a list of all Opinion Words present in review text
# here the corpus contains sentences, biwords and triwords from review
# Also here only matched text is used
####################################################################################
# Step 3: Part-of-speech Tagging
pos_dict = defaultdict(list)
for i in corpus_key:
for j in range(len(corpus_list[i])):
text = TextBlob(corpus_list[i][j])
text = text.tags
pos_dict[i].append(text)
pos_dict_key = pos_dict.keys()
corpus_noun = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
for k in range(len(pos_dict[i][j])):
if(pos_dict[i][j][k][1] == 'NN'):
corpus_noun[i].append(pos_dict[i][j][k])
##########################################################################################
# Step 4: pattern generation from Part -of -speech tagging
pattern1 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 2):
if((pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NN') or (pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NNS')):
#pattern1
pattern1[i].append(pos_dict[i][j])
pattern2 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 3):
if((pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NN'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NN'and pos_dict[i][j][2][1] == 'NNS')):
#pattern2
pattern2[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NNS'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'JJ' and pos_dict[i][j][1][1] == 'NNS'and pos_dict[i][j][2][1] == 'NNS')):
#pattern2
pattern2[i].append(pos_dict[i][j])
pattern3 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 2):
if((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'JJ')):
#pattern3
pattern3[i].append(pos_dict[i][j])
pattern4 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 3):
if((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'JJ'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NN') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][2][1] == 'NNS')):
#pattern4
pattern4[i].append(pos_dict[i][j])
pattern5 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 2):
if((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'VBN') or (pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'VBN') or (pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'VBN')):
#pattern5
pattern5[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'VBD') or (pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'VBD') or (pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'VBD')):
#pattern5
pattern5[i].append(pos_dict[i][j])
pattern6 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 3):
if((pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RB' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][0][1] == 'JJ')):
#pattern6
pattern6[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBR' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][0][1] == 'JJ')):
#pattern6
pattern6[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RB'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RBR'and pos_dict[i][j][2][1] == 'JJ') or (pos_dict[i][j][0][1] == 'RBS' and pos_dict[i][j][1][1] == 'RBS'and pos_dict[i][j][0][1] == 'JJ')):
#pattern6
pattern6[i].append(pos_dict[i][j])
pattern7 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 2):
if((pos_dict[i][j][0][1] == 'VBN' and pos_dict[i][j][1][1] == 'NN') or (pos_dict[i][j][0][1] == 'VBD' and pos_dict[i][j][1][1] == 'NN')):
#pattern7
pattern7[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'VBN' and pos_dict[i][j][1][1] == 'NNS') or (pos_dict[i][j][0][1] == 'VBD' and pos_dict[i][j][1][1] == 'NNS')):
#pattern7
pattern7[i].append(pos_dict[i][j])
pattern8 = defaultdict(list)
for i in pos_dict_key:
for j in range(len(pos_dict[i])):
if(len(pos_dict[i][j]) == 2):
if((pos_dict[i][j][0][1] == 'VBN' and pos_dict[i][j][1][1] == 'RB') or (pos_dict[i][j][0][1] == 'VBD' and pos_dict[i][j][1][1] == 'RB')):
#pattern8
pattern8[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'VBN' and pos_dict[i][j][1][1] == 'RBR') or (pos_dict[i][j][0][1] == 'VBD' and pos_dict[i][j][1][1] == 'RBR')):
#pattern8
pattern8[i].append(pos_dict[i][j])
elif((pos_dict[i][j][0][1] == 'VBN' and pos_dict[i][j][1][1] == 'RBS') or (pos_dict[i][j][0][1] == 'VBD' and pos_dict[i][j][1][1] == 'RBS')):
#pattern8
pattern8[i].append(pos_dict[i][j])
pattern = defaultdict(set)
pattern.update(pattern1)
pattern.update(pattern2)
pattern.update(pattern3)
pattern.update(pattern4)
pattern.update(pattern5)
pattern.update(pattern6)
pattern.update(pattern7)
pattern.update(pattern8)
#select stuff from OT_OW_key
####################################################################################
# Step 5: Semi- supervised approach creates Opinion Target from stuff.
stuff = ['software', 'application', 'service', 'power supply', 'sim card', 'display',
'storage space', 'sensor', 'wireless charging', 'design', 'cpu', 'accessories',
'camera','quality','time','condition','screen','price','case','build','access',
'battery','buy','power','switch','light','design','technology','radio','fashion'
'product','charging','feature','touch','profile','car','slot','tables','construction',
'period ','system','game','bottom','sound','blackberry charge','price anyone','price extra',
'cord length','charge port',' phone','horizon charge','fraction price','charge ','key',
'extension','internet','cheap','cover','speaker']
####################################################################################
# Step 6: Finding Similar waords of above Opinion Targets
from nltk.corpus import wordnet as wn
from itertools import chain
stuff_OT = defaultdict(set)
OT2 = set()
synsets_set = defaultdict(set)
hyponyms_set = defaultdict(set)
for z in range(len(stuff)):
input_word = stuff[z]
OT1= set()
for i,j in enumerate(wn.synsets(input_word)):
# print ('Meaning', i, 'NLTK ID: ', j.name())
hypernyms = ', '.join(list(chain(*[l.lemma_names() for l in j.hypernyms()])))
# print ('Hypernyms:', hypernyms)
synsets_set[i].add(hypernyms)
hyponyms = ', '.join(list(chain(*[l.lemma_names() for l in j.hyponyms()])))
# print ('Hyponyms:', hyponyms)
hyponyms_set[i].add(hyponyms)
# print()
ho = [hypernyms]
for h in range(len(ho)):
temp_list = ho[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OT2.add(temp_word)
OT1.add(temp_word)
hy = [hypernyms]
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OT2.add(temp_word)
OT1.add(temp_word)
OT1 = list(OT1)
for i in range(len(OT1)):
stuff_OT[stuff[z]].add(OT1[i])
# stuff contains Opinion Target and its similar words from wordnet
####################################################################################
hyponyms_set_keys = hyponyms_set.keys()
synsets_set_keys = synsets_set.keys()
for z in range(len(stuff)):
for k in hyponyms_set_keys:
hy = hyponyms_set[k]
hy = list(hy)
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OT2.add(temp_word)
for z in range(len(stuff)):
for k in synsets_set_keys:
hy = synsets_set[k]
hy = list(hy)
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OT2.add(temp_word)
OT2 = list(OT2)
list_of_subset = defaultdict(set)
for i in range(len(stuff)):
stuff_OT[stuff[i]] = list(stuff_OT[stuff[i]])
for j in range(len(stuff_OT[stuff[i]])):
list_of_subset[i].add(stuff[i])
list_of_subset[i].add(stuff_OT[stuff[i]][j])
for i in list_of_subset.keys():
list_of_subset[i] = list(list_of_subset[i])
list_of_subset2 =[]
for i in range(len(stuff)):
list_of_subset2.append(stuff[i])
for i in list_of_subset.keys():
for j in range(len(list_of_subset[i])):
list_of_subset2.append(list_of_subset[i][j])
stuff = list_of_subset2
#######################################################################################
# Step 7: Finding Opinion Words of above Opinion Target(Stuff + its similar words.)
# from pattern generated in step 3.
OW = defaultdict(set)
OT = defaultdict(set)
OT_OW = defaultdict(set)
OW_OT = defaultdict(set)
# it has 1 OW
pattern1_OT_OW = defaultdict(set)
pattern1_OW_OT = defaultdict(set)
p1_keys = pattern1.keys()
for i in p1_keys:
if( pattern1[i] != []):
for j in range(len(pattern1[i])):
OT[i].add(pattern1[i][j][1][0])
OW[i].add(pattern1[i][j][0][0])
if(pattern1[i][j][1][0] in stuff):
OT_OW[pattern1[i][j][1][0]].add(pattern1[i][j][0][0])
OW_OT[pattern1[i][j][0][0]].add(pattern1[i][j][1][0])
pattern1_OT_OW[pattern1[i][j][1][0]].add(pattern1[i][j][0][0])
pattern1_OW_OT[pattern1[i][j][0][0]].add(pattern1[i][j][1][0])
#it has 1 OW
pattern2_OT_OW = defaultdict(set)
pattern2_OW_OT = defaultdict(set)
p2_keys = pattern2.keys()
for i in p2_keys:
if( pattern2[i] != []):
for j in range(len(pattern2[i])):
target = pattern2[i][j][1][0] + " " + pattern2[i][j][2][0]
OT[i].add(target)
OW[i].add(pattern2[i][j][0][0])
if(pattern2[i][j][1][0] in stuff or pattern2[i][j][2][0] in stuff or target in stuff):
OT_OW[target].add(pattern2[i][j][0][0])
OW_OT[pattern2[i][j][0][0]].add(target)
pattern2_OT_OW[target].add(pattern2[i][j][0][0])
pattern2_OW_OT[pattern2[i][j][0][0]].add(target)
# dont filter here
# it has 2 OW pretty, good we use only pretty good combination
pattern3_OW_OT = defaultdict(set)
p3_keys = pattern3.keys()
for i in p3_keys:
if( pattern3[i] != []):
for j in range(len(pattern3[i])):
target = pattern3[i][j][0][0] + " " + pattern3[i][j][1][0]
OW[i].add(target)
OW_OT[target].add('NO Opinion Target found')
pattern3_OW_OT[target].add('NO Opinion Target found')
# we use near much, near, much word combinations in OW here
pattern4_OT_OW = defaultdict(set)
pattern4_OW_OT = defaultdict(set)
p4_keys = pattern4.keys()
for i in p4_keys:
if( pattern4[i] != []):
for j in range(len(pattern4[i])):
word = pattern4[i][j][0][0] + " " + pattern4[i][j][1][0]
OT[i].add(pattern4[i][j][2][0])
OW[i].add(word)
if(pattern4[i][j][2][0] in stuff):
OT_OW[pattern4[i][j][2][0]].add(word)
OW_OT[word].add(pattern4[i][j][2][0])
pattern4_OT_OW[pattern4[i][j][2][0]].add(word)
pattern4_OW_OT[word].add(pattern4[i][j][2][0])
# dont filter here
pattern5_OW_OT = defaultdict(set)
p5_keys = pattern5.keys()
for i in p5_keys:
if( pattern5[i] != []):
for j in range(len(pattern5[i])):
target = pattern5[i][j][0][0] + ' ' + pattern5[i][j][1][0]
OW[i].add(target)
OW_OT[target].add('No Opinion Target found')
pattern5_OW_OT[target].add('No Opinion Target found')
# dont filter here
pattern6_OW_OT = defaultdict(set)
p6_keys = pattern6.keys()
for i in p6_keys:
if( pattern6[i] != []):
for j in range(len(pattern6[i])):
target = pattern6[i][j][0][0] + " " + pattern6[i][j][2][0]
OW[i].add(target)
OW_OT[target].add('NO Opinion Target found')
pattern6_OW_OT[target].add('NO Opinion Target found')
pattern7_OW_OT = defaultdict(set)
pattern7_OT_OW = defaultdict(set)
p7_keys = pattern7.keys()
for i in p7_keys:
if( pattern7[i] != []):
for j in range(len(pattern7[i])):
target = pattern7[i][j][1][0]
OT[i].add(target)
OW[i].add(pattern7[i][j][0][0])
if(target in stuff):
OW_OT[pattern7[i][j][0][0]].add(target)
OT_OW[target].add(pattern7[i][j][0][0])
pattern7_OW_OT[pattern7[i][j][0][0]].add(target)
pattern7_OT_OW[target].add(pattern7[i][j][0][0])
# dont filter here
pattern_8_OW_OT = defaultdict(set)
p8_keys = pattern8.keys()
for i in p8_keys:
if( pattern8[i] != []):
for j in range(len(pattern8[i])):
target = pattern8[i][j][1][0]
OW[i].add(target)
OW_OT[target].add('No Opinion Target found')
pattern_8_OW_OT[target].add('No Opinion Target found')
################################################################################
# Step 8: Finding similar Words of above Opinion Words.
OW_OT_key = OW_OT.keys()
OW_list = list(OW_OT_key)
from nltk.corpus import wordnet as wn
from itertools import chain
stuff_OW = defaultdict(set)
OW2 = set()
synsets_set = defaultdict(set)
hyponyms_set = defaultdict(set)
for z in range(len(OW_list)):
input_word = OW_list[z]
OW1= set()
for i,j in enumerate(wn.synsets(input_word)):
# print ('Meaning', i, 'NLTK ID: ', j.name())
hypernyms = ', '.join(list(chain(*[l.lemma_names() for l in j.hypernyms()])))
# print ('Hypernyms:', hypernyms)
synsets_set[i].add(hypernyms)
hyponyms = ', '.join(list(chain(*[l.lemma_names() for l in j.hyponyms()])))
# print ('Hyponyms:', hyponyms)
hyponyms_set[i].add(hyponyms)
# print()
ho = [hypernyms]
for h in range(len(ho)):
temp_list = ho[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OW2.add(temp_word)
OW1.add(temp_word)
hy = [hypernyms]
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OW2.add(temp_word)
OW1.add(temp_word)
OW1 = list(OW1)
for i in range(len(OW1)):
stuff_OW[OW_list[z]].add(OW1[i])
hyponyms_set_keys = hyponyms_set.keys()
synsets_set_keys = synsets_set.keys()
for z in range(len(OW_list)):
for k in hyponyms_set_keys:
hy = hyponyms_set[k]
hy = list(hy)
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OW2.add(temp_word)
for z in range(len(OW_list)):
for k in synsets_set_keys:
hy = synsets_set[k]
hy = list(hy)
for h in range(len(hy)):
temp_list = hy[h].split(', ')
if(temp_list != ['']):
for l in range(len(temp_list)):
temp_word = ' '.join(temp_list[l].split('_'))
OW2.add(temp_word)
OW_concept = []
OW2 = list(OW2)
for i in range(len(OW2)):
if(OW2[i] != ''):
OW_concept.append(OW2[i])
for i in range(len(OW_list)):
OW_concept.append(OW_list[i])
# Here OW_concept creates a list of all similar Opinion words of Opinion words generated from pattern
#####################################################################################
# Step 9: Finding Opinion Weight of above similar Opinion Words ( from testimonial.sentiment.polarity )
OW_OT = OW_concept
OW_OT_key = OW_OT
OT_OW_key = OT_OW.keys()
OW_in_corpus = defaultdict(set)
for i in corpus.keys():
for j in range(len(corpus[i])):
word = corpus[i][j].words
for k in range(len(word)):
if( word[k] in OW_OT_key):
OW_in_corpus[i].add(word[k])
for i in OW_in_corpus.keys():
OW_in_corpus[i] = list(OW_in_corpus[i])
testimonial_sentiment = defaultdict(list)
testimonial_sentiment_polarity = defaultdict(list)
for i in OW_in_corpus.keys():
for j in range(len(OW_in_corpus[i])):
testimonial = TextBlob(OW_in_corpus[i][j])
testimonial_sentiment[OW_in_corpus[i][j]].append(testimonial.sentiment)
testimonial_sentiment_polarity[OW_in_corpus[i][j]].append(testimonial.sentiment.polarity)
OW_in_corpus_list = defaultdict(list)
OW_in_corpus_value = defaultdict(list)
for i in OW_in_corpus.keys():
for j in range(len(OW_in_corpus[i])):
word = [OW_in_corpus[i][j]]
# for k in range(len(word)):
if(word[0] in testimonial_sentiment_polarity.keys()):
polarity = testimonial_sentiment_polarity[word[0]]
OW_in_corpus_value[i].append(polarity[0])
OW_in_corpus_list[i].append(word[0])
dictionary1 = dict()
dictionary2 = dict()
key_value_pair = defaultdict(list)
for i in range(len(OW_in_corpus_list)):
for j in range(len(OW_in_corpus_list[i])):
# dictionary1[OW_in_corpus_value[i][j]] = OW_in_corpus_list[i][j]
dictionary2[ OW_in_corpus_list[i][j]] = OW_in_corpus_value[i][j]
################################################################################################
# Step 10: Creating Words and its score in tuple_word_score
score = defaultdict(list)
words = OW_in_corpus_list
OW_in_corpus_value_key = OW_in_corpus_value.keys()
for i in OW_in_corpus_value_key:
for j in range(len(OW_in_corpus_value[i])):
score[i].append(OW_in_corpus_value[i][j])
tuple_word_score = defaultdict(list)
for i in OW_in_corpus_value_key:
for j in range(len(OW_in_corpus_value[i])):
tuple_word_score[i].append((words[i][j], score[i][j]))
Opinion_Words = tuple_word_score
list_Opinion_Words = []
for i in range(len(Opinion_Words)):
list_Opinion_Words.append(Opinion_Words[i])
################################################################################################
# Step 11: Finding Maximum scored words.
max_abs_score = defaultdict(list)
for i in range(len(tuple_word_score)):
maxx = 0
for j in range(len(tuple_word_score[i])):
temp = abs(tuple_word_score[i][j][1])
if(temp > maxx):
maxx = abs(tuple_word_score[i][j][1])
maxx_word = tuple_word_score[i][j][0]
max_abs_score[i].append((maxx_word, maxx))
##################################################
maxx_OW_value = defaultdict(set)
for i in range(len(max_abs_score)):
maxx_OW_value[max_abs_score[i][0][0]].add(max_abs_score[i][0][1])
New_OW = maxx_OW_value.keys()
################################################
# step11: Collecting important opinion words.
list_max_abs_score = []
for i in range(len(max_abs_score)):
list_max_abs_score.append(max_abs_score[i])
##########Searching######################################
# step12: Searching it again
OW_in_corpus2 = defaultdict(set)
for i in range(len(corpus)):
for j in range(len(corpus[i])):
word = corpus[i][j].words
for k in range(len(word)):
if( word[k] in New_OW):
OW_in_corpus2[i].add(word[k])
for i in OW_in_corpus2.keys():
OW_in_corpus2[i] = list(OW_in_corpus2[i])
testimonial_sentiment = defaultdict(list)
testimonial_sentiment_polarity = defaultdict(list)
for i in OW_in_corpus2.keys():
for j in range(len(OW_in_corpus2[i])):
testimonial = TextBlob(OW_in_corpus2[i][j])
testimonial_sentiment[OW_in_corpus2[i][j]].append(testimonial.sentiment)
testimonial_sentiment_polarity[OW_in_corpus2[i][j]].append(testimonial.sentiment.polarity)
OW_in_corpus_list = defaultdict(list)
OW_in_corpus_value = defaultdict(list)
for i in OW_in_corpus2.keys():
for j in range(len(OW_in_corpus2[i])):
word = [OW_in_corpus2[i][j]]
# for k in range(len(word)):
if(word[0] in testimonial_sentiment_polarity.keys()):
polarity = testimonial_sentiment_polarity[word[0]]
OW_in_corpus_value[i].append(polarity[0])
OW_in_corpus_list[i].append(word[0])
dictionary1 = dict()
dictionary2 = dict()
key_value_pair = defaultdict(list)
for i in range(len(OW_in_corpus_list)):
for j in range(len(OW_in_corpus_list[i])):
# dictionary1[OW_in_corpus_value[i][j]] = OW_in_corpus_list[i][j]
dictionary2[ OW_in_corpus_list[i][j]] = OW_in_corpus_value[i][j]
# OW_in_corpus_value indicates the polarity score of opinion word
###############################################################################
import numpy as np
score = defaultdict(list)
words = OW_in_corpus_list
OW_in_corpus_value_key = OW_in_corpus_value.keys()
for i in OW_in_corpus_value_key:
for j in range(len(OW_in_corpus_value[i])):
score[i].append(OW_in_corpus_value[i][j])
tuple_word_score = defaultdict(list)
for i in OW_in_corpus_value_key:
for j in range(len(OW_in_corpus_value[i])):
tuple_word_score[i].append((words[i][j], score[i][j]))
##########################################################################
final_score = defaultdict(list)
for i in range(len(tuple_word_score)):
final_score[i].append(0)
for i in range(len(tuple_word_score)):
for j in range(len(tuple_word_score[i])):
final_score[i].append(tuple_word_score[i][j][1])
final_score = defaultdict(list)
for i in range(len(tuple_word_score)):
final_score[i].append(0)
for i in range(len(tuple_word_score)):
for j in range(len(tuple_word_score[i])):
final_score[i].append(tuple_word_score[i][j][1])
############################################################################################
#Step 13: Creating Average
average_score = defaultdict(list)
for i in range(len(final_score)):
average_score[i].append(np.mean(final_score[i]))
for i in range(len(average_score)):
if(np.isnan(average_score[i]) == True):
average_score[i] = [0]
list_average_score = []
for i in range(len(average_score)):
list_average_score.append(average_score[i])
############################################################################################
import numpy as np
X_train = []
for i in average_score.keys():
temp = set()
for j in range(len(OW_in_corpus[i])):
temp.add(OW_in_corpus[i][j])
X_train.append(temp)
l = set()
for i in range(len(X_train)):
X_train[i] = list (X_train[i])
for j in range(len(X_train[i])):
l.add(X_train[i][j])
l =list(l)
import pandas as pd
da = pd.DataFrame(columns = l, index = OW_in_corpus.keys(), data = 0)
k = list(OW_in_corpus.keys())
for m in range(len(k)):
for j in range(len(l)):
if(l[j] in OW_in_corpus[k[m]]):
da.iloc[m][l[j]] = 1
X_train = da.iloc[:, 0:]
X_train = np.array(X_train)
y_train = []
for i in average_score.keys():
y_train.append(average_score[i][0])
r = (max(y_train) - min(y_train)) / 4
m = min(y_train)
y_train = np.array(y_train)
y = []
for i in range(len(y_train)):
y.append(y_train[i])
X = X_train
for i in range(len(y)):
if(y[i] <= m):
y[i] = 'Negative'
elif(y[i] <= m+r and y[i] > m):
y[i] = 'Negative'
elif(y[i] <= m+r+r and y[i] > m+r):
y[i] = 'Negative'
elif(y[i] <= m+r+r+r and y[i] > m+r+r):
y[i] = 'Positive'
elif(y[i] <= m+r+r+r+r and y[i] > m+r+r+r):
y[i] = 'Positive'
y_set = defaultdict(list)
key = average_score.keys()
key = list(key)
for i in range(len(key)):
y_set[key[i]].append(y[i])
da['calculated rating'] = [y_set[i] for i in y_set.keys()]
rev_rate = []
rate = dataset['rating']
for i in y_set.keys():
rev_rate.append(rate[i])
for i in range(len(rev_rate)):
if(rev_rate[i] == 1.0):
rev_rate[i] = 'Negative'
elif(rev_rate[i] == 2.0):
rev_rate[i] = 'Negative'
elif(rev_rate[i] == 3.0):
rev_rate[i] = 'Negative'
elif(rev_rate[i] == 4.0):
rev_rate[i] = 'Positive'
elif(rev_rate[i] == 5.0):
rev_rate[i] = 'Positive'
list_tuple_word_score = []
for i in range(len(tuple_word_score)):
list_tuple_word_score.append(tuple_word_score[i])
comparision_dataframe = pd.DataFrame()
comparision_dataframe['Original Review'] = dataset['reviewText']
comparision_dataframe['Opinion Words'] = list_Opinion_Words
comparision_dataframe['Maximum scored Opinion Words'] = list_max_abs_score
comparision_dataframe['Final Opnion Words'] = list_tuple_word_score
comparision_dataframe['Average'] = list_average_score
comparision_dataframe['Reviewer Rating'] = rev_rate
comparision_dataframe['Calculated rating'] = y
save = comparision_dataframe.to_csv(sep=',')
text_file = open("train_file.csv", "w")
text_file.write(save)
text_file.close()
#######################################################################
# Step 14: Making the Confusion Matrix
y = comparision_dataframe['Calculated rating']
from sklearn.metrics import confusion_matrix
#cm = confusion_matrix(y_test, y_pred)
cm = confusion_matrix(rev_rate, y)
accuracy = 0
for i in range(len(cm)):
for j in range(len(cm)):
if(i == j):
accuracy = accuracy + cm[i][j]
accuracy = accuracy/ len(rev_rate) * 100
TP = np.diag(cm)
FP = np.sum(cm, axis=0) - TP
FN = np.sum(cm, axis=1) - TP
num_classes = len(cm)
TN = []
for i in range(num_classes):
temp = np.delete(cm, i, 0) # delete ith row
temp = np.delete(temp, i, 1) # delete ith column
TN.append(sum(sum(temp)))
#Let's make a sanity check: for each class, the sum of TP, FP, FN, and TN
# must be equal to the size of our test set (here 10,000):
#let's confirm that this is indeed the case:
l = len(rev_rate)
for i in range(num_classes):
print(TP[i] + FP[i] + FN[i] + TN[i] == l)
# link : http://blog.exsilio.com/all/accuracy-precision-recall-f1-score-interpretation-of-performance-measures/
precision = TP/(TP+FP)
recall = TP/(TP+FN)
FScore = 2*(recall * precision) / (recall + precision)
DataFrame = pd.DataFrame()
DataFrame['precision'] = precision
DataFrame['recall'] = recall
DataFrame['FScore'] = FScore
###################################################################################################
#################################################################################################3
# Step 15: Testing
New_sentence = "this phone is gifted to me by my friend.oh my god!!. i am not much impreseed by the sound quality. It isn't the one i expected. i am disappointed by its short term life."
New_rating = 'Negative'
New_sentence = ' This is a wonderful screen touch!! can it get better than this? i am so much excited!!'
New_rating = 'Positive'
#step 1: pre processing
# isn't = is not
New_sentence = decontracted(New_sentence)
# removed Unwanted symbols, lower case
comment_dict2 = defaultdict(list)
for i in range(1):
sentence = re.sub('[^a-zA-Z.]',' ', New_sentence)
sentence = sentence.lower()
sentence = sentence.split('.')
for k in range(len(sentence)):
review = sentence[k].split()
review = [word for word in review if not word in set(stopwords.words('english'))]
sentence[k] = ' '.join(review)
comment_dict2[i].append(sentence[k])
#delete unwanted '' words
for j in range(len(comment_dict2)):
comment_dict2[j] = [comment_dict2[j][i] for i in range(len(comment_dict2[j])) if comment_dict2[j][i] not in '']