Updated lettercount to use a Counter
authorNeil Smith <neil.git@njae.me.uk>
Fri, 16 Mar 2018 20:29:13 +0000 (20:29 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Fri, 16 Mar 2018 20:29:13 +0000 (20:29 +0000)
support/lettercount.py

index c095cefb6a67ab9a9e21d97f85b0370c3a66e0fa..fe1ea08b655d6ff6051520e18486acf5dceb6e95 100644 (file)
@@ -2,16 +2,12 @@ import collections
 import string
 from utilities import sanitise
 
-# def sanitise(text):
-#     return [l.lower() for l in text if l in string.ascii_letters]
-
 corpora = ['shakespeare.txt', 'sherlock-holmes.txt', 'war-and-peace.txt']
-counts = collections.defaultdict(int)
+counts = collections.Counter()
 
 for corpus in corpora:
     text = sanitise(open(corpus, 'r').read())
-    for letter in text:
-        counts[letter] += 1
+    counts.update(text)
 
 sorted_letters = sorted(counts, key=counts.get, reverse=True)