- Type
- Statistical language model
What does it do?
An n-gram language model is a statistical model that estimates the probability of the next word from only the previous n−1 words, a Markov assumption over a fixed window.
Jurafsky and Martin's textbook *Speech and Language Processing* describes the model in detail, and it is used for text generation, spelling correction, and speech recognition.
The intuition behind the method
Language is sequential: the words you have just seen strongly constrain what comes next. Rather than reasoning over an entire sentence at once, an n-gram model makes a simplifying bet — that only the last handful of words matter for predicting the next one. It counts how often word sequences appear in a training corpus and turns those counts into probabilities, so a phrase like 'thank you very' makes 'much' a likely continuation.
History
The n-gram approach is a long-standing part of statistical natural-language processing, covered in Jurafsky and Martin's *Speech and Language Processing*. Smoothing has been a central practical problem for the model; Chen and Goodman's 1999 empirical study of smoothing techniques for language modeling addresses this comparison. N-gram models were a standard approach before neural language models became dominant.
When to use it
Choose an n-gram model when your task is to score or generate word sequences, for example autocompletion, spelling correction, or a transparent baseline language model, and when you have a text corpus that matches the domain you care about. It is a good fit when you need probabilities that can be traced back to observed counts. Do not rely on it when the dependencies you need span more words than the chosen window, such as agreement or topic carried across a paragraph, or when the training text is too small to give reliable counts even after smoothing. Neural language models that learn distributed representations are the usual alternative for those cases.
Limitations and considerations
- The Markov assumption means only the previous n−1 words affect the prediction, so longer-range dependencies are invisible to the model.
- Most word sequences never occur in a finite corpus, so smoothing is required and the chosen smoothing method changes the predicted probabilities.
- The number of stored n-grams grows with the number of distinct sequences in the corpus, and higher-order counts become sparser as n increases.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Each probability is a ratio of counts from the corpus, so any prediction can be traced back to the observed word sequences behind it.
- Scoring a sequence requires only looking up stored counts and combining them, with no network to evaluate.
- The main design choice, the order n, is a single parameter that is easy to inspect and vary.
Applications
- Autocompletion and next-word suggestion, where the model ranks likely continuations of a typed prefix.
- Spelling and grammar correction, where candidate corrections are ranked by the probability of the resulting word sequence.
- Speech recognition, where a language model scores competing transcription hypotheses.
- Text generation from a learned word-sequence distribution, and as a baseline for comparing neural language models.
How it works
How it works
The training corpus is segmented into tokens and sentences are padded so that beginnings and ends of sequences are modelled explicitly. Every overlapping window of n consecutive tokens becomes one n-gram, which is the unit the model will count and score.
The model counts how often each n-gram and its (n−1)-word prefix occur in the corpus. Dividing the n-gram count by its prefix count gives the conditional probability of the final word given the preceding n−1 words — the core estimate of the model.
Because most possible word sequences never appear in any finite corpus, raw counts assign zero probability to unseen n-grams (the sparsity problem). Smoothing methods such as Laplace add-one or Kneser-Ney redistribute probability mass so that unseen but plausible sequences receive a small non-zero probability. This step is essential — without it the model breaks on real text.
Given a context of n−1 words, the model returns a probability distribution over the next word, from which the most likely continuation can be chosen or text can be generated. The same machinery scores how probable an observed sentence is, which is what powers spelling correction and speech recognition.
Common pitfalls
- Skipping smoothing leaves every unseen n-gram at zero probability, so any sentence containing a new word sequence receives probability zero.
- Training on a small corpus gives many n-grams counts based on only a few occurrences, which makes their probability estimates unstable.
- Choosing n too large makes most prefix contexts unseen, while choosing n too small discards context the model could have used.
- Failing to pad sentence boundaries means the model cannot estimate how sequences begin and end.
- Words that never appear in the training corpus have no counts. Map them to an explicit unknown-word token or fix the vocabulary in advance; otherwise their probabilities are undefined and sentences containing them cannot be scored.
- Scoring the model on its own training text overstates its quality, because the counts were collected from that text. Use held-out data for evaluation.
Frequently asked questions
What does the 'n' in n-gram mean?
It is the number of consecutive words considered together. A bigram (n=2) conditions the next word on the single previous word, a trigram (n=3) on the previous two. Larger n captures more context but needs far more training data and becomes sparse.
Why is smoothing necessary?
A finite corpus can never contain every plausible word sequence, so many valid n-grams have a count of zero and would receive zero probability. Smoothing methods such as Laplace add-one or Kneser-Ney reassign a small probability to unseen sequences so the model does not break on new text.
How much training data do I need?
There is no universal minimum. The amount needed depends on your domain and on the order n. More text gives more reliable counts, and higher-order n-grams need substantially more data to avoid sparsity. Check that the n-grams you care about occur often enough in your corpus to be estimated reliably.
How does an n-gram model differ from a neural language model?
An n-gram model estimates next-word probabilities directly from counts over a fixed window of previous words, making it simple, fast, and interpretable. Neural models learn distributed representations and capture longer context, at higher computational cost. N-grams remain a useful transparent baseline.
How do I tell whether my model is good?
Measure it on held-out text that was not used to collect the counts. Perplexity on that text is the usual measure: lower perplexity means the model predicts the held-out words better within the same domain. Scores on the training text are not informative, because the counts were built from it.
Related methods
Sources
Jurafsky, D. & Martin, J.H. (2023). Speech and Language Processing, 3rd ed.
Open publication referenceChen, S.F. & Goodman, J. (1999). An Empirical Study of Smoothing Techniques for Language Modeling. Computer Speech & Language, 13(4), 359-394.
Open DOI reference