AI GLOSSARY · AI CONCEPTS

N-gram

Markdown version · Wikidata Q94489

Definition

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.

Worked examples in this catalogue

Catalogue examples point at a skill, guide or coding tool listed on this site, with what to run and what you see. An example marked illustrative describes the item from its own documentation; it was not executed for this page. Platform Scores are our assessment under a published method, not a safety guarantee.

  1. Skill by Skill Almanac · Illustrative, not executed

    keyword-density-count 41/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  2. Skill by Skill Almanac · Illustrative, not executed

    stock-phrase-scanner 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  3. Guide · Illustrative, not executed

    What Is an LLM? The Facts Agent Users Need

    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.

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 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, 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 page.
  • Unseen sequences. A sequence missing from the training text has a zero count, which is why smoothing methods exist; NLTK lists several.

Terms that mention N-gram

5 terms mention this one

More in AI concepts

Next steps

  1. What Is an LLM? The Facts Agent Users Need The long-form guide.
  2. keyword-density-count A skill that exercises it.
  3. Language model Related term.
  4. Tokenization (NLP) Related term.
  5. All glossary terms A to Z and by family.

Sources

Definitions are written by Skill Almanac from these sources and were checked against them on 2026-11-25. Wikipedia is never the only source.

Review record

Last verified

Drafted by claude-sonnet-5-5; sources and example targets checked by Independent automated source review (OpenAI Codex); not human peer review on 2026-11-25. Last verified .

Change log (5)
  • : Independent automated factual/example and synonym review completed; cached US query estimate aligned to receipt. Final technical gate separate; publication disabled.
  • : First version.
  • : Verifier pass: sourced the plain-text uses, quoted Stanford on sparsity, cut an unsourced confusion line and trimmed an example.
  • : OpenAI5.6/Codex revision: Defines n-grams by units, separates order from input length and puts the Stanford sparsity quotation back in its sample-matrix context. Independent re-review pending.
  • : Automated review completed; approved for the staged public release. Search indexing is controlled by the release configuration.