AI Glossary / AI and machine learning / Intermediate
N-gram
Definition
An N-gram is a sequence of N consecutive items from text, usually words: 'the cat' is a bigram, 'the cat sat' a trigram, and counting such sequences is the oldest way to give language a numeric shape.
What it means
Before neural networks, N-grams were language modeling: count every word pair and triple in a corpus, and you can estimate what word likely follows, what phrase is common, and how two documents differ.
The idea is deliberately shallow. N-grams know nothing about meaning, grammar or context beyond the window, yet they capture a surprising amount, because so much of language is habit and collocation.
The count is the model: how often each sequence appears becomes a probability table, and larger N means more context, sparser counts and sharper guesses, a trade that runs straight through all of NLP.
N-grams still earn their keep today: as quick features for classification, as baselines any fancier model must beat, and inside the tokenization and evaluation machinery of modern systems.
For a business, the N-gram lens is the first diagnostic on text data: the most common phrases in complaints, reviews or tickets, ranked by frequency, is an analysis any team can run in an afternoon.
The same idea applies beyond words: character N-grams power language identification and fuzzy matching, and token N-grams still appear inside modern text evaluation.
How it works
Choose N deliberately: unigrams for vocabulary, bigrams and trigrams for phrasing, and rarely more, since counts thin out fast.
Count the N-grams across the corpus, keeping the most frequent as candidate features or vocabulary.
Convert counts to features or probabilities: raw frequencies, normalized rates, or presence-absence flags per document.
Use the features for classification, similarity or language identification, or as the baseline a neural model must beat.
Filter with care: stop words, rare sequences and spelling variants decide whether the counts signal or just sprawl.
Three practical examples
- A churn study finding 'cancel my account' and 'speak to a manager' among the top trigrams in exit-survey text.
- A spam filter using bigram counts, where 'free prize' and 'claim now' carry the signal whole paragraphs would blur.
- A review dashboard ranking bigrams weekly, catching 'delivery late' climbing before the ratings move.
Think of it this way
Think of reading a language through a tiny window: you see only a few words at a time, but slide the window across millions of pages and the patterns of habit become statistics.
Worked example
the bigram that rang the bell
Suppose a retailer counts bigrams in product reviews monthly. The figures are illustrative.
In March, 'zip broke' appears in 0.4 per cent of reviews for one jacket line; by May it reaches 3 per cent, while average ratings have barely moved.
The bigram triggers a quality review two months before the rating averages would have, and the batch is traced to one supplier's zipper change.
The lesson is the window's reach: one pair of words, counted over time, outperformed the summary statistics the team had been watching.
Illustrative case study
A fictional bank built an N-gram sentiment scorer on English reviews and applied it unchanged to its Gulf customer base, where mixed Arabic-English phrasing made the counts meaningless.
Positive-sounding scores flowed from text the model could not parse, and the dashboard reported calm while complaints grew.
The invented lesson: N-grams count the surface of a language. Change the language, the dialect or the script, and every count must be rebuilt, not adjusted.
Common misconceptions and mistakes
- N-grams are obsolete. They are limited, not dead: as features, baselines and diagnostics they remain the fastest first look at text.
- Bigger N is always better. Counts thin exponentially with N, so trigrams are usually the practical ceiling for useful statistics.
- They understand phrases. They count them; 'not good' and 'good, not great' are just overlapping pairs to a counter.
Business use
Run an N-gram frequency review on any text collection before choosing tools; the common phrases tell you what the data is about.
Use bigram and trigram features as the baseline any expensive text model must beat, and keep them if it cannot.
Rebuild counts when the language mix or channel changes, since N-gram statistics belong to a specific text population.
Watch phrase trends over time as an early-warning channel, ahead of averages and ratings.
Export the top N-grams per segment as a plain spreadsheet for domain teams; the ranking needs no explanation to be acted on.
People also ask
What is an N-gram?
A run of N consecutive items, usually words, used to turn text into countable, comparable pieces.
What are bigrams and trigrams good for?
Capturing phrasing and collocation: the fixed expressions where meaning lives beyond single words.
Why are N-grams still used?
They are cheap, transparent and strong baselines for classification, similarity and trend watching.
What is their main weakness?
No meaning or long context: they count surface sequences, and their counts thin out fast as N grows.
How do N-grams relate to language models?
N-gram models were the original language models; neural models replaced them by learning from far more context, but the counting idea still underpins evaluation and baselines.
Check your understanding
Why do N-gram counts thin out so quickly as N grows?
Answer: Because the number of possible sequences grows explosively with N, so any fixed corpus sees most long sequences once or never, leaving too little evidence for stable estimates beyond trigrams in most collections.
Related terms
Learn next
Be Productive Using AI shows how quick text diagnostics sharpen any AI-assisted writing workflow: https://aiforbusinessandlife.com/academy/be-productive-using-ai. [C1]
More in AI and machine learning
- Multispectral pattern recognition - Multispectral pattern recognition identifies or groups objects and regions using measurements from several wavelength bands.
- Multitask - Multitask describes learning several related tasks together in one model: one representation, several output heads, so each task borrows evidence from the...
- Multivariate adaptive regression spline - A multivariate adaptive regression spline model represents a response as a weighted combination of simple piecewise functions chosen from the data.
- Native-language identification - Native-language identification is the task of estimating a writer's or speaker's first-language background from language they produce, often in a second...
- Natural language understanding - Natural language understanding is the branch of NLP concerned with machine comprehension: turning what people write or say into structured meaning a system...
- Nearest Centroid Classifier - Nearest Centroid Classifier is one of the simplest classifiers that exists: compute the average example of each class, the centroid, and assign any new item...
Sources
[S8] Jurafsky and Martin, Speech and Language Processing (3rd ed. draft); [S2] Google, Machine Learning Glossary. Full URLs are in the source ledger. Verified 11 October 2026.
Previous: n-best list Next: n-gram model