Commit 4c2c7ece authored by Elizabeth Myers's avatar Elizabeth Myers 💬

Add new statistics counter for word lengths

parent a4a6f4a0
#!/usr/bin/env python3
""" Prints statistics about the word corpus """
import wordmanager
wordmanager.word_len_stats("words.txt")
......@@ -2,6 +2,8 @@
""" Functions for manipulating the word and extension corpuses """
import fcntl
from collections import defaultdict
class WordError(Exception):
""" Generic word error """
pass
......@@ -58,3 +60,21 @@ def del_word(filename, word):
corpus.seek(0)
corpus.writelines(newwords)
corpus.truncate()
def word_len_stats(filename):
""" Print word length stats about a given file """
lengths = defaultdict(int)
with open(filename, "r") as corpus:
fcntl.flock(corpus, fcntl.LOCK_EX)
for word in corpus:
word = word.strip()
lengths[len(word)] += 1
print("Length statistics for corpus {}".format(filename))
for index in range(1, max(lengths.keys()) + 1):
print("{:<4}{}".format(str(index) + ":", lengths[index]))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment