1. The likeliest next word
With a bigram table, predicting is easy: look up the current word and pick the next word with the biggest count. Probability is just count ÷ total: if after “a” we saw six 3 times, four once and catch once, then P(six | a) = 3/5.
To generate a sentence, repeat: predict the next word, add it, move on. Always taking the top word is called greedy decoding. It is simple but can get stuck in loops (“a six a six a six…”), so we always cap the length. Real chatbots add a little controlled randomness to sound more natural.
If a word was never followed by anything, the model has no idea — return null and stop.
Challenge: Most likely next word
Write mostLikelyNext(table, word): look at table[word] and return the next word with the highest count. On a tie, return the one that comes first in the object. If word is not in the table, return null.
2. Final project: cricket commentary bot
Time to put it all together into a tiny next-word predictor, trained on (made-up) cricket commentary:
train(sentences) → the bigram modelpredictNext(model, word) → the likeliest next wordgenerate(model, start, n) → a sentence written greedily
One important detail: count pairs inside each sentence only. The last word of “Kohli hits a six!” should not be linked to the first word of the next sentence — those words were never next to each other in real speech.
When it works, swap in your own sentences: chai-stall orders, Hinglish chats, your favourite film dialogues. You have just built, from scratch, the core idea behind autocomplete. 🏏
Challenge: Next-word predictor
tokenise is ready. Write three functions:
train(sentences): tokenise each sentence separately and count its bigrams into one shared table { word: { next: count } }. Return the table.predictNext(model, word): lowercase word, then return its most frequent next word (ties → first in the object), or null if unknown.generate(model, start, n): start from the lowercased start word and add up to n more words with predictNext, stopping early at null. Return the words joined with single spaces.