liveBook · Manning

Bag-of-words model

As explained in Natural Language Processing in Action, Second Edition

Bag-of-words is a text representation that records word occurrences while disregarding the order and syntactic relationships of the words.

A bag-of-words (BOW) model represents text by the words it contains, without preserving their order or grammatical relationships. Imagine sorting tokens into bins: the contents of each bin tell you whether a word appeared or how often it occurred. The resulting vector makes text available for numerical comparison and tasks such as search and sentiment analysis. section 1.5.3 section 3.0

To build vectors that can be compared, use a consistent vocabulary: each position corresponds to the same token in every document. A binary vector records presence or absence; a count vector records occurrences. Dividing each count by the total token count gives a relative frequency. Many vocabulary entries may be absent from a document, so its vector is often sparse. section 2.5.2 section 3.1 section 1.5.3

Count tokens in a sentence

Tokenize a sentence and pass its tokens to Python’s collections.Counter. In an example about algorithmic bias, spaCy identifies punctuation as tokens: the comma appears five times and “and” twice. The 23 tokens contain 18 distinct token types, so the comma’s relative frequency is 5/23, or about 0.217. Inspecting counts matters: punctuation and common words can dominate the result. section 3.1

>>> from collections import Counter
>>> bag_of_words = Counter(tokens)
>>> bag_of_words
Counter({',': 5, 'and': 2, 'It': 1, 'has': 1, 'also': 1, 'arisen': 1, ...})

Inspect the Counter output for the tokenized algorithmic-bias sentence. section 3.1

Tokenization and vocabulary choices affect what the vector records. A pipeline can filter stop words or rare words, while a binary BOW can serve as an index for document retrieval. Counts can also suggest relevance or sentiment, but frequent words are only a rough signal and can lead a system astray. section 1.5.3 section 3.1

The trade-off is lost order. Rearrangements of a short greeting may retain a similar intent, but a complex logical query can change meaning when its words are rearranged. Word-level comparison also does not guarantee semantic closeness: changing “now” to “not” changes one word but reverses the message. Bags of n-grams retain counts of word pairs or longer sequences; TF–IDF scores words for importance across documents. section 1.7 section 2.5.3 section 3.0

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through tokenization, vocabulary, binary and count vectors, and relative frequencies, while highlighting lost word order.
Examples
Counts spaCy tokens in an algorithmic-bias sentence and compares permutations of a greeting with a logical query.
What you can do after
Apply it via Python’s Counter to inspect token counts and calculate relative frequencies.
How it connects
Connects tokenization and vector space models with n-grams, TF–IDF, and document similarity.

Also explained in

Compare how 6 books explain bag-of-words model →

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.