# Words by the Numbers 1, Exercise 3: a letter profile # Compare the letter profile of two texts: the commonest letter, # the share of vowels, and the number of different letters used. from collections import Counter texts = { "Austen": ("It is a truth universally acknowledged, that a single man in " "possession of a good fortune, must be in want of a wife."), "invented": ("The rain fell on the town and the river rose. The people of " "the town watched the river and waited."), } for name, text in texts.items(): letters = [ch for ch in text.lower() if ch.isalpha()] counts = Counter(letters) top, n = counts.most_common(1)[0] vowels = sum(counts[v] for v in "aeiou") / len(letters) print(f"{name:9} letters={len(letters):3d} commonest={top} ({n / len(letters):.1%}) vowels={vowels:.1%} distinct={len(counts)}") # Output when run: # Austen letters= 92 commonest=a (9.8%) vowels=40.2% distinct=22 # invented letters= 77 commonest=e (18.2%) vowels=37.7% distinct=16