# N-gram

> Canonical: https://skillalmanac.com/glossary/n-gram | Last verified: 2026-11-25 | Index status: index-eligible

An n-gram is a sequence of n consecutive units, such as words, tokens or characters. An n-gram language model uses nearby preceding units to predict the next one.

Also called: n-grams, ngram, ngrams.

## In plain language

An n-gram is a run of neighbouring units, often words or characters. For word n-grams, 'the water' is a two-word run and 'the water of' is a three-word run. Counting such patterns can help estimate what word comes next. In a simple order-n word model, prediction uses at most n-1 preceding words; n does not describe the total input length. Counts alone can assign zero probability to unseen sequences, so practical models need ways to handle those cases.

## Technical definition

In Jurafsky and Martin's textbook, an n-gram is a sequence of n words, and the word is also used for a probabilistic model that estimates the probability of a word given the n-1 previous words, and so assigns probabilities to whole sequences. Probabilities are estimated from counts in a training text; sequences absent from it need smoothing or backoff, and the NLTK language-model module provides classes for several such methods. In scikit-learn, the ngram_range parameter of CountVectorizer extracts word or character n-grams as features.

## How it shows up when you use agents

You often do not meet n-grams inside a modern coding agent, whose model is neural. You meet them in the tools around it: a search or text-analysis script that counts two-word phrases, a classifier whose features are word pairs, or a duplicate-text check. If an agent writes such a script with scikit-learn, the ngram_range setting decides what is counted. The scikit-learn page says (1, 1) means only unigrams, (1, 2) unigrams and bigrams, and (2, 2) only bigrams.

What breaks when this is misunderstood: increasing the model order uses more immediately preceding units—n-1 for next-unit prediction—but does not create an unrestricted context window. Counts also become sparse. Stanford's quoted statement that "the majority of the values are zero" describes a particular sample matrix of bigram counts for eight selected words from the Berkeley Restaurant Project corpus, not a universal measured percentage for every n-gram table. CountVectorizer separately creates a sparse document-by-feature matrix whose width depends on the extracted vocabulary. Start with a narrow ngram_range, inspect the resulting features and widen it only when useful.

A basic count-based n-gram model is one simple [language-model](/glossary/language-model) approach, and the Stanford chapter uses it to introduce ideas that larger models share, including training and test sets and sampling. Text must be divided into units before counting. Those units may be words or subword tokens chosen by [tokenization](/glossary/tokenization), while a character analyzer can instead form n-grams directly from characters.

## Common confusions

- **N-gram versus n-gram model.** The textbook notes the same word is used for the sequence itself and for the probability model built from counts of such sequences.
- **Word n-grams versus character n-grams.** scikit-learn can extract either.
- **N-gram model versus large language model.** Both predict upcoming words. An n-gram model works from counts over the previous n-1 words; large models are neural and are described on the [language model](/glossary/language-model) page.
- **Unseen sequences.** A sequence missing from the training text has a zero count, which is why smoothing methods exist; NLTK lists several.

## Worked examples in this catalogue

### [keyword-density-count](https://skillalmanac.com/skills/skill-almanac/keyword-density-count)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Counts how often each keyword or phrase appears in plain page text and reports case-insensitive, whole-word counts.

Why it exercises the concept: Counting phrase occurrences is the operation an n-gram model starts from. The skill reports counts for phrases you give it; it does not build a language model.

### [stock-phrase-scanner](https://skillalmanac.com/skills/skill-almanac/stock-phrase-scanner)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Scans a plain-text draft for stock filler phrases, hedges and repeated sentence openers, reporting each hit with its line numbers.

Why it exercises the concept: Repeated openers and phrases are repeated word sequences, which is what n-gram counts surface.

### [What Is an LLM? The Facts Agent Users Need](https://skillalmanac.com/learn/what-is-an-llm-for-agent-users)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A guide explaining that an LLM predicts text one token at a time inside a fixed context window.

Why it exercises the concept: The Stanford chapter says large models are trained to predict upcoming words, as n-gram models are, but they are neural and not count-based. The guide covers the neural case.

## Related terms

- [Language model](https://skillalmanac.com/glossary/language-model.md)
- [Natural language processing (NLP)](https://skillalmanac.com/glossary/natural-language-processing.md)
- [Tokenization (NLP)](https://skillalmanac.com/glossary/tokenization.md)
- [Word embedding](https://skillalmanac.com/glossary/word-embedding.md)
- [BM25](https://skillalmanac.com/glossary/bm25.md)

## Sources

- [Speech and Language Processing, chapter 3: N-gram Language Models (Jurafsky and Martin, draft of August 19, 2026)](https://web.stanford.edu/~jurafsky/slp3/3.pdf), Stanford University (accessed 2026-11-25)
- [CountVectorizer (parameters stop_words, ngram_range)](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html), scikit-learn (accessed 2026-11-25)
- [nltk.lm module (n-gram language models)](https://www.nltk.org/api/nltk.lm.html), NLTK Project (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
