-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobidroidMR_2.py
851 lines (549 loc) · 20.2 KB
/
obidroidMR_2.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
from mrjob.job import MRJob
# from sentClassifier import sentClassify
from cPickle import load
import re
import nltk
from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
from nltk.collocations import BigramCollocationFinder
from nltk.metrics import BigramAssocMeasures as BAM
from nltk.metrics import TrigramAssocMeasures as TAM
from nltk import FreqDist
class ObidroidReview(MRJob):
@staticmethod
# def featureExtractor(sentStr):
# def getWordsFromSent(sent):
# words = [w.lower() for w in word_tokenize(sent) if w not in stopwords.words('english') ]
# return words
# def getTaggedSents(sentWords):
# return nltk.pos_tag(sentWords)
# def getCharlesScore(upperCount, negativeWordCount, positiveWordCount, bigramBeginWithNotCount):
# new_vote = 0 - upperCount - negativeWordCount + positiveWordCount + 2 * bigramBeginWithNotCount
# return new_vote
# # feature extraction methods
# def getReviewDict(sent):
# # print parsedata[:5]
# contain_features = {}
# global top_words
# for word in top_words:
# contain_features['contains(%s)' % (word)] = (word in set(sent))
# return contain_features
# def getAdjOpinionScore(tagSent, opinioncorpus):
# score = 0
# for (word, tag) in tagSent:
# if tag == 'JJ' or tag == 'ADV' or tag == 'VBG' or tag == 'RB' or tag == 'VBZ' or tag == 'JJS':
# if word in opinioncorpus['positive']:
# score += 1
# if word in opinioncorpus['negative']:
# score -= 1
# return score
# ##
# ## Charles' Features
# ##
# def getUpperCount(sent):
# uppercase_meaningless_words = ["A", "I", "IPOD", "USB", "MP3", "CD", "FM", "GB", "PC", "LCD", "MP-3", "WMA", "WMP",
# "AC/DC", "PDA", "PXC250", "XP", "LED", "AC", "AGK", "DVD", "SD", "MB"]
# upperCount = 0
# for word in sent.split(" "):
# word = word.replace(".","").replace(",","").replace("!","").replace("?","").replace("##","").replace("(","").replace(")","").replace("**","")
# for letter in word:
# if letter.isdigit():
# word = word.replace(letter, "")
# else:
# break
# if word.isupper() and len(word) != 1 and not word in uppercase_meaningless_words:
# upperCount += 1
# return upperCount
# def getPostiveWordCount(sent):
# positive_keywords = ["good", "happy", "love", "great", "reasonable", "glad", "simple", "outstanding", "easy",
# "wonderful", "cool", "remarkably", "remarkable", "enjoy", "nice", "thoughtful", "pretty",
# "responsive", "comforatable", "favorite", "desire", "best", "solid", "cool", "impressed",
# "sleek", "appealing", "rocks", "blazing", "amazing", "plus", "blessing", "awesome", "loved",
# "enjoyed", "desired", "impressive", "impress", "rocked", "bless", "positive", "fabulous"]
# postiveCount = 0
# for word in sent.split(" "):
# word = word.replace(".","").replace(",","").replace("!","").replace("?","").replace("##","").replace("(","").replace(")","").replace("**","")
# if word.lower() in positive_keywords:
# postiveCount += 1
# return postiveCount
# def getNegativeWordCount(sent):
# negative_keywords = ["bad", "sad", "don't", "could not", "crappy", "unfortunately", "remove", "why", "poor",
# "bothersome", "terrible", "although", "complaints", "outrageous", "isn't", "poorly",
# "drawback", "annoying", "against", "irritating", "wouldn't", "won't", "wasn't", "couldn't",
# "awful", "didn't", "hasn't", "difficult", "hate", "incorrect", "junk", "trash", "removed",
# "complain", "complained", "hated", "negative"]
# negativeCount = 0
# for word in sent.split(" "):
# word = word.replace(".","").replace(",","").replace("!","").replace("?","").replace("##","").replace("(","").replace(")","").replace("**","")
# if word.lower() in negative_keywords:
# negativeCount += 1
# return negativeCount
# def getBigramBeginWithNotCount(sent):
# negative_keywords = ["bad", "sad", "don't", "could not", "crappy", "unfortunately", "remove", "why", "poor",
# "bothersome", "terrible", "although", "complaints", "outrageous", "isn't", "poorly",
# "drawback", "annoying", "against", "irritating", "wouldn't", "won't", "wasn't", "couldn't",
# "awful", "didn't", "hasn't", "difficult", "hate", "incorrect", "junk", "trash", "removed",
# "complain", "complained", "hated", "negative"]
# bigramPostiveCount = 0
# for i, word in enumerate(word_tokenize(sent)):
# if word.lower() == "not":
# if word_tokenize(sent)[i + 1] in negative_keywords : # e.g. NOT bad
# bigramPostiveCount += 1
# if i < len(word_tokenize(sent)) - 2 and word_tokenize(sent)[i + 2] in negative_keywords: # e.g. NOT too bad
# bigramPostiveCount += 1
# else: # e.g. NOT good
# bigramPostiveCount -= 1
# return bigramPostiveCount
# def getUnigramWordFeatures(sent, words):
# return dict(('contains("%s")' % word, True) for word in words)
# def getBigramWordFeatures(sent, words, score_fn=BAM.chi_sq, n=2000):
# filtered_words = [w for w in words if w != '.' and w != '?' and w != ')' and w != '(' and w != '-']
# bigram_finder = BigramCollocationFinder.from_words(filtered_words)
# # score = bigram_finder.score_ngrams(BAM.jaccard)
# bigrams = bigram_finder.nbest(score_fn, n)
# return dict((bg, True) for bg in chain(filtered_words, bigrams))
# def getSentOverallOpinion(sent, words, opinioncorpus):
# score = 0.0
# if len(words) != 0:
# for w in words:
# if w in opinioncorpus['positive']:
# score += 1.0
# elif w in opinioncorpus['negative']:
# score -= 1.0
# return score
# else:
# return score
# def getCharCount(sent):
# return int(len(sent))
# def getWordCount(sent):
# return len(word_tokenize(sent))
# def getCommaCount(sent):
# commaRegEx = re.compile(',')
# numoccur = len([a.start() for a in commaRegEx.finditer(sent)])
# return numoccur
# def getExclaimCount(sent):
# exclaimRegEx = re.compile('!')
# numoccur = len([a.start() for a in exclaimRegEx.finditer(sent)])
# return numoccur
# def getSemicolonCount(sent):
# semicolonRegEx = re.compile(';')
# numoccur = len([a.start() for a in semicolonRegEx.finditer(sent)])
# return numoccur
# def getWhiteSpaceCount(sent):
# whitespaceRegEx = re.compile(' ')
# numoccur = len([a.start() for a in whitespaceRegEx.finditer(sent)])
# return numoccur
# def getUpperCount(sent):
# numoccur=0
# for i in range(len(sent)):
# i=str(i)
# if i.isupper==True:
# numoccur+=1
# return numoccur
# def getDigitCount(sent):
# numoccur=0
# for i in range(len(sent)):
# i=str(i)
# if i.isdigit==True:
# numoccur+=1
# return numoccur
# def getTabCount(sent):
# tabRegEx = re.compile(' ')
# numoccur = len([a.start() for a in tabRegEx.finditer(sent)])
# return numoccur
# def getPercentCount(sent):
# numoccur=0
# for i in sent:
# i=str(i)
# if i== '%':
# numoccur+=1
# return numoccur
# def getEtcCount(sent):
# numoccur=0
# for i in sent:
# i=str(i)
# if i== 'etc.':
# numoccur+=1
# return numoccur
# def getDollarCount(sent):
# numoccur=0
# for i in sent:
# i=str(i)
# if i== '$':
# numoccur+=1
# return numoccur
# def getAvgWordLen(sent):
# avg, total = 0,0
# sent=sent.split(" ")
# ln= len(sent)
# if ln>0:
# for i in sent:
# i=str(i)
# lnword=len(i)
# total=total+lnword
# avg=total/ln
# return avg
# def getWordLen6(sent):
# numoccur = 0
# for i in sent:
# if len(i)>= 6:
# numoccur+=1
# return numoccur
# def getUniqueWords(sent):
# word=[]
# wunique=0
# for item in sent:
# if item not in word:
# wunique+=1
# return wunique
# def getCountJJ(sent):
# countjj= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="JJ":
# countjj+=1
# return countjj
# def getCountCC(sent):
# countcc= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="CC":
# countcc+=1
# return countcc
# def getCountIN(sent):
# countin= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="IN":
# countin+=1
# return countin
# def getCountRB(sent):
# countrb= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="RB":
# countrb+=1
# return countrb
# def getCountPRP(sent):
# countprp= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="PRP":
# countprp+=1
# return countprp
# def getCountTO(sent):
# countto= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="TO":
# countto+=1
# return countto
# def getCountVBD(sent):
# countvbd= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VBD":
# countvbd+=1
# return countvbd
# def getCountJJR(sent):
# countjjr= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="JJR":
# countjjr+=1
# return countjjr
# def getCountNN(sent):
# countnn= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="NN":
# countnn+=1
# return countnn
# def getCountNNS(sent):
# countnns= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="NNS":
# countnns+=1
# return countnns
# def getCountNNP(sent):
# countnnp= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="NNP":
# countnnp+=1
# return countnnp
# def getCountRBR(sent):
# countrbr= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="RBR":
# countrbr+=1
# return countrbr
# def getCountVB(sent):
# countvb= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VB":
# countvb+=1
# return countvb
# def getCountVBP(sent):
# countvbp= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VBP":
# countvbp+=1
# return countvbp
# def getCountVBZ(sent):
# countvbz= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VBZ":
# countvbz+=1
# return countvbz
# def getCountVBG(sent):
# countvbg= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VBG":
# countvbg+=1
# return countvbg
# def getCountVBN(sent):
# countvbn= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="VBN":
# countvbn+=1
# return countvbn
# def getCountMD(sent):
# countmd= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="MD":
# countmd+=1
# return countmd
# def getCountWDT(sent):
# countwdt= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="WDT":
# countwdt+=1
# return countwdt
# def getCountPRPA(sent):
# countprpa= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(sent)):
# if text[i][1]=="PRP$":
# countprpa+=1
# return countprpa
# def getCountJN(sent):
# countjn= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(text)):
# if text[i-1][1]=="JJ" and text[i][1] in ["NN","NNS"]: countjn+=1
# return countjn
# def getCountRJ(sent):
# countrj= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(text)):
# if text[i-1][1] in ["RB","RBR","RBS"] and text[i][1]=="JJ": countrj+=1
# return countrj
# def getCountJJC(sent):
# countjjc= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(text)):
# if text[i-1][1]=="JJ" and text[i][1]=="JJ": countjjc +=1
# return countjjc
# def getCountNJ(sent):
# countnj= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(text)):
# if text[i-1][1]=="NNS" and text[i][1]=="jj": countnj+=1
# return countnj
# def getCountRV(sent):
# countrv= 0
# sent= nltk.word_tokenize(sent)
# text=nltk.pos_tag(sent)
# for i in range(len(text)):
# if text[i-1][1]==["RR","RBS","RBR"] and text[i][1]==["VB", "VBN", "VBD", "VBG"]: countrv+=1
# return countrv
# def getAfinn(sent):
# w=str(sent)
# w=w.lower()
# w=w.split()
# total, avg=0,0
# ln = len(w)
# if ln>0:
# for item in range(len(w)):
# for i in range(len(dc)):
# if dc.keys()[i]==w[item]:
# temp = int(dc.values()[i])
# total += temp
# avg=total/ln
# # print avg
# return avg
# def pmiScore(sent):
# sent = nltk.word_tokenize(sent)
# x=nltk.pos_tag(sent)
# # print x
# countnn, countjj, countnj=0,0,0
# pnn, pjj, pjn=0,0,0
# for i in range(len(x)):
# if x[i][1] in ["NN", "NNP"]:countnn+=1
# if x[i][1]=="JJ":countjj+=1
# for i in range(len(x)):
# if str(x[i-1][1])in ["NN","NNP", "JJ"] and x[i][1]==["JJ","RB","NN","VB", "VBP", "VBD","VBR", "VBG","VBZ"]: countnj+=1
# if (len(sent)-1)>0:
# pnn=countnn/len(sent)
# pjj=countjj/len(sent)
# pnj=countnj/(len(sent)-1)
# # print pnn, pjj, pnj
# if pnj>0:
# pmi= math.log(pnj/(pnn*pjj))
# return pmi
# def parseOpinionLexicon():
# # print os.getcwd()
# opinionLexPath = 'lexicon/opinionwords/'
# posfileObj = open(opinionLexPath + 'positive-words.txt')
# negfileObj = open(opinionLexPath + 'negative-words.txt')
# lexWords = {}
# lexWords['positive'] = [l[:-2] for l in posfileObj if not l.startswith(';') and l[:-2] is not '']
# lexWords['negative'] = [l[:-2] for l in negfileObj if not l.startswith(';') and l[:-2] is not '']
# posfileObj.close()
# negfileObj.close()
# return lexWords
# sentwords = getWordsFromSent(sentStr)
# taggedSent = getTaggedSents(sentwords)
# # opinionWords = parseOpinionLexicon()
# featList = {}
# # featList['charCount'] = getCharCount(sentStr)
# featList['wordCount'] = getWordCount(sentStr)
# # # featList['commaCount'] = getCommaCount(sentStr)
# # # featList['semicolonCount'] = getSemicolonCount(sentStr)
# # # featList['uppercount'] = getUpperCount(sentStr)
# featList['digitcount'] = getDigitCount(sentStr)
# featList['exclaimCount'] = getExclaimCount(sentStr)
# featList["countJJ"]=getCountJJ(sentStr)
# featList["countCC"]=getCountCC(sentStr)
# featList["countVBD"]=getCountVBD(sentStr)
# featList["countRB"]=getCountRB(sentStr)
# featList["countVBG"]=getCountVBG(sentStr)
# featList["countVBZ"]=getCountVBZ(sentStr)
# #Charles' Features
# featList['upperCount'] = getUpperCount(sentStr)
# featList['postiveWordCount'] = getPostiveWordCount(sentStr)
# featList['negativeWordCount'] = getNegativeWordCount(sentStr)
# featList['bigramBeginWithNotCount'] = getBigramBeginWithNotCount(sentStr)
# featList['charlesScore'] = getCharlesScore(
# featList['upperCount'],
# featList['postiveWordCount'],
# featList['negativeWordCount'],
# featList['bigramBeginWithNotCount'])
# featList.update(getUnigramWordFeatures(sentStr, sentwords))
# featList.update(getBigramWordFeatures(sentStr, sentwords))
# return featList
## Sentiment Classifier
# def tokenizeReviewsBySentence(revStr):
# return nltk.tokenize.sent_tokenize(revStr)
# def getReviewSentiment(tknRevs, classifier):
# revAggSentiment = 0
# for sent in tknRevs:
# sent = unicode(sent.strip())
# featdata = extractor.featureExtractor(sent)
# cl= classifier.classify(featdata)
# if cl == 'pos':
# label = 1
# elif cl == 'neutral':
# label = 0
# else:
# label = -1
# revAggSentiment += label
# return revAggSentiment
# def sentClassify(sentStr):
# """
# Given a sentence string, classify the sentence
# """
# tokenizedReviews = tokenizeReviewsBySentence(sentStr)
# ## load the classifier pickle
# fObj = open('mySentClassifier.pickle')
# cl = load(fObj)
# fObj.close()
# revSent = getReviewSentiment(tokenizedReviews, cl)
# return revSent
@staticmethod
def getFeatures(rev):
wordpattern = re.compile('\w+')
capspattern = re.compile('([A-Z])+\w')
exclaimpattern = re.compile('!')
rev = rev.decode('utf-8', 'ignore')
revCharLength = len(rev)
words = wordpattern.findall(rev)
revWordsLength = len(words)
revUniqueWordLength = len(set(words))
revCapCount = len(capspattern.findall(rev))
revExclaimCount = len(exclaimpattern.findall(rev))
revAdjCount = 0
revPosTokens = nltk.pos_tag(nltk.word_tokenize(rev))
for _, pos in revPosTokens:
if pos == 'JJ' or pos == 'VBP':
revAdjCount += 1
## Sentiment Classifiers:
# revSentAgg = sentClassify(rev)
## overall production sentiment classifier
blob = TextBlob(rev, analyzer=NaiveBayesAnalyzer())
blobSent = blob.sentiment
# print blobSent
if blobSent[0] == 'pos':
revSent = 1 * blobSent[1]
elif blobSent[0] == 'neg':
revSent = -1 * blobSent[2]
else:
revSent = 0
return [
revCharLength,
revWordsLength,
revUniqueWordLength,
revCapCount,
revExclaimCount,
revAdjCount,
revSentAgg,
revSent
]
def getRecord(self, _, record): #Mapper 1
record = record.split(',')
idpattern = re.compile('(\w+\.+\w+[(\.+)(\w+)]+)')
appid = idpattern.split(record[0])
features = ObidroidReview.getFeatures(record[1])
yield appid[1], features
def performAction(self,appid,appfeature): #Reducer 1
yield appid, list(appfeature)
def steps(self):
return [
self.mr(mapper=self.getRecord, reducer=self.performAction)
]
if __name__ == '__main__':
ObidroidReview.run()