Code / AI & ML Basics / Unit 5: Language models

Predicting the next word अगले शब्द का अनुमान

Lesson 13 of 13 · about 30 minutes · Pick the likeliest next word, then build the full predictor.

⚡ 0 XP

Step 1 of 2

The likeliest next word

chaipeena×5garam×3wala×1

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 +15 XP

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.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • After “a” comes “six” (3 of 5)
  • Unknown word → null
  • A tie keeps the first word
Lesson notes (text version)

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 model
  • predictNext(model, word) → the likeliest next word
  • generate(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:

  1. train(sentences): tokenise each sentence separately and count its bigrams into one shared table { word: { next: count } }. Return the table.
  2. predictNext(model, word): lowercase word, then return its most frequent next word (ties → first in the object), or null if unknown.
  3. 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.

Search by task, format or tool name.