# Words by the Numbers 2, Exercise 1: a rank-frequency table for a short invented passage. import re from collections import Counter text = ( "the tide came in and the tide went out and the boats rested on the sand " "while the gulls watched the water and the children ran to the sea" ) tokens = re.findall(r"[a-z']+", text.lower()) counts = Counter(tokens) print("tokens:", len(tokens), "| types:", len(counts)) print("rank word count rank*count") for rank, (word, n) in enumerate(counts.most_common(8), start=1): print(f"{rank:4d} {word:<9} {n:5d} {rank * n:10d}") # Group the ranks by count to see the 'steps' in the tail. steps = Counter(counts.values()) print("types per count:", sorted(steps.items(), reverse=True)) # Output when run (Python 3.14; the documentation-text counts depend on the Python version): # tokens: 29 | types: 19 # rank word count rank*count # 1 the 8 8 # 2 and 3 6 # 3 tide 2 6 # 4 came 1 4 # 5 in 1 5 # 6 went 1 6 # 7 out 1 7 # 8 boats 1 8 # types per count: [(8, 1), (3, 1), (2, 1), (1, 16)]