Building a Vocabulary
Map every word to a fixed index.
Building a Vocabulary is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Vocabulary?
A vocabulary is the full set of unique words your model knows. Each word gets one fixed slot in every document vector. 📖
Collect Every Unique Word
To build it, gather all words across your documents and keep only the distinct ones, dropping repeats.
words = set("the cat sat the mat".split())Give Each Word an Index
Sort the unique words and assign each a number. This word to index map is your vocabulary lookup.
vocab = {w: i for i, w in enumerate(sorted(words))}The Index Defines the Slot
The index tells a word which position it occupies in every vector, so cat always lands in the same column.
Vocabulary Sets Vector Length
If your vocabulary has 5000 words, every document becomes a vector of 5000 numbers, one slot per word.
Order Must Stay Fixed
Once set, the mapping must never change. A stable order guarantees that column 3 means the same word for every document.
Out-of-Vocabulary Words
A word not in your vocabulary is called out of vocabulary. The simplest choice is to ignore it during counting.
Pruning Rare Words
Words that appear once add noise and size. Dropping very rare terms keeps the vocabulary smaller and cleaner.
Dropping Very Common Words
You can also cut words that appear in nearly every document. These stopwords rarely help tell documents apart.
Look Up an Index
With the map built, finding a word position is instant. Just index the dictionary by the word you want.
print(vocab["cat"]) # the column index for catVocabulary Powers Counting
This map is the backbone of vectorization. Next you fill each slot with how often that word appears in a document.
Quick Check
What does a word index represent?
Recap: Your Vocabulary
You built a vocabulary of unique words, mapped each to a fixed index, and saw how pruning keeps it lean and useful. 🎉
Frequently asked questions
Is the “Building a Vocabulary” lesson free?
Yes — the full text of “Building a Vocabulary” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Vocabulary”?
Map every word to a fixed index. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Vocabulary” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.