This commit is contained in:
Paweł Skurzyński 2024-04-05 15:15:25 +02:00
parent 2a702c14d3
commit d881302e4e
1 changed files with 31 additions and 38 deletions

View File

@ -1,38 +1,31 @@
#!/usr/bin/env python from operator import itemgetter
from operator import itemgetter import sys
import sys
# Lista do przechowywania wyników
current_word = None results = []
current_count = 0
word = None # input comes from STDIN
for line in sys.stdin:
# input comes from STDIN # remove leading and trailing whitespace
for line in sys.stdin: line = line.strip()
# remove leading and trailing whitespace
line = line.strip() # parse the input we got from mapper.py
word, count = line.split('\t', 1)
# parse the input we got from mapper.py
word, count = line.split('\t', 1) # convert count (currently a string) to int
try:
# convert count (currently a string) to int count = int(count)
try: except ValueError:
count = int(count) # count was not a number, so silently
except ValueError: # ignore/discard this line
# count was not a number, so silently continue
# ignore/discard this line
continue # Dodaj słowo i jego długość do listy wyników
results.append((word, len(word), count))
# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer # Posortuj wyniki po długości słowa
if current_word == word: results.sort(key=lambda x: x[1])
current_count += count
else: # Wypisz posortowane wyniki
if current_word: for result in results:
# write result to STDOUT print('%s\t%s' % (result[0], result[2]))
print('%s\t%s' % (current_word, current_count))
current_count = count
current_word = word
# do not forget to output the last word if needed!
if current_word == word:
print('%s\t%s' % (current_word, current_count))