#!/usr/bin/env python3
"""Punkt odniesienia bez Hadoopa: zliczanie słów jednym procesem Pythona (collections.Counter).

Ta sama tokenizacja co w pozostałych wersjach: małe litery, ciągi liter Unicode.
Użycie: python3 wordcount_single_process.py PLIK [WYJŚCIE.tsv]
"""
import re
import sys
import time
from collections import Counter

LETTERS = re.compile(r'[^\W\d_]+')

started = time.time()
counts = Counter()
with open(sys.argv[1], encoding='utf-8') as f:
    for line in f:
        counts.update(LETTERS.findall(line.lower()))
print('różnych słów: %d, wszystkich: %d, czas %.1f s' % (len(counts), sum(counts.values()), time.time() - started))
if len(sys.argv) > 2:
    with open(sys.argv[2], 'w', encoding='utf-8') as out:
        for word, count in sorted(counts.items()):
            out.write('%s\t%d\n' % (word, count))
for word, count in sorted(counts.items(), key=lambda pair: (-pair[1], pair[0]))[:5]:
    print('%s\t%d' % (word, count))
