1. How could a keyboard guess the next word?
Imagine typing “The weather…” and seeing suggestions such as “is lovely”, “is nice” and “is hot”. How could a system produce these continuations?
One simple method is to inspect previous text and count what came next. If the same phrase has often been followed by a particular continuation, offer that continuation again.
After the phrase “The weather…”
This method does not need to check a weather forecast or represent the meaning of weather. It uses regularities in text. It is a useful historical starting point for language modelling, rather than a complete account of modern keyboard systems, which may also use neural models, personalisation and other signals.
2. Make the rule general: use an N-gram
Rather than build a separate rule for every complete phrase, estimate the next token from a fixed recent context.
An N-gram is a sequence of tokens. When predicting its final token, the model conditions on the preceding tokens:
- Unigram, : no preceding context; use overall token frequencies.
- Bigram, : use one preceding token.
- Trigram, : use two preceding tokens.
The predicted token is included in . A model using the previous two words is therefore a trigram, not a bigram.
Predict the next token with a trigram model
N = 3: use “the first” → predict “time”. Earlier tokens are outside the window.
For a token sequence , the probability chain rule is:
The N-gram assumption replaces the full prefix with the most recent tokens:
Near the beginning of a sentence, we must define how shorter contexts are handled. Practical models often use sentence-boundary markers. Our small experiment below omits them and counts only windows fully inside each sentence.
Train by counting, predict by looking up
Given a context , count how often each candidate token follows it. The basic maximum-likelihood estimate is:
The denominator counts all observed continuation events for that context. It is not necessarily every occurrence of the context anywhere: an occurrence at the end of a sentence has no following token unless an end marker is included.
Training a basic count model means extracting token windows and storing their frequencies. It does not require gradient descent or backpropagation.
| Next word | Count after “weather” | Probability |
|---|---|---|
| lovely | 2,300 | 23.0% |
| nice | 2,100 | 21.0% |
| forecast | 1,800 | 18.0% |
| changing | 1,500 | 15.0% |
| hot | 1,000 | 10.0% |
| banana | 3 | 0.03% |
| Other words combined | 1,297 | 13.0% |
| Total continuation events | 10,000 | 100% |
The candidate “banana” was observed three times out of 10,000 continuation events, so its probability is:
The frequency table is a conditional probability distribution. Choosing the most likely continuation and sampling from the distribution are different prediction strategies.
Try an actual count table
The following experiment counts windows in ten short English teaching sentences. Unlike the illustrative large counts above, every result here is derived from the displayed corpus.
Used context: . Earlier tokens are discarded.
Observed continuation events: · Vocabulary size: 12
Probability sum:
| Next word | Count | Probability |
|---|---|---|
| is | 4 | |
| forecast | 1 | |
| cat | 0 | |
| cold | 0 | |
| good | 0 | |
| kitten | 0 | |
| likes | 0 | |
| milk | 0 | |
| nice | 0 | |
| quiet | 0 | |
| the | 0 | |
| weather | 0 |
Ties use alphabetical order. This selects the maximum; it does not sample randomly.
Inspect the complete teaching corpus
- the weather is good
- the weather is good
- the weather is nice
- the weather is cold
- the weather forecast is good
- the cat is quiet
- the kitten is quiet
- the cat likes milk
- the kitten likes milk
- the forecast is cold
Words are lowercased and extracted from English letter sequences. Sentences are counted separately, with no start/end markers. This deliberately small tokenizer and corpus are educational examples.
Try these comparisons:
- With bigram, “the weather” uses only “weather”. The corpus has four continuations “is” and one “forecast”, giving probabilities and .
- With trigram, the same prefix uses “the weather”. This corpus happens to contain the same five events, so the result matches here. A larger context does not necessarily change every prediction.
- For unigram, the prefix is ignored and the distribution comes from overall word counts.
- “purple dragon” has no observed continuation. Without smoothing, the conditional estimate has a zero denominator and is undefined, rather than a normalised all-zero distribution.
3. Short memory and sparse data
Consider a sentence about a shy kitten that has never met a stranger. A completion far later in the sentence might depend on those earlier details. A short-context model cannot recover them once they leave its window.
Why not increase ? Larger windows distinguish more contexts, but exact combinations become rarer. There may be many perfectly sensible contexts that never appeared in the training corpus.
This is data sparsity:
- For a seen context, an unseen continuation receives probability zero under the unsmoothed frequency estimate.
- For an unseen context, the frequency estimate has no observed denominator.
- Longer exact contexts often have fewer examples supporting each estimate.
A single zero-probability event also makes an unsmoothed sequence probability zero when we multiply its conditional probabilities. “Not observed” does not mean “impossible in real language”.
Smoothing and backoff help, but do not solve everything
Add-one smoothing adds a count of one to every candidate in a fixed vocabulary of size :
This assigns positive probability to vocabulary words that were not observed after that context. For an entirely unseen context it produces a uniform distribution over that vocabulary. That distribution comes from the smoothing assumption, not new evidence.
Enable smoothing in the experiment and compare the results. On a tiny corpus, adding one everywhere can substantially flatten the distribution. It is easy to understand, but often too crude for serious language modelling.
Other methods include backoff, which uses shorter contexts when a longer context lacks evidence, and interpolation, which combines estimates from several orders. More sophisticated smoothing redistributes probability mass more carefully. These improve sparse estimates while retaining the limits of the chosen context representation.
Words outside a fixed vocabulary need their own treatment, such as an unknown-token symbol. Add-one smoothing over the current vocabulary does not automatically cover every possible new word.
4. What N-grams can and cannot do
Many nearby word combinations are regular. “weather forecast” is more common than “weather banana”, and counts can capture that difference cheaply.
Count-based language models have historically been useful for text suggestions, speech recognition and statistical machine translation. N-gram features can also be useful in text classification, including spam detection, even when that system is not itself generating language.
Their strengths include simple training, inspectable counts and useful local patterns. Their limitations include short context, sparse combinations and the lack of learned semantic relationships between distinct word symbols.
Tokenisation matters too. Word-level, character-level and other token choices produce different tables. Our experiment lowercases simple English word tokens, so its results should not be read as a general model of punctuation, multilingual text or sentence structure.
N-grams remain a useful baseline and teaching tool. They are not a claim that all contemporary language systems simply consult a small count table.
5. What you have discovered
A language model assigns probabilities to possible next tokens. An N-gram model estimates them from local counts.
- Order includes the predicted token, leaving context tokens.
- Training counts observed windows; prediction looks up a conditional distribution.
- Sparsity gives zero probabilities for unseen continuations and no raw estimate for entirely unseen contexts.
- Smoothing, backoff and interpolation provide ways to use limited evidence more effectively.
- A fixed context window ignores older information, and separate word symbols do not automatically share meaning.
The next lesson asks how to turn a word into a representation that can express its relationship with other words.