liveBook · Manning

Trigram model

As explained in Natural Language Processing in Action, Second Edition

A character trigram is a sequence of three consecutive characters or word-boundary characters extracted from text.

A character trigram is a sequence of three consecutive characters, including characters representing word boundaries. Comparing shared trigrams gives search systems a practical way to find approximate matches when exact words differ. section 10.3.1

Recovering a misspelled search

Someone searching for “Steven King” may miss results about “Stephen King” in an exact word index. A character-trigram index can share enough fragments between the spellings to retrieve and rank the relevant documents. section 10.3.1

Build a useful trigram representation by applying consistent tokenization rules:

  • Extract three-character windows from each word, adding boundary trigrams such as “ tr” and “am ” for “trigram.” section 10.3.1
  • Fold punctuation and whitespace into one boundary character so every tokenization step represents boundaries consistently. section 10.3.1
  • Configure TfidfVectorizer with character analysis and a trigram ngram_range when indexing text for typo-tolerant search. section 3.9

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains extraction, boundary normalization, shared-trigram similarity, and vectorizer configuration without developing the indexing implementation fully.
Examples
Uses the Stephen King misspelling and boundary-aware trigrams extracted from the word “trigram.”
What you can do after
Apply character trigrams through TfidfVectorizer to support typo-tolerant full-text search and relevance ranking.
How it connects
Connects character trigrams with tokenization, TF-IDF, inverted indices, stemming, lemmatization, and full-text search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.