Bird
Raised Fist0
ML Pythonml~12 mins

Text preprocessing (tokenization, stemming, lemmatization) in ML Python - Model Pipeline Trace

Choose your learning style10 modes available
Model Pipeline - Text preprocessing (tokenization, stemming, lemmatization)

This pipeline shows how raw text is cleaned and prepared for machine learning. It breaks text into words (tokenization), simplifies words to their root forms (stemming), and converts words to their dictionary forms (lemmatization).

Data Flow - 4 Stages
1Raw Text Input
1000 sentences x variable length→Collect raw sentences from dataset→1000 sentences x variable length
"I am loving the sunny weather today!"
↓
2Tokenization
1000 sentences x variable length→Split sentences into words (tokens)→1000 sentences x average 10 tokens
["I", "am", "loving", "the", "sunny", "weather", "today", "!"]
↓
3Stemming
1000 sentences x average 10 tokens→Reduce words to their root forms by chopping endings→1000 sentences x average 10 stemmed tokens
["I", "am", "love", "the", "sunni", "weather", "today", "!"]
↓
4Lemmatization
1000 sentences x average 10 stemmed tokens→Convert words to dictionary base forms using context→1000 sentences x average 10 lemmatized tokens
["I", "be", "love", "the", "sunny", "weather", "today", "!"]
Training Trace - Epoch by Epoch

Loss
1.0 |***************
0.8 |**********     
0.6 |*******        
0.4 |****           
0.2 |**             
0.0 +--------------
     1 2 3 4 5 Epochs
EpochLoss ↓Accuracy ↑Observation
10.850.60Model starts learning with raw text features.
20.650.72Tokenization helps model understand word boundaries.
30.500.80Stemming reduces word variations, improving learning.
40.400.85Lemmatization further refines word forms, boosting accuracy.
50.350.88Model converges with stable loss and high accuracy.
Prediction Trace - 4 Layers
Layer 1: Input Raw Sentence
Layer 2: Tokenization
Layer 3: Stemming
Layer 4: Lemmatization
Model Quiz - 3 Questions
Test your understanding
What is the main purpose of tokenization in text preprocessing?
AReduce words to their root forms
BSplit text into words or tokens
CConvert words to dictionary base forms
DRemove punctuation from text
Key Insight
Text preprocessing steps like tokenization, stemming, and lemmatization help the model understand and generalize language better by simplifying and structuring raw text data.

Practice

(1/5)
1. What is the main purpose of tokenization in text preprocessing?
easy
A. To split text into smaller units like words or tokens
B. To convert words to their base dictionary form
C. To remove stop words from the text
D. To reduce words to their root form by cutting suffixes

Solution

  1. Step 1: Understand tokenization

    Tokenization means breaking text into smaller pieces called tokens, usually words.
  2. Step 2: Compare with other preprocessing steps

    Lemmatization and stemming change words, but tokenization just splits text.
  3. Final Answer:

    To split text into smaller units like words or tokens -> Option A
  4. Quick Check:

    Tokenization = splitting text [OK]
Hint: Tokenization means splitting text into words or tokens [OK]
Common Mistakes:
  • Confusing tokenization with stemming or lemmatization
  • Thinking tokenization changes word forms
  • Assuming tokenization removes words
2. Which of the following Python code snippets correctly performs stemming using NLTK's PorterStemmer?
easy
A. from nltk.tokenize import word_tokenize word_tokenize('running')
B. import re re.sub(r'\W+', '', 'running')
C. from nltk.stem import WordNetLemmatizer wl = WordNetLemmatizer() wl.lemmatize('running')
D. from nltk.stem import PorterStemmer ps = PorterStemmer() ps.stem('running')

Solution

  1. Step 1: Identify stemming code

    PorterStemmer from nltk.stem is used for stemming; calling stem('running') returns stemmed word.
  2. 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.
  3. Final Answer:

    from nltk.stem import PorterStemmer ps = PorterStemmer() ps.stem('running') -> Option D
  4. Quick Check:

    PorterStemmer.stem() = stemming [OK]
Hint: PorterStemmer.stem() is for stemming, not tokenization or lemmatization [OK]
Common Mistakes:
  • Confusing stemming with lemmatization functions
  • Using tokenization code for stemming
  • Using regex instead of stemmer
3. What will be the output of the following Python code using NLTK's WordNetLemmatizer?
from nltk.stem import WordNetLemmatizer
wl = WordNetLemmatizer()
print(wl.lemmatize('better', pos='a'))
medium
A. good
B. bet
C. better
D. betters

Solution

  1. Step 1: Understand lemmatization with POS

    Using pos='a' means adjective. Lemmatizer converts 'better' (comparative adjective) to base form 'good'.
  2. Step 2: Check output possibilities

    Stemming would cut to 'bet', but lemmatization returns dictionary base word 'good'.
  3. Final Answer:

    good -> Option A
  4. Quick Check:

    Lemmatize('better', pos='a') = 'good' [OK]
Hint: Lemmatizer with pos='a' returns base adjective form [OK]
Common Mistakes:
  • Expecting stemming output instead of lemmatization
  • Ignoring pos parameter in lemmatization
  • Assuming output is unchanged word
4. Identify the error in this code snippet for stemming a list of words using NLTK's PorterStemmer:
from nltk.stem import PorterStemmer
ps = PorterStemmer()
words = ['running', 'jumps', 'easily']
stemmed = [ps.stem(words)]
print(stemmed)
medium
A. The list comprehension syntax is incorrect
B. PorterStemmer is not imported correctly
C. ps.stem() expects a single word, but a list was passed
D. The print statement is missing parentheses

Solution

  1. Step 1: Check ps.stem() input

    ps.stem() expects a single string word, but words (a list) was passed directly.
  2. Step 2: Identify correct usage

    We should stem each word individually, e.g., using list comprehension: [ps.stem(w) for w in words].
  3. Final Answer:

    ps.stem() expects a single word, but a list was passed -> Option C
  4. Quick Check:

    Stem() needs single word input [OK]
Hint: Stem words one by one, not as a whole list [OK]
Common Mistakes:
  • Passing list directly to stem() instead of iterating
  • Confusing stemming with tokenization
  • Incorrect list comprehension syntax
5. You want to preprocess text data by first splitting sentences into words, then reducing words to their dictionary base form, and finally removing suffixes to get root forms. Which sequence of steps is correct?
hard
A. Tokenization -> Lemmatization -> Stemming
B. Tokenization -> Stemming -> Lemmatization
C. Lemmatization -> Stemming -> Tokenization
D. Stemming -> Tokenization -> Lemmatization

Solution

  1. Step 1: Understand preprocessing order

    First, split text into tokens (words) using tokenization.
  2. Step 2: Apply stemming then lemmatization

    Stemming reduces words to root forms by removing suffixes; lemmatization converts words to dictionary base form.
  3. Step 3: Verify option sequence

    Tokenization -> Stemming -> Lemmatization matches the correct order: Tokenization -> Stemming -> Lemmatization.
  4. Final Answer:

    Tokenization -> Stemming -> Lemmatization -> Option B
  5. Quick Check:

    Tokenize first, then stem, then lemmatize [OK]
Hint: Tokenize first, then stem, then lemmatize for best results [OK]
Common Mistakes:
  • Applying stemming before tokenization
  • Skipping tokenization step
  • Confusing order of lemmatization and stemming
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard