# N-gram

> A run of n words in a row, like 'green tea' for n = 2.

**Field:** Artificial Intelligence  
**Category:** NLP & Language  
**Also known as:** Bigram, Trigram, N-gram Model

## Think of it like

Think of it like you look at a line of words through a small hole. Then you slide it along.

## Definition

An n-gram is a run of n words that sit side by side in a piece of text. A bigram is two words, like 'green tea', and a trigram is three. An n-gram model counts how often each run shows up in a corpus. Then it guesses the next word from the last few words alone. These were the main language models before neural networks, and they are still used for quick tasks like spotting phrases.

## Example

From 'I love green tea', the bigrams are 'I love', 'love green' and 'green tea'. A trigram model would see 'love green' and guess 'tea'.

## Why it matters

N-grams are the simplest way to see how a computer can guess words from counts alone.

## Common mistake

Thinking a bigger n is always better. Long runs are rare, so most of them never show up in the corpus at all.

## Three principles

- **n words in a row.** Two is a bigram, three a trigram.
- **Counted in a corpus.** How often each run appears.
- **Short memory.** Only the last few words count.

## Related terms

- [Language Model](https://learn.benibix.com/term/ai/language-model)
- [Bag of Words](https://learn.benibix.com/term/ai/bag-of-words)
- [Corpus](https://learn.benibix.com/term/ai/corpus)

![The sentence I love green tea, read three ways. One word at a time, then in pairs such as love green, then in threes such as love green tea.](https://learn.benibix.com/diagrams/nlp-ngram.svg)

---

Source: https://learn.benibix.com/term/ai/n-gram
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-18
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
