AI Notes
Complete preprocessing guide. Normalization, stop words, stemming. NLP 2024.
Why Preprocess Text?
Raw text is messy: it contains HTML tags, special characters, inconsistent casing, abbreviations, misspellings, and irrelevant tokens. Before any NLP model can effectively learn patterns, this noise must be reduced. Text preprocessing transforms raw, unstructured text into a clean, standardized format suitable for analysis.
Preprocessing is not one-size-fits-all. The steps you apply depend on your task, domain, and model. For a modern transformer, minimal preprocessing is best (the model handles complexity). For classical bag-of-words models, aggressive preprocessing is essential. Understanding each step's purpose helps you make informed decisions about your pipeline.
The Preprocessing Pipeline
Step-by-Step Preprocessing
1. Text Cleaning
2. Tokenization
Breaking text into individual tokens (words, subwords, or characters):
Whitespace tokenization
"I can't believe it!" → ["I", "can't", "believe", "it!"]
Problem: "can't" and "it!" aren't clean tokens
Word tokenization (NLTK)
"I can't believe it!" → ["I", "ca", "n't", "believe", "it", "!"]
Handles contractions and punctuation
Sentence tokenization
"Dr. Smith went home. He was tired." →
["Dr. Smith went home.", "He was tired."]
Must handle abbreviations (Dr., Mr., etc.)
Subword tokenization (BPE/WordPiece)
"unhappiness" → ["un", "##happi", "##ness"]
No out-of-vocabulary problem
3. Lowercasing
4. Stop Word Removal
| Stop words | extremely common words with little semantic content |
| English | "the", "is", "at", "which", "on", "a", "an", "and" |
| Before | ["the", "cat", "sat", "on", "the", "mat"] |
| After | ["cat", "sat", "mat"] |
| Common stop word lists | NLTK (179 words), spaCy, sklearn |
| ✗ "To be or not to be" | "be" loses all meaning with removal |
| ✗ Phrase search | "not good" vs "good" (negation lost!) |
| ✗ Language models | need all words for grammar |
| ✗ Transformers | pre-trained with stop words |
5. Handling Numbers
Strategies
- Remove all: "born in 1879" → "born in"
- Replace with token: "born in 1879" → "born in <NUM>"
- Keep as-is: important for dates, quantities, financial data
- Normalize: "1,000,000" → "1000000"
Domain-dependent
Medical: "take 2 tablets" → numbers critical!
Movie reviews: numbers rarely matter
Financial: all numbers are essential
6. Handling Contractions
| "can't" | "cannot" |
| "won't" | "will not" |
| "I'm" | "I am" |
| "they're" | "they are" |
| "it's" | "it is" (or "it has" — ambiguous!) |
Task-Specific Preprocessing
For Sentiment Analysis
| Keep | emoticons (:-) = positive), exclamation marks (!!!) |
| Keep | capitalization emphasis (AMAZING vs amazing) |
| Remove | URLs, @mentions, #hashtags (or extract as features) |
| Handle: negation scope ("not good" | "not_good") |
For Topic Modeling (LDA)
| Remove | stop words, numbers, short words (< 3 chars) |
| Apply | lemmatization (group word forms) |
| Remove | very rare words (< 5 occurrences) |
| Remove | very common words (> 80% of documents) |
For Transformers (BERT, GPT)
Practical Implementation
Common Mistakes
Interview Questions
Q: What preprocessing would you apply for a BERT-based classifier? A: Minimal: remove HTML, fix encoding, maybe lowercase (depends on task). Let the BERT tokenizer handle subwords. Do NOT remove stop words or punctuation—BERT was trained with them and uses them for understanding. Truncate to 512 tokens if needed.
Q: Why might removing stop words hurt a model? A: Stop words carry grammatical and semantic information. "Not good" without "not" becomes "good"—opposite meaning. "The dog bit the man" vs "the man bit the dog"—word order (stop words help) changes meaning entirely. Modern models leverage these words for understanding.
Q: How do you preprocess text in a language you don't speak? A: Use language-specific tokenizers (spaCy supports 60+ languages, Stanza). Don't apply English rules (no English stop word list). Use Unicode normalization. For Chinese/Japanese, use segment-based tokenizers (jieba, MeCab). Multilingual models (mBERT) handle most languages with their built-in tokenizers.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Text Preprocessing - Cleaning & Preparation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, natural, language, processing, text
Related Artificial Intelligence Topics