Trigram Tokenization: What It Changes and When It Fits

A practical review of trigram tokenization: how character and word trigrams are built, what they do and don't change in your text, Unicode pitfalls, and when another approach suits better.

A trigram tokenizer splits text into overlapping sequences of three units. It is mainly used for fuzzy search, typo-tolerant matching, language identification and simple statistical language models. This review explains what the process produces, where it helps, where it falls short, and why the trigrams themselves are never a substitute for the original text.

Is trigram tokenization suitable for your use?

It suits matching and analysis tasks, not presentation. The main benefit is tolerance: two strings that share most of their three-character sequences can be recognized as similar even when they contain typos, inflections or partial words. It also works without knowing where words begin and end, which helps with partial-string search and with scripts that don't separate words with spaces.

The key tradeoff is precision versus recall. Trigrams find near matches that whole-word tokenization would miss, but they also produce false matches between unrelated strings that happen to share character sequences. They capture no meaning, so synonyms with different spellings do not match. Trigram indexes are also generally larger than word-level indexes, because each word yields several entries.

  • Trigrams tend to fit when:
  • users misspell names, product codes or search terms
  • you need substring or "contains" search over short-to-medium text fields
  • you want rough language detection from character patterns
  • word boundaries are unreliable or absent
  • A plain word tokenizer, or no tokenization at all, is usually preferable when:
  • exact keyword or identifier matching is required
  • queries are very short (one or two characters yield few or no complete trigrams)
  • results must reflect meaning rather than spelling
  • the text is being displayed, shared or read by people

What trigram tokenization actually changes

Tokenization does not alter the stored text. It produces a separate, derived set of tokens, usually kept in an index or used in a calculation, while the original string stays as it was. This sets it apart from transformations such as character reversal or Unicode font substitution, which replace the characters themselves.

"Trigram" can mean two different things, so check which one a tool uses. Character trigrams are three consecutive characters. Word trigrams are three consecutive words, used mainly in n-gram language models. A third, unrelated meaning is the set of eight Yijing trigram symbols encoded in Unicode (☰ through ☷). Those are display characters, not a tokenization method.

Character trigrams slide a three-character window along the text, one position at a time. Without padding, a string of n characters yields n − 2 trigrams:

input:     "search"
trigrams:  sea  ear  arc  rch

Many implementations pad each word so that its first and last letters appear in more trigrams. This gives more weight to word beginnings and endings. PostgreSQL's pg_trgm extension, for example, documents that it lowercases text, ignores non-alphanumeric characters, and pads each word with two spaces before and one after:

input:     "cat"
trigrams:  "  c"  " ca"  "cat"  "at "

Word trigrams apply the same sliding window to words:

input:     "the quick brown fox"
trigrams:  [the quick brown]  [quick brown fox]

Similarity between two strings is then typically calculated from the trigrams they share relative to the total set of distinct trigrams in both. Implementations differ in their padding, case folding, punctuation handling, which characters count as part of a word, and the formula used, so results from one tool should not be assumed to carry over to another.

Unicode pitfalls

Many implementations count Unicode code points or bytes rather than user-perceived characters. Text containing combining marks or multi-code-point emoji then splits at unexpected places. An accented letter written as a base letter plus a combining accent occupies two positions, so a trigram can contain a lone accent. The same visible word may also tokenize differently depending on whether it was stored in composed or decomposed form.

"café" with é as e + U+0301 (combining acute accent)
code points:  c  a  f  e  ◌́
trigrams:     caf  afe  fe◌́

"café" with é as the single code point U+00E9
code points:  c  a  f  é
trigrams:     caf  afé

Emoji built from several code points, such as family or flag sequences, can be cut into fragments with no meaning on their own. If your text includes accented scripts or emoji, normalize it to a single Unicode form before tokenizing, and check whether the tool works on grapheme clusters or on raw code points. Whether a given tool treats non-Latin letters as word characters may also depend on its configuration or locale; check its documentation rather than assuming.

Readability and usability after copying or sharing

Trigram output is an internal representation, not readable text. A list of overlapping fragments loses normal reading order, repeats letters, and may include padding spaces. If copied into a document or message, it would be confusing to readers and to screen readers, which would announce each fragment separately. Always keep the original text alongside any trigram data and show people the original.

For search, the original text is not affected: users type ordinary queries, and the system tokenizes both query and content behind the scenes. Editing is also unaffected, but the trigram index must be regenerated or updated whenever the source text changes, or matches will be stale. Exported trigram lists are only portable between tools that use identical rules for padding, case and character handling.

Evidence gaps

No tool-specific measurements, benchmarks or test results were available for this review. Claims about index size, speed, matching quality or screen reader behavior are therefore general principles, not verified observations of any particular product. Before relying on a specific trigram tokenizer, test it on a sample of your own text, including short queries, typos, accented characters and emoji. Also review its documentation for its exact tokenization rules and for how it handles your data.