<commit_before>import sys
from disco.core import Disco, result_iterator
from disco.settings import DiscoSettings

def map(entry, params):
    for word in entry.split():
        yield word, 1

def reduce(iter, out, params):
    s = {}
    for word, freq in iter:
        s[word] = s.get(word, 0) + int(freq)
    for word, freq in s.iteritems():
        out.add(word, freq)

disco = Disco(DiscoSettings()['DISCO_MASTER'])
print "Starting Disco job.."
print "Go to %s to see status of the job." % disco.master
results = disco.new_job(name="wordcount",
                        input=["http://discoproject.org/media/text/chekhov.txt"],
                        map=map,
                        reduce=reduce,
                        save=True).wait()
print "Job done. Results:"
for word, freq in result_iterator(results):
    print word, freq<commit_msg>make the count_words example slightly more idiomatic<commit_after>import sys
from disco.core import Disco, result_iterator
from disco.settings import DiscoSettings

def map(line, params):
    for word in line.split():
        yield word, 1

def reduce(iter, params):
    from disco.util import kvgroup
    for word, counts in kvgroup(sorted(iter)):
        yield word, sum(counts)

disco = Disco(DiscoSettings()['DISCO_MASTER'])
print "Starting Disco job.."
print "Go to %s to see status of the job." % disco.master
results = disco.new_job(name="wordcount",
                        input=["http://discoproject.org/media/text/chekhov.txt"],
                        map=map,
                        reduce=reduce,
                        save=True).wait()
print "Job done. Results:"
for word, count in result_iterator(results):
    print word, count
{'fraction_non_alphanumeric': 0.11150442477876106, 'fraction_numerical': 0.0017699115044247787, 'mean_word_length': 2.5705263157894738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7561066', 'n_tokens_mistral': 509, 'n_tokens_neox': 499, 'n_words': 153}