-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmostFrequenct.py
More file actions
45 lines (26 loc) · 824 Bytes
/
Copy pathmostFrequenct.py
File metadata and controls
45 lines (26 loc) · 824 Bytes
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
from mrjob.job import MRJob
import re
WORD_RE = re.compile(r"[\w']+")
class mostfrequent(MRJob):
def init(self):
self.map={}
def mfmapper(self,_,line):
for word in WORD_RE.findall(line):
word=word.lower()
self.map.setdefault(word,0)
self.map[word]=self.map[word]+1
def mapperfinal(self):
for word,val in self.map.iteritems():
yield word,val
def mfeducer(self,word,count):
yield word,sum(count)
def mfeducerglobal(self,word,count):
yield word,sum(count)
def steps(self):
return [self.mr(mapper_init=self.init,
mapper=self.mfmapper,
mapper_final=self.mapperfinal,
combiner=self.mfeducer,
reducer=self.mfeducerglobal)]
if __name__ == '__main__':
mostfrequent.run()