-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr.py
More file actions
executable file
·32 lines (25 loc) · 933 Bytes
/
pr.py
File metadata and controls
executable file
·32 lines (25 loc) · 933 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
#!/usr/bin/env python
import itertools
import string
def map_reduce(i,mapper,reducer):
intermediate = []
for (key,value) in i.items():
print "%s,%s" % (key,value)
intermediate.extend(mapper(key,value))
groups = {}
for key, group in itertools.groupby(sorted(intermediate), lambda x: x[0]):
groups[key] = list([y for x, y in group])
return [reducer(intermediate_key,groups[intermediate_key]) for intermediate_key in groups]
def remove_punctuation(s):
return s.translate(string.maketrans("",""),string.punctuation)
def mapper(input_key,input_value):
return [(word,1) for word in remove_punctuation(input_value.lower()).split()]
def reducer(intermediate_key,intermediate_value_list):
return (intermediate_key,sum(intermediate_value_list))
filenames = ["data"]
i = {}
for filename in filenames:
f = open(filename)
i[filename] = f.read()
f.close()
print sorted(map_reduce(i,mapper,reducer),key= lambda x: x[0])