The bag-of-words model represents each sentence or document as a fixed-length vector of word frequencies. It tokenizes the corpus, builds a vocabulary of unique words, and ignores grammar, word order, and context. Optional frequency thresholds can restrict the vocabulary. section 7.9
A practical construction workflow is:
- Tokenize every document into individual words. section 7.9
- Collect the unique tokens into a corpus vocabulary, optionally applying upper and lower frequency limits. section 7.9
- Encode each document with one vector position per vocabulary word, using zero when a word is absent. section 7.9
- Use the resulting vectors for fast, computationally inexpensive document classification, while recognizing that frequency alone can reduce accuracy. section 7.9 section 7.20
Two-sentence vocabulary
For “It is raining heavily” and “We should eat fruits,” treat the words in both sentences as the vocabulary. The first sentence receives its word counts and zeros for words appearing only in the second sentence. section 7.9
Figure 7.5 The frequency of each word has been calculated. In this example, we have two sentences.
Inspect the token frequencies that precede vector construction. section 7.9
Figure 7.6 We are assuming that in the vocabulary only two sentences are present and the first sentence will be represented as shown.
Inspect how the first sentence becomes a vocabulary-sized vector with zero values for absent words. section 7.9




