Build a bigram language model from a tiny corpus and predict the most likely next word after a given word (deterministic tie-break: highest count, then alphabetical).
Build a bigram language model from a tiny corpus and predict the most likely next word after a given word (deterministic tie-break: highest count, then alphabetical).
Answer
from collections import defaultdict corpus = "the dog runs the cat sleeps the dog barks the bird sings".split() # count how often each word follows each preceding word bigrams = defaultdict(lambda: defaultdict(int)) for a, b in zip(corpus, corpus[1:]): bigrams[a][b] += 1 def predict(word): nexts = bigrams[word] total = sum(nexts.values()) # break ties deterministically: highest count, then alphabetical best = sorted(nexts.items(), key=lambda kv: (-kv[1], kv[0]))[0] word_next, count = best p = count / total print(f"after '{word}' -> '{word_next}' (p={p:.1f})") predict('the')