AI GLOSSARY · AI CONCEPTS
Bag of words (BoW)
Definition
Bag of words is a way of turning a text into numbers by counting how many times each word appears, while ignoring the order of the words. Each document becomes a row of word counts.
Also called: BoW, bag-of-words model, bag of words model, bow nlp.
In plain language
Imagine tipping a page of text into a bag and shaking it so the sentences fall apart. You can still count how many times each word is in the bag, but you cannot tell which word came first. That count is the whole description of the page. A program can compare two such counts to guess whether two texts are about the same thing. The limit is that meaning that depends on order or context is lost: 'dog bites man' and 'man bites dog' give the same count. Counting also produces long lists in which most entries are zero.
Technical definition
Bag of words builds a vocabulary from a corpus, assigns each token an integer id, counts the occurrences of each token per document, and optionally normalises or weights the counts. The result is a matrix with one row per document and one column per token. scikit-learn describes the strategy of tokenization, counting and normalization as the Bag of Words or bag of n-grams representation, in which relative word position is ignored. Because each document uses a small part of the vocabulary, the matrix is sparse. Weighting schemes such as tf-idf are applied to these counts. See BM25 for a related ranking function.
Worked examples in this catalogue
Catalogue examples point at a skill, guide or coding tool listed on this site, with what to run and what you see. An example marked illustrative describes the item from its own documentation; it was not executed for this page. Platform Scores are our assessment under a published method, not a safety guarantee.
Skill by Skill Almanac · Illustrative, not executed
keyword-density-count 41/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- What you see
- Counts for each keyword or phrase in plain page text, case-insensitive and whole-word, with density figures.
- Why it exercises the concept
- Its unigram counts illustrate order-free word counting on one page. Requested phrases and reported bigrams retain local word order, so the full skill is not a pure unigram bag of words.
Skill by Skill Almanac · Illustrative, not executed
stock-phrase-scanner 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- What you see
- A report of stock filler phrases, hedges and repeated sentence openers in a plain-text draft, with line numbers for each hit.
- Why it exercises the concept
- An ordered lexical contrast: it matches listed phrases and sentence openers, so local word order matters; it is not pure order-free bag of words.
Skill by Skill Almanac · Illustrative, not executed
duplicate-passage-finder 41/100 · Provisional, not yet measuredScored · Method v0.2 · How we score
- What you see
- Groups of exact and near-duplicate paragraphs across a folder of .txt and .md files, each with its file and line number.
- Why it exercises the concept
- An ordered lexical contrast: its near-duplicate matching uses word shingles, retaining local order rather than only unigram counts.
How it shows up when you use agents
You may not meet a bag of words by name in Claude Code, Codex or Cursor, but the idea sits underneath tools you use. Some lexical retrieval representations use word-occurrence counts. File searches and hybrid retrievers can also use exact phrases or other order-sensitive signals; inspect the actual representation. A large language model, by contrast, reads words in order as tokens and turns them into embeddings that carry context.
This matters when you debug retrieval. If an agent or a retrieval pipeline fails to find a passage that says the same thing in different words, a word-count method is a likely cause, because it matches the words and not the meaning. If it returns passages that share many words but say the opposite, the lost word order is another. IBM lists related limits: words are treated as independent, compound terms are split, and words with several meanings share one feature.
A next action: when a search misses, test the same query with a keyword method and an embedding method and compare the results before changing prompts. The catalogue's keyword-density-count skill shows plain counting on a single page, which is a useful way to see what this representation keeps and what it drops.
Common confusions
- Bag of words versus word embeddings. A bag of words has one column per word and holds counts. An embedding is a short list of numbers learned so that similar meanings sit close together. See Word embedding.
- Bag of words versus bag of n-grams. The n-gram version counts short runs of words, such as pairs. scikit-learn treats both as the same family of representation.
- Bag of words versus tf-idf. Raw counts treat each word equally. Weighting schemes reduce the importance of words that occur in most documents.
- Myth. Bag of words understands the text. It records which words appear and how often, nothing about their order or meaning.
Related terms
Terms that mention Bag of words (BoW)
1 terms mention this one
More in AI concepts
Sources
- Feature extraction: the Bag of Words representation, scikit-learn (accessed 2026-11-24)
- What is bag of words?, IBM (accessed 2026-11-24)
Definitions are written by Skill Almanac from these sources and were checked against them on 2026-11-25. Wikipedia is never the only source.
Review record
Last verified
Drafted by Claude (original draft) and OpenAI Codex (targeted revisions); sources and example targets checked by Independent automated source review (OpenAI Codex); not human peer review on 2026-11-25. Last verified .
Change log (5)
- : Independent automated factual/example review completed. Cached US query estimate aligned to receipt; no traffic promise. Final technical gate recorded separately; publication disabled.
- : Bounded batch-13 source and illustration repairs; independent affected recheck pending, publication disabled.
- : First version.
- : Verifier pass: trimmed unsourced claims and example rationales, removed bag of n-grams alias.
- : Automated review completed; approved for the staged public release. Search indexing is controlled by the release configuration.