-
Notifications
You must be signed in to change notification settings - Fork 1
/
Word_Count.py
64 lines (45 loc) · 1.23 KB
/
Word_Count.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
54
55
56
57
58
59
60
61
62
63
64
# > Touch data.txt
# > nano data.txt
# DEAR BEAR RIVER CAR CAR RIVER DEAR CAR BEAR (Input this sentence when you're inside nano editor)
# ctrl + s, ctrl + x, yes (To save and exit nano)
# > touch mapper.py
# > nano mapper.py
# Input these line to mapper.py file
import sys
for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print(f"{word}\t{1}")
# > cat mapper.py (To view your mapper.py file)
# > touch reducer.py
# > nano reducer.py
# Input these line to reducer.py file
from operator import itemgetter
import sys
current_word = None
current_count = 0
word = None
for line in sys.stdin:
line = line.strip()
word, count = line.split("\t", 1)
try:
count = int(count)
except ValueError:
continue
if current_word == word:
current_count += count
else:
if current_word:
print(f"{current_word}\t{current_count}")
current_count = count
current_word = word
if current_word == word:
print(f"{current_word}\t{current_count}")
# > cat reducer.py (To view your reducer.py file)
# > cat data.txt | python mapper.py | sort -kl, l | python reducer.py
# Output:
# BEAR 2
# CAR 3
# DEAR 2
# RIVER 2