From 872eabffdbbd09fc6470802e35f078a87e041e52 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Mon, 7 Oct 2013 14:57:15 -0700 Subject: [PATCH] I just broke my rules about not adding more than one program in one language, but this code has gotta go in: contribution by Peter Norvig via email. Thank you! --- 04-code-golf/tf-04-pn.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 04-code-golf/tf-04-pn.py diff --git a/04-code-golf/tf-04-pn.py b/04-code-golf/tf-04-pn.py new file mode 100644 index 0000000..87c6b3e --- /dev/null +++ b/04-code-golf/tf-04-pn.py @@ -0,0 +1,10 @@ +# My golf score is slightly lower! +# Best wishes, Peter Norvig + +import re, sys, collections + +stopwords = set(open('../stop_words.txt').read().split(',')) +words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower()) +counts = collections.Counter(w for w in words if w not in stopwords) +for (w, c) in counts.most_common(25): + print w, '-', c