liveBook · Manning

Bag-of-n-grams

As explained in Natural Language Processing in Action, Second Edition

Bag-of-n-grams is a text representation that records occurrences of contiguous token sequences of a specified length while disregarding their order in the document.

Bag-of-n-grams represents a document with counts of contiguous token sequences: pairs are bigrams, triplets are trigrams, and larger values follow the same pattern. Unlike single-token counts, these features preserve local word combinations while still discarding the document’s broader word order. section 3.0

Choose the sequence length through the ngram_range hyperparameter. Combining 1-grams and 2-grams creates a vector containing individual tokens alongside phrases, often capturing a more useful match than either representation alone. section 3.4

Capturing “algorithmic bias”

With CountVectorizer(ngram_range=(1, 2)), a small corpus becomes a 16-by-616 sparse matrix. Inspecting the feature named “algorithmic bias” shows nonzero counts in documents containing that phrase, while cosine similarity can rank a matching document for the query “What is algorithmic bias?” section 3.4

>>> ngram_vectorizer = CountVectorizer(ngram_range=(1, 2))
>>> ngram_vectors = ngram_vectorizer.fit_transform(corpus)
>>> ngram_vectors
<16x616 sparse matrix of type '<class 'numpy.int64'>'
    with 772 stored elements in Compressed Sparse Row format>

Inspect the runnable CountVectorizer configuration and the resulting sparse n-gram matrix. section 3.4

The trade-off is dimensionality: unique n-grams outnumber unique tokens, and large corpora can produce exponentially more features. Use the approach when phrase-level evidence matters, while recognizing its curse-of-dimensionality limits. section 3.4

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains n-gram selection, sparse vectors, phrase inspection, similarity matching, and dimensionality costs without descending into deeper nonlinear language representations.
Examples
Demonstrates CountVectorizer on a corpus, the phrase “algorithmic bias,” sparse matrix dimensions, and cosine-ranked query results.
What it assumes
Assumes familiarity with token counts and bag-of-words while treating n-gram size as a tunable pipeline parameter.
What you can do after
Apply it via CountVectorizer to create sparse 1-gram and n-gram document vectors and inspect phrase-level matches.
How it connects
Connects bag-of-words, TF-IDF, cosine similarity, hyperparameter tuning, and the curse of dimensionality.

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.