-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pos-neg_sentimentAnalysis.py
55 lines (32 loc) · 1.1 KB
/
Pos-neg_sentimentAnalysis.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
from mrjob.job import MRJob
import nltk
import re
WORD_RE = re.compile(r"[\w']+")
class smAnalysis(MRJob):
def init(self):
self.poslisting=['happy','care','win','love','empathy','glow','fantastic','handsome', 'clever', 'rich']
self.neglisting=['no','die','murder','kill','cry']
self.map={}
self.map.setdefault('pos',0)
self.map.setdefault('neg',0)
def smainitialmapper(self,_,line):
for word in WORD_RE.findall(line):
if word in self.poslisting:
self.map['pos']=self.map['pos']+1
if word in self.neglisting:
self.map['neg']=self.map['neg']+1
def smafinalmapper(self):
for word,val in self.map.iteritems():
yield word,val
def smacombiner(self ,word,countpair):
yield word,sum(countpair)
def smareducer(self,word,count):
yield word,sum(count)
def steps(self):
return [self.mr(mapper_init=self.init,
mapper=self.smainitialmapper,
mapper_final=self.smafinalmapper,
combiner=self.smacombiner,
reducer=self.smareducer)]
if __name__ == '__main__':
smAnalysis.run()