# Words by the Numbers 6, Exercise 1 import re FUNCTION_WORDS = ["the", "of", "and", "to", "a", "in", "it", "but", "so", "we"] sample_a = "the road to the harbour was long and the wind was cold but the men walked on and the night fell over the sea" # invented sample_b = "it was a long walk to a harbour in a cold wind so we rested in a barn and ate a loaf of bread in the dark" # invented mystery = "so it was a long road to a town in the north but we came in a wagon and slept in a shed" # invented def profile(text): words = re.findall(r"[a-z]+", text.lower()) return [100 * words.count(f) / len(words) for f in FUNCTION_WORDS], len(words) pa, na = profile(sample_a) pb, nb = profile(sample_b) pm, nm = profile(mystery) print("words:", na, nb, nm) print(f"{'word':6} {'A':>6} {'B':>6} {'mystery':>8} (uses per 100 words)") for f, x, y, z in zip(FUNCTION_WORDS, pa, pb, pm): print(f"{f:6} {x:6.1f} {y:6.1f} {z:8.1f}") dist_a = sum(abs(x - z) for x, z in zip(pa, pm)) dist_b = sum(abs(y - z) for y, z in zip(pb, pm)) print(f"distance to A: {dist_a:.1f} distance to B: {dist_b:.1f}") print("closer to", "A" if dist_a < dist_b else "B") # Output when run (Python 3.14; Federalist text from the Project Gutenberg plain-text file): # words: 24 27 23 # word A B mystery (uses per 100 words) # the 25.0 3.7 4.3 # of 0.0 3.7 0.0 # and 8.3 3.7 4.3 # to 4.2 3.7 4.3 # a 0.0 18.5 17.4 # in 0.0 11.1 13.0 # it 0.0 3.7 4.3 # but 4.2 0.0 4.3 # so 0.0 3.7 4.3 # we 0.0 3.7 4.3 # distance to A: 68.5 distance to B: 15.0 # closer to B