N-gram
An n-gram is a contiguous sequence of n consecutive items, commonly linguistic tokens or characters, drawn from a larger sequence.
Natural Language Processing in Action, Second Edition
An n-gram is a contiguous sequence of up to n extracted elements, such as words, characters, syllables, or symbols.
- How it is used
- Uses n-grams to preserve local word order in search vectors and represent unfamiliar names through character fragments.
- How deep it goes
- Defines n-grams and demonstrates vectorizer settings and feature counts, while leaving informative n-gram selection for later treatment.
- Examples
- Tests “algorithmic bias” as a search bigram and character fragments as features of baby names.
- What you can do after
- Apply it via scikit-learn CountVectorizer or TfidfVectorizer to configure word or character n-gram features.
Read how Natural Language Processing in Action, Second Edition explains n-gram →
LLMs in Production
An n-gram is a contiguous sequence of n linguistic units, commonly used to model local patterns in text.
- How it is used
- Uses overlapping word sequences to evaluate whether generated summaries resemble reference summaries without demanding exact matches.
- How deep it goes
- Defines overlap-based scoring and distinguishes n-size variants, longest common subsequences, and skip-bigrams.
- Examples
- Uses paired Zelda summaries and Python calculations of ROUGE-1 and ROUGE-L scores.
- What you can do after
- Evaluate summary similarity with the Python rouge-score library using overlap-based metrics.
Ensemble Methods for Machine Learning
An n-gram is a contiguous sequence of n successive tokens, such as characters or words, extracted from a string.
- How it is used
- Uses n-grams to compare longer string categories and encode high-cardinality or noisy categorical features.
- How deep it goes
- Defines n-grams and places them in token-level similarity, without detailing extraction or comparison algorithms.
- Examples
- Uses employee-position titles, substring patterns, MinHashEncoder, and GapEncoder as practical examples.
- What you can do after
- Apply n-gram-based similarity through specialized encoders for noisy or high-cardinality string categories.
Read how Ensemble Methods for Machine Learning explains n-gram →
Elasticsearch in Action, Second Edition
An n-gram is a contiguous sequence of a specified number of characters or words derived from text.
- How it is used
- Supports typeahead suggestions, spelling correction, and matching broken words through overlapping character or word sequences.
- How deep it goes
- Explains character windows, edge anchoring, word shingles, tokenizer configuration, and analysis output without covering broader relevance tuning.
- Examples
- Uses “coffee” and “Bond” for character tokens, plus book titles for word shingles and typeahead search.
- What you can do after
- Apply it via Elasticsearch tokenizers and search_as_you_type mappings to inspect tokens and implement prefix-oriented search.
Read how Elasticsearch in Action, Second Edition explains n-gram →
Related concepts
- bigramincludes · A bigram is an ordered sequence of two consecutive items, such as tokens, words, or symbols, treated as a unit.
- sequence modelkind of · A sequence model processes ordered data while accounting for dependencies among its positions to produce predictions or representations.
- BLEU scoreused by · BLEU is an automatic text-generation evaluation metric that compares candidate text with reference text using modified n-gram precision and a brevity penalty.
- trigram modelincludes · A trigram model represents or predicts sequences using groups of three consecutive units, such as characters or tokens.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



