For text preprocessing steps like tokenization, stemming, and lemmatization, the key metrics focus on accuracy of text normalization. This means how well the processed text matches the intended meaning or structure for the model. Metrics include tokenization accuracy (correctly splitting text), and normalization accuracy (correctly reducing words to base forms). These matter because poor preprocessing leads to noisy or misleading input, hurting model performance.
Text preprocessing (tokenization, stemming, lemmatization) in ML Python - Model Metrics & Evaluation
While confusion matrices are common for classification, here we use a token-level accuracy table to show preprocessing quality.
Tokenization Results:
+----------------+----------------+----------------+
| Original Token | Correct Split? | Count |
+----------------+----------------+----------------+
| "running" | Yes | 100 |
| "runing" | No | 5 |
+----------------+----------------+----------------+
Stemming/Lemmatization Results:
+----------------+----------------+----------------+
| Original Word | Correct Base? | Count |
+----------------+----------------+----------------+
| "running" | "run" (Yes) | 100 |
| "ran" | "run" (Yes) | 50 |
| "runs" | "run" (Yes) | 80 |
| "runned" | Incorrect | 3 |
+----------------+----------------+----------------+
In text preprocessing, the tradeoff is between over-normalization and under-normalization.
- Over-normalization (High Precision, Low Recall): Only very sure words are stemmed or lemmatized, so fewer errors but many words remain unprocessed. This keeps precision high (correct base forms) but recall low (many words not normalized).
- Under-normalization (Low Precision, High Recall): Aggressive stemming changes many words, catching most variants but also making mistakes (wrong base forms). This increases recall but lowers precision.
Example: For a search engine, high recall is better to find all relevant documents, even if some words are wrongly normalized. For a grammar checker, high precision is better to avoid false corrections.
Good preprocessing:
- Tokenization accuracy > 98% (almost all words split correctly)
- Stemming/Lemmatization accuracy > 90% (most words reduced correctly)
- Low error rate in base form assignment
Bad preprocessing:
- Tokenization accuracy < 90% (many words incorrectly split or joined)
- Stemming/Lemmatization accuracy < 70% (many words wrongly changed)
- High noise in processed text causing model confusion
- Ignoring context: Stemming can cut words wrongly without context, hurting meaning.
- Overfitting preprocessing: Tailoring stemming rules too much to training data can fail on new text.
- Data leakage: Using test data to tune preprocessing can inflate accuracy falsely.
- Accuracy paradox: High tokenization accuracy may hide poor lemmatization quality.
- Ignoring downstream impact: Good preprocessing metrics alone don't guarantee better model results.
Your text preprocessing pipeline has 99% tokenization accuracy but only 60% lemmatization accuracy. Is this good enough for a sentiment analysis model? Why or why not?
Answer: Not necessarily. While tokenization is excellent, low lemmatization accuracy means many words are not properly normalized. This can confuse the model by treating similar words as different, hurting sentiment detection. Improving lemmatization or using better normalization is important.
Practice
tokenization in text preprocessing?Solution
Step 1: Understand tokenization
Tokenization means breaking text into smaller pieces called tokens, usually words.Step 2: Compare with other preprocessing steps
Lemmatization and stemming change words, but tokenization just splits text.Final Answer:
To split text into smaller units like words or tokens -> Option AQuick Check:
Tokenization = splitting text [OK]
- Confusing tokenization with stemming or lemmatization
- Thinking tokenization changes word forms
- Assuming tokenization removes words
Solution
Step 1: Identify stemming code
PorterStemmer from nltk.stem is used for stemming; calling stem('running') returns stemmed word.Step 2: Check other options
from nltk.tokenize import word_tokenize word_tokenize('running') is tokenization, C is lemmatization, D is regex substitution, not stemming.Final Answer:
from nltk.stem import PorterStemmer ps = PorterStemmer() ps.stem('running') -> Option DQuick Check:
PorterStemmer.stem() = stemming [OK]
- Confusing stemming with lemmatization functions
- Using tokenization code for stemming
- Using regex instead of stemmer
from nltk.stem import WordNetLemmatizer
wl = WordNetLemmatizer()
print(wl.lemmatize('better', pos='a'))Solution
Step 1: Understand lemmatization with POS
Using pos='a' means adjective. Lemmatizer converts 'better' (comparative adjective) to base form 'good'.Step 2: Check output possibilities
Stemming would cut to 'bet', but lemmatization returns dictionary base word 'good'.Final Answer:
good -> Option AQuick Check:
Lemmatize('better', pos='a') = 'good' [OK]
- Expecting stemming output instead of lemmatization
- Ignoring pos parameter in lemmatization
- Assuming output is unchanged word
from nltk.stem import PorterStemmer ps = PorterStemmer() words = ['running', 'jumps', 'easily'] stemmed = [ps.stem(words)] print(stemmed)
Solution
Step 1: Check ps.stem() input
ps.stem() expects a single string word, but words (a list) was passed directly.Step 2: Identify correct usage
We should stem each word individually, e.g., using list comprehension: [ps.stem(w) for w in words].Final Answer:
ps.stem() expects a single word, but a list was passed -> Option CQuick Check:
Stem() needs single word input [OK]
- Passing list directly to stem() instead of iterating
- Confusing stemming with tokenization
- Incorrect list comprehension syntax
Solution
Step 1: Understand preprocessing order
First, split text into tokens (words) using tokenization.Step 2: Apply stemming then lemmatization
Stemming reduces words to root forms by removing suffixes; lemmatization converts words to dictionary base form.Step 3: Verify option sequence
Tokenization -> Stemming -> Lemmatization matches the correct order: Tokenization -> Stemming -> Lemmatization.Final Answer:
Tokenization -> Stemming -> Lemmatization -> Option BQuick Check:
Tokenize first, then stem, then lemmatize [OK]
- Applying stemming before tokenization
- Skipping tokenization step
- Confusing order of lemmatization and stemming
Ready to prove it?
Pass the test and this pattern moves unknown
