Day 4: Bag-of-Words and Text Vectorization
Previously, on Day 3: Explained stopword removal, stemming, and lemmatization in NLP, including how they simplify and normalize text for analysis using practical examples and Python code. Text Vectorization: Turning Words into Numbers Computers work with numbers, not text. To handle language, a Natural Language Processing (NLP) system must convert words, sentences, or documents into numerical data. Usually, this means turning them into vectors—ordered arrays of numbers. This process is called text vectorization . A vector is a mathematical summary of a piece of text. The details and meaning behind the numbers depend on which vectorization method is used, but all serve a common purpose: to translate language into something a machine can process. For example, imagine building a program to filter spam emails. The program can't directly understand words like "WINNER" or "sale." Every word must be mapped to a number before the program can look for patterns in messages. What is the Bag-of-Words Model? Bag-of-Words (BoW) is the simplest and most common way to vectorize text. BoW ignores grammar and word order. It treats each document as a "bag" containing words, just counting how many times each word appears. For example, the sentences "dog bites man" and "man bites dog" will produce the same vector in a BoW system. Both have the words "dog," "bites," and "man," each once. The meaning is very different to a human, but to BoW, they're identical. This straightforward approach makes BoW fast and effective for many tasks, especially where quickly spotting key words is enough—for example, spam detection. From Words to Vectors: Building a Vocabulary The first step in BoW is to build a vocabulary . This is a list of all unique words seen across your dataset (called a "corpus"). Suppose your dataset contains two sentences: "cat sat on the mat" "dog sat on the log" List all unique words: ["cat", "sat", "on", "the", "mat", "dog", "log"] The word order in the vocabulary doesn't matte