N-gram tokenizers
What an n-gram tokenizer does to text, when character, word or edge n-grams suit search and analysis, and when a plain word tokenizer is the better choice.
An n-gram tokenizer splits text into overlapping sequences of a fixed length, called n-grams. It is common in search indexing, autocomplete, spelling tolerance and text analysis. This review explains what the transformation changes, where it helps, where it causes trouble, and when a plain word tokenizer is the better choice. It describes general, documented behavior of the technique. No specific tool was tested for this article, and no performance measurements are reported.
Is an n-gram tokenizer suitable for your use?
The main benefit is partial matching. Text is broken into small overlapping pieces, so a query can match the middle or start of a word, a misspelled word, or text in a language that does not separate words with spaces. The main cost is volume and noise. One word produces many tokens, so an index grows, and short fragments match many unrelated words, which can lower result precision unless relevance scoring or a minimum length limits it.
Overall, n-gram tokenization works well for search-as-you-type, fuzzy lookup over short fields such as names, product codes or titles, and languages written without word boundaries. A plain word tokenizer is usually the better choice when users search for whole words, when exact or phrase matching matters more than tolerance, when the text is long and index size is a concern, or when the output has to be read by people.
What the transformation actually changes
An n-gram tokenizer does not edit the source text. It produces a derived list of tokens, usually stored in a search index or passed to an analysis step, while the original text stays as it was. This differs from transformations such as reversed text or Unicode "font" styling, which change the characters a reader sees and copies. N-grams are a machine-facing representation. They are not a display format.
The term "n-gram" covers several variants, and documentation does not always say which one is meant:
- Character n-grams: overlapping runs of n characters within the text or within each word.
- Word n-grams (sometimes called shingles): overlapping runs of n consecutive words, used for phrase matching and language modeling.
- Edge n-grams: prefixes anchored to the start of a word or string, mainly used for autocomplete.
- The value of n may be a single number or a range, for example every length from 2 to 3.
A unit sequence of length L yields L − n + 1 n-grams of length n, as long as L is at least n. The following outputs are worked examples that follow directly from that definition. They are not output captured from a specific product.
Input word: quick
Character bigrams (n=2): qu ui ic ck
Character trigrams (n=3): qui uic ick
Edge n-grams (1 to 3): q qu qui
Input phrase: the quick brown fox
Word bigrams (n=2): the quick | quick brown | brown fox
Implementations also differ in details that change the output. Some split on whitespace or punctuation before generating n-grams and some do not. Some lowercase the text first. Some include or exclude spaces inside character n-grams. Check the documentation of the tool you use, because these choices affect both matching and index size.
Combining marks and multi-code-point emoji
What counts as "one character" matters. A tokenizer may count UTF-16 code units, Unicode code points or user-perceived characters (grapheme clusters). Many visible characters are made of more than one code point. An accented letter can be stored as a base letter followed by a combining mark, and many emoji, such as family or skin-tone sequences, are several code points joined together.
"é" in decomposed form = U+0065 (e) + U+0301 (combining acute accent)
Code-point unigrams: "e" and a lone combining accent
Grapheme unigram: "é"
If n-grams are cut by code point or code unit, they can separate a mark from its letter or split an emoji sequence into fragments. These fragments then fail to match the composed form a user types. Two practical safeguards are normalizing text to a consistent Unicode form, such as NFC, before tokenizing, and checking whether your tool's documentation says it respects grapheme boundaries. If it is unclear, index a few test strings with accents and compound emoji and inspect the resulting tokens before relying on the behavior.
Will the result remain readable and usable?
Because the source text is not modified, copying, sharing, screen reader output and editing of the original are unaffected. Problems appear only when n-gram output is shown to people or used as if it were text.
- Readability: a list of fragments such as "qu ui ic ck" is not readable prose. Show the original text to users and keep tokens for debugging or analysis views.
- Assistive technology: a screen reader given n-gram output would announce fragments rather than words. Any user-facing interface should present the original text. No specific screen reader was tested for this article.
- Copy and paste: copying tokens gives fragments, not the source. Store and expose the original text wherever copying or reuse is expected.
- Search highlighting: when a match comes from a partial n-gram, highlighting may mark part of a word or an unexpected word. Review how your search tool maps n-gram hits back to the original text.
- Editing: tokens cannot be edited and turned back into text. Changes must be made to the source, which is then re-tokenized.
Keep a plain-text version whenever the output leaves the indexing or analysis pipeline. In most setups that version is simply the original field, stored next to the index.
Quick comparison
| Approach | Good for | Main tradeoff |
| Word tokenizer | Whole-word search, phrase queries, readable terms | Misses partial words and typos |
| Character n-grams | Substring and typo-tolerant matching, unsegmented languages | Larger index, noisier matches |
| Edge n-grams | Autocomplete and prefix search | Matches only the start of words |
| Word n-grams (shingles) | Phrase relevance, language modeling | More tokens per sentence, little help with typos |
Gaps in this review
This article relies on how the technique is defined, not on tests of a particular tokenizer. It makes no claims about specific products' defaults, speed, index size, privacy or data handling. To judge a specific implementation, consult its documentation and run a small test with the n range you plan to use, with accented and emoji-containing text, and with real queries from your users.