AI Glossary / AI and machine learning / Beginner

Trigram

Definition

A trigram is a sequence of three consecutive units, usually words or characters. It captures a short piece of local order and can be used as a feature in text analysis or as context in a simple language model.

What it means

The word tri means three. If the units are words, the sentence please send the blue box contains the word trigrams please send the, send the blue and the blue box.

Character trigrams instead slide across characters. They can capture fragments of spelling and may help compare words with related forms. The choice of units must always be stated.

Trigrams preserve more local order than individual words, but they do not represent a complete sentence's meaning. A phrase can appear in very different contexts, and important dependencies may stretch far beyond three units.

For business readers, trigrams are a practical building block for search, matching and classification. They are also a useful way to understand why short phrase patterns can improve a simple model without giving it broad language understanding.

How it works

The text is first divided into units under a defined preprocessing rule. Word tokenization, case handling, punctuation and document boundaries all affect which trigrams exist.

A window of length three moves one position at a time. Each resulting sequence can be counted or represented as a feature. A collection of texts then provides frequencies for those patterns.

In a trigram language model, the next word is estimated using the preceding two words. Unseen or rare combinations require handling such as smoothing or backing off to shorter contexts.

Evaluation depends on the application. A phrase feature may improve document classification, while a character trigram representation may help approximate matching. Neither use guarantees that the system understands negation, intent or the broader document context.

Three practical examples

  • Search: phrase features help distinguish short ordered expressions from the same words appearing separately.
  • Text classification: recurring three-word patterns can provide evidence for topics or request types.
  • Approximate matching: character trigrams help compare names or product descriptions with spelling variations, followed by a more careful check.

Think of it this way

Think of viewing a long sentence through a window that shows three words at a time. You see local phrases, but not the whole story in one view.

The analogy has a limit. A statistical model can combine many windows, while a human may remember the entire sentence. The three-unit pattern itself still contains only limited context, and its usefulness depends on how the larger system uses it.

Worked example

Counting word trigrams

Take the invented phrase we need a larger delivery van. It contains six word tokens: we, need, a, larger, delivery and van. Spaces separate the units in this simple example; they are not counted as word tokens themselves.

Using the six tokens as written, there are six minus three plus one, or four trigrams: we need a; need a larger; a larger delivery; larger delivery van.

Adding a punctuation token would change the count if the tokenization policy treats it as a separate unit. Adding boundary markers could also create additional sequences.

The lesson is to define and inspect the tokens before counting features. A formula is only as useful as the representation supplied to it. These examples are deliberately simple enough to check by hand.

Illustrative case study

A fictional catalog team compares product names using character trigrams. It retrieves plausible matches despite minor spelling differences, but also finds unrelated items sharing common fragments.

The team treats trigram similarity as a candidate-generation step, then checks product attributes such as size, material and manufacturer. It evaluates known difficult matches and false positives rather than assuming a high string-similarity score proves identity.

The lesson is that local text overlap can be useful evidence without being a final decision. Matching systems often need structured information and explicit rules about what counts as the same item.

Common misconceptions and mistakes

  • A trigram always means three words. Character and other unit choices are also possible.
  • Trigrams preserve complete meaning. They retain only short-range order and can miss wider context.
  • More phrase features always improve a model. Rare trigrams can expand the feature space substantially without providing reliable evidence.

Business use

Trigrams offer an interpretable feature for some text tasks and a simple baseline for more complex language systems. They can help teams see which short phrases influence a result.

Document tokenization and preprocessing so training and live use remain consistent. Compare word and character representations where appropriate, and test whether extra phrase features improve the actual task. For matching, report missed matches and false candidates separately from computational speed.

People also ask

How many trigrams are in a sequence of n units?

Without added boundaries, a sequence of at least three units has n minus two contiguous trigrams.

Is a trigram the same as a skip-gram?

No. An ordinary trigram uses consecutive units; skip-grams allow gaps under specified rules.

Are modern language models limited to trigrams?

No. Many use much longer context, although trigram features and models remain useful for teaching and some practical baselines.

Check your understanding

A five-word phrase has no added boundary markers. How many contiguous word trigrams does it contain?

Answer: Three. The windows start at the first, second and third words, each covering three consecutive words.

Related terms

  • N-gram
  • Bigram
  • Token
  • Skip-gram
  • Bag of words
  • Language model

Learn next

Read n-gram and token. Write out a short sentence's tokens, list its trigrams and compare what information is retained versus a bag-of-words representation.

  • Bag of words
  • N-gram
  • Token

More in AI and machine learning

  • Transduction - Transduction is learning or inference directed at a particular supplied collection of cases, rather than producing a general prediction rule for arbitrary...
  • Transfer learning - Transfer learning reuses knowledge learned in one task or setting to help solve another.
  • Tree-based feature selection - Tree-based feature selection uses a fitted decision tree or tree ensemble to help choose which input features a model should retain.
  • Triplet loss - Triplet loss is a training objective that encourages an anchor example to be closer to a related positive example than to an unrelated negative example by a...
  • Tsetlin machine - A Tsetlin machine is a learning model that uses teams of Tsetlin automata to construct propositional-logic clauses.
  • Tucker decomposition - Tucker decomposition represents a multiway array, or tensor, using a smaller core tensor and one factor matrix for each mode.

Sources

Google, Machine Learning Glossary, trigram and n-gram terminology: https://developers.google.com/machine-learning/glossary. scikit-learn, Text feature extraction: https://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction. Primary references consulted; examples are original.

Previous: Trifocal tensor Next: Trigram search

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer