OILS / benchmarks / compute / word_freq.py View on Github | oilshell.org

36 lines, 23 significant
1#!/usr/bin/env python2
2"""
3word_freq.py
4"""
5from __future__ import print_function
6
7import sys
8
9
10def main(argv):
11 try:
12 iters = int(argv[1])
13 except IndexError:
14 iters = 1000
15
16 text = sys.stdin.read()
17
18 words = {}
19
20 for i in xrange(iters):
21 for word in text.split():
22 if word in words:
23 words[word] += 1
24 else:
25 words[word] = 1
26
27 for word in words:
28 print("%d %s" % (words[word], word))
29
30
31if __name__ == '__main__':
32 try:
33 main(sys.argv)
34 except RuntimeError as e:
35 print('FATAL: %s' % e, file=sys.stderr)
36 sys.exit(1)