# Words by the Numbers 4, Exercise 1: bigrams and trigrams of a short invented passage. import re from collections import Counter passage = ("The tea was strong and the coffee was strong too, but the rain was heavy " "and the wind was heavy. Strong tea and heavy rain go together; the wind " "and the rain came together.") words = re.findall(r"[a-z]+", passage.lower()) bigrams = list(zip(words, words[1:])) trigrams = list(zip(words, words[1:], words[2:])) print("words:", len(words), "| bigrams:", len(bigrams), "| distinct bigrams:", len(set(bigrams))) print("trigrams:", len(trigrams), "| distinct trigrams:", len(set(trigrams))) print("commonest bigrams:", [(" ".join(g), c) for g, c in Counter(bigrams).most_common(4)]) print("commonest trigram:", [(" ".join(g), c) for g, c in Counter(trigrams).most_common(1)]) print("words that follow 'was':", Counter(b for a, b in bigrams if a == "was")) # Output when run (Python 3.14; the documentation-text figures depend on the Python version): # words: 34 | bigrams: 33 | distinct bigrams: 27 # trigrams: 32 | distinct trigrams: 32 # commonest bigrams: [('and the', 3), ('was strong', 2), ('the rain', 2), ('was heavy', 2)] # commonest trigram: [('the tea was', 1)] # words that follow 'was': Counter({'strong': 2, 'heavy': 2})