# Words by the Numbers 1, Exercise 1: a short passage, counted # Count a short invented passage: tokens, types, the top five words, # and how many types appear exactly once (hapax legomena). import re from collections import Counter text = ( "The rain fell on the town and the river rose. The people of the town " "watched the river and waited, and when the rain stopped the river fell." ) tokens = re.findall(r"[a-z']+", text.lower()) counts = Counter(tokens) print("tokens:", len(tokens)) print("types:", len(counts)) print("top five:", counts.most_common(5)) hapax = sorted(w for w, n in counts.items() if n == 1) print("seen once:", len(hapax), hapax) # Output when run: # tokens: 28 # types: 14 # top five: [('the', 8), ('and', 3), ('river', 3), ('rain', 2), ('fell', 2)] # seen once: 8 ['of', 'on', 'people', 'rose', 'stopped', 'waited', 'watched', 'when']