Bird
Raised Fist0
NLPml~20 mins

Text preprocessing pipelines in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Text Preprocessing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this text tokenization code?
Consider the following Python code that tokenizes a sentence into words using a simple split method. What is the output list?
NLP
sentence = "Hello, world! Welcome to AI." 
tokens = sentence.split()
print(tokens)
A['Hello,', 'world!', 'Welcome', 'to', 'AI.']
B['Hello', 'world', 'Welcome', 'to', 'AI']
C['Hello', ',', 'world', '!', 'Welcome', 'to', 'AI', '.']
D['Hello world Welcome to AI']
Attempts:
2 left
💡 Hint
The split() method splits by spaces and does not remove punctuation.
🧠 Conceptual
intermediate
1:30remaining
Which step is NOT typically part of a text preprocessing pipeline?
In a typical text preprocessing pipeline for NLP tasks, which of the following steps is usually NOT included?
AApplying stemming or lemmatization
BRemoving stopwords
CTraining a neural network model
DLowercasing all text
Attempts:
2 left
💡 Hint
Preprocessing prepares data before modeling.
Metrics
advanced
2:00remaining
What is the vocabulary size after preprocessing?
Given the following list of sentences, after converting all text to lowercase, removing punctuation, and tokenizing, what is the size of the vocabulary (unique words)? Sentences: 1. "AI is fun!" 2. "Fun with AI and machine learning." 3. "Learning AI is exciting."
NLP
import string
sentences = ["AI is fun!", "Fun with AI and machine learning.", "Learning AI is exciting."]
vocab = set()
for sent in sentences:
    sent = sent.lower()
    sent = sent.translate(str.maketrans('', '', string.punctuation))
    tokens = sent.split()
    vocab.update(tokens)
print(len(vocab))
A10
B8
C9
D7
Attempts:
2 left
💡 Hint
Count unique words after cleaning and splitting.
🔧 Debug
advanced
1:30remaining
Why does this text normalization code raise an error?
Examine the code below that attempts to normalize text by removing digits and converting to lowercase. Why does it raise an error?
NLP
import re
text = "AI version 2.0 is here!"
normalized = re.sub('[0-9]+', '', text).lower()
print(normalized)
ARaises AttributeError because lower() is called on None
BRaises TypeError because re.sub returns bytes, not string
CRaises SyntaxError due to incorrect regex pattern
DNo error; the code runs and outputs 'ai version . is here!'
Attempts:
2 left
💡 Hint
Check the return type of re.sub and method chaining.
Model Choice
expert
2:00remaining
Which model is best suited for text classification after preprocessing?
After completing text preprocessing (tokenization, stopword removal, and vectorization), which model below is generally best for classifying short text documents?
ARecurrent Neural Network (RNN) or LSTM tailored for sequences
BConvolutional Neural Network (CNN) designed for images
CLinear Regression model
DK-Means clustering algorithm
Attempts:
2 left
💡 Hint
Consider models that handle sequences and context well.

Practice

(1/5)
1. What is the main purpose of a text preprocessing pipeline in NLP?
easy
A. To train the machine learning model directly
B. To generate new text data automatically
C. To clean and prepare text data step-by-step for models
D. To visualize text data in graphs

Solution

  1. Step 1: Understand the role of preprocessing

    Preprocessing cleans and prepares raw text so models can understand it better.
  2. Step 2: Identify pipeline benefits

    Pipelines organize these steps neatly and make the process repeatable.
  3. Final Answer:

    To clean and prepare text data step-by-step for models -> Option C
  4. Quick Check:

    Preprocessing pipeline = clean and prepare text [OK]
Hint: Pipelines organize cleaning steps before modeling [OK]
Common Mistakes:
  • Confusing preprocessing with model training
  • Thinking pipelines generate new text
  • Assuming pipelines visualize data
2. Which of the following is the correct way to chain text preprocessing steps in Python using a pipeline?
easy
A. pipeline = [tokenize, lowercase, remove_stopwords]
B. pipeline = Pipeline(steps=[('tokenize', tokenize), ('lowercase', lowercase), ('stop', remove_stopwords)])
C. pipeline = tokenize + lowercase + remove_stopwords
D. pipeline = tokenize.lowercase.remove_stopwords()

Solution

  1. Step 1: Recognize pipeline syntax

    In Python, pipelines are often created using a Pipeline class with named steps.
  2. Step 2: Check options

    pipeline = Pipeline(steps=[('tokenize', tokenize), ('lowercase', lowercase), ('stop', remove_stopwords)]) correctly uses Pipeline with steps as tuples of (name, function).
  3. Final Answer:

    pipeline = Pipeline(steps=[('tokenize', tokenize), ('lowercase', lowercase), ('stop', remove_stopwords)]) -> Option B
  4. Quick Check:

    Pipeline uses steps list with (name, function) tuples [OK]
Hint: Use Pipeline class with named steps list [OK]
Common Mistakes:
  • Trying to chain functions with dots or plus signs
  • Not naming steps in the pipeline
  • Using list of functions without Pipeline wrapper
3. Given the following code snippet, what will be the output of processed_text?
def lowercase(text):
    return text.lower()

def remove_punctuation(text):
    return ''.join(c for c in text if c.isalnum() or c.isspace())

text = "Hello, World!"

pipeline = [lowercase, remove_punctuation]

processed_text = text
for step in pipeline:
    processed_text = step(processed_text)

print(processed_text)
medium
A. hello world
B. Hello World
C. hello, world!
D. HELLO WORLD

Solution

  1. Step 1: Apply lowercase function

    "Hello, World!" becomes "hello, world!" after lowercase.
  2. Step 2: Apply remove_punctuation function

    Removes commas and exclamation marks, leaving "hello world".
  3. Final Answer:

    hello world -> Option A
  4. Quick Check:

    Lowercase + remove punctuation = "hello world" [OK]
Hint: Apply steps one by one on text [OK]
Common Mistakes:
  • Forgetting to lowercase before removing punctuation
  • Assuming punctuation remains
  • Confusing case sensitivity
4. Identify the error in this text preprocessing pipeline code and select the fix:
def tokenize(text):
    return text.split()

def remove_stopwords(words):
    stopwords = ['the', 'is', 'at']
    return [w for w in words if w not in stopwords]

text = "The cat is at the door"

pipeline = [tokenize, remove_stopwords]

processed = text
for step in pipeline:
    processed = step(processed)

print(processed)
medium
A. Define stopwords outside the function
B. Add join after remove_stopwords to convert list back to string
C. Replace split() with list() in tokenize
D. Change text to lowercase before tokenizing

Solution

  1. Step 1: Analyze stopwords matching

    Stopwords are lowercase but input text has capitalized words, so matching fails.
  2. Step 2: Fix by lowercasing text before tokenizing

    Lowercasing ensures stopwords match and are removed correctly.
  3. Final Answer:

    Change text to lowercase before tokenizing -> Option D
  4. Quick Check:

    Lowercase text first to match stopwords [OK]
Hint: Lowercase text before removing stopwords [OK]
Common Mistakes:
  • Ignoring case mismatch in stopwords
  • Trying to join list without need
  • Changing split() to list() incorrectly
5. You want to build a text preprocessing pipeline that: 1. Converts text to lowercase 2. Removes punctuation 3. Tokenizes text into words 4. Removes stopwords Which of the following pipeline orders is correct to ensure proper processing?
hard
A. Lowercase -> Remove punctuation -> Tokenize -> Remove stopwords
B. Tokenize -> Lowercase -> Remove stopwords -> Remove punctuation
C. Remove stopwords -> Tokenize -> Lowercase -> Remove punctuation
D. Remove punctuation -> Remove stopwords -> Tokenize -> Lowercase

Solution

  1. Step 1: Start with lowercase

    Lowercasing first ensures uniform text for all later steps.
  2. Step 2: Remove punctuation before tokenizing

    Removing punctuation cleans text so tokens are words only.
  3. Step 3: Tokenize then remove stopwords

    Tokenizing splits text into words, then stopwords can be removed from tokens.
  4. Final Answer:

    Lowercase -> Remove punctuation -> Tokenize -> Remove stopwords -> Option A
  5. Quick Check:

    Correct pipeline order = A [OK]
Hint: Lowercase, clean, tokenize, then filter stopwords [OK]
Common Mistakes:
  • Tokenizing before cleaning punctuation
  • Removing stopwords before tokenizing
  • Not lowercasing first