LambdaLabTM
Artificial Intelligence · Class 10 · Unit 6 · Natural Language Processing
class 10 ainlp⏱️ 10 min read

Bag of Words

Now the text is clean, it has to become numbers. Bag of Words is how — and drawing its table is a 4-mark question that has come up in consecutive years.

1What it is

Bag of Words
A Natural Language Processing model which helps in extracting features out of the text that can be used by machine learning algorithms. It gives us two things:
1. A vocabulary of words for the corpus.
2. The frequency of those words — how many times each has occurred.
Why it is called a bag
Calling it a “bag” symbolises that the sequence of the words does not matter. Tip a bag out and the order is gone. All that is kept is the unique words and how many of each there were.

A shopping receipt

A receipt does not record the order you picked things off the shelves. It records what you bought and how many. Two shoppers with identical receipts had identical trolleys, whatever route they took round the shop.

2The four steps

  1. Text processing — collect the data and pre-process it.
  2. Create a dictionary — list all the unique words occurring in the corpus. This is the vocabulary.
  3. Create a document vector — for a document, count how many times each dictionary word occurs in it.
  4. Create document vectors for all the documents.
Bag of Words, in four steps

This is exactly the table a 4-mark question asks you to produce. Step through it once and the shape sticks.

Document 1
the students study science
thestudentsstudyscience
Document 2
the students study history
thestudentsstudyhistory
Document 3
the teacher marks the science test
theteachermarksthesciencetest
Document 4
the students enjoy the science laboratory
thestudentsenjoythesciencelaboratory

Collect the documents and normalise them — lowercase, punctuation gone, split into tokens.

How to build the row
Write the vocabulary along the top. Then for each word in the document: if it matches the vocabulary, put a 1 under it; if the same word appears again, increment the previous value by 1; and if the word does not occur in that document, put a 0.

3From the board papers

1
Consider the following documents. Implement all four steps of the Bag of Words model to create a document vector table.
Document 1: Data Science requires information.
Document 2: Information analysis requires data.CBSE board exam 2026
4 marks
▶Show the answer

Step 1 — text processing. Lowercase, drop the full stops, tokenise:
D1: [data, science, requires, information]
D2: [information, analysis, requires, data]

Step 2 — dictionary (5 unique words): data, science, requires, information, analysis

Steps 3 and 4 — document vectors:

datasciencerequiresinformationanalysis
Doc 111110
Doc 210111
2
Consider the following documents. Implement all four steps of the Bag of Words model to create a document vector table.
Document 1: NLP is a domain of AI.
Document 2: NLP stands for Natural Language Processing.CBSE board exam 2025
4 marks
▶Show the answer

D1: [nlp, is, a, domain, of, ai]    D2: [nlp, stands, for, natural, language, processing]
Dictionary (11 words): nlp, is, a, domain, of, ai, stands, for, natural, language, processing

nlpisadomainofaistandsfornaturallanguageprocessing
Doc 111111100000
Doc 210000011111

Note the stop words are still there. Text normalisation says to remove is, a, of, for— but the handbook’s own worked example keeps them in the dictionary, and so do the board’s. Follow the book: keep them unless the question says otherwise.

3
Three documents, already pre-processed:
D1: [students, love, studying, ai]   D2: [ai, is, transforming, education]   D3: [teachers, and, students, explore, ai, tools]
(a) Create the dictionary. (b) Construct the document vector for Document 3. (c) Explain how Bag of Words helps in feature extraction. (d) Why is the order of words not considered important?CBSE sample paper 2025–26
4 marks
▶Show the answer

(a) 11 unique words: students, love, studying, ai, is, transforming, education, teachers, and, explore, tools

(b) Document 3 in that order:
1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1

(c) It converts text into numbers a machine learning algorithm can use: each document becomes a vector of word counts, and those counts are the features.

(d) Because the model only records which words occur and how often. It is called a bag precisely because the sequence is thrown away — the vocabulary and the frequencies are all that is kept.

4Check yourself

Quick Check

What two things does Bag of Words give you?

Quick Check

Why is it called a 'bag' of words?

Quick Check

In a document vector, a word that does not appear in that document is written as:

Quick Check

Which algorithm creates a vocabulary of all unique words in the corpus?