-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize_count.py
More file actions
50 lines (42 loc) · 1.23 KB
/
size_count.py
File metadata and controls
50 lines (42 loc) · 1.23 KB
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
__author__ = 'farhankhwaja'
import MapReduce
import sys
from collections import OrderedDict
"""
A python program, that implements a mapReduce algorithm to count the words of each size (large, medium, small, tiny) in a document.
"""
mr = MapReduce.MapReduce()
# =============================
# Do not modify above this line
def mapper(record):
# key: document identifier
# value: document contents
key = record[0]
value = record[1]
words = value.split()
large = 0
medium = 0
small = 0
tiny = 0
for w in words:
if len(w) >= 10:
large += 1
elif 5 <= len(w) <= 9:
medium += 1
elif 2 <= len(w) <= 4:
small += 1
else:
tiny += 1
mr.emit_intermediate(key,["large",large])
mr.emit_intermediate(key,["medium",medium])
mr.emit_intermediate(key,["small",small])
mr.emit_intermediate(key,["tiny",tiny])
def reducer(key, list_of_values):
# key: document identifier
# value: list of large, medium, small & tiny word count
mr.emit((key, list_of_values))
# Do not modify below this line
# =============================
if __name__ == '__main__':
inputdata = open(sys.argv[1])
mr.execute(inputdata, mapper, reducer)