-
Notifications
You must be signed in to change notification settings - Fork 1
/
vocabularyVariances.py
60 lines (33 loc) · 1.06 KB
/
vocabularyVariances.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
from mrjob.job import MRJob
import nltk
from nltk.corpus import stopwords
import re
from string import punctuation
#.strip(punctuation)
WORD_RE = re.compile(r"[\w']+")
class smAnalysis(MRJob):
def init(self):
self.fileid=stopwords.words('english')
self.map={}
self.map.setdefault('content',0)
self.map.setdefault('text',0)
def smamapper(self,_,line):
for word in WORD_RE.findall(line):
self.map['text']=self.map['text']+1
if word.lower() not in self.fileid:
self.map['content']=self.map['content']+1
def smafinalmapper(self):
for key,value in self.map.iteritems():
yield key,value
def smacombiner(self,key,value):
yield key,sum(value)
def smareducer(self,word,count):
yield word,sum(count)
def steps(self):
return [self.mr(mapper_init=self.init,
mapper=self.smamapper,
mapper_final=self.smafinalmapper,
combiner=self.smacombiner,
reducer=self.smareducer)]
if __name__ == '__main__':
smAnalysis.run()