Bird
Raised Fist0
NLPml~5 mins

Stemming (Porter, Snowball) in NLP

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
Introduction

Stemming helps reduce words to their root form so computers can understand similar words as the same. It makes text simpler and easier to analyze.

When you want to group similar words like 'running' and 'runs' as the same word 'run' in a search engine.
When cleaning text data before training a machine learning model to reduce word variety.
When you want to count word frequencies ignoring different endings like 'talked' and 'talking'.
When building a chatbot that should understand different forms of a word as one concept.
Syntax
NLP
from nltk.stem import PorterStemmer, SnowballStemmer

porter = PorterStemmer()
snowball = SnowballStemmer('english')

stemmed_word_porter = porter.stem('running')
stemmed_word_snowball = snowball.stem('running')

PorterStemmer is one of the oldest and most common stemmers.

SnowballStemmer is newer and supports multiple languages, often giving better results.

Examples
Stems 'running' to 'run' using PorterStemmer.
NLP
from nltk.stem import PorterStemmer
porter = PorterStemmer()
print(porter.stem('running'))
Stems 'running' to 'run' using SnowballStemmer.
NLP
from nltk.stem import SnowballStemmer
snowball = SnowballStemmer('english')
print(snowball.stem('running'))
Shows how PorterStemmer reduces different forms to similar roots.
NLP
words = ['runs', 'running', 'runner']
porter = PorterStemmer()
stemmed = [porter.stem(w) for w in words]
print(stemmed)
Sample Model

This program shows how both Porter and Snowball stemmers reduce words to their root forms. It prints the original words and their stemmed versions.

NLP
from nltk.stem import PorterStemmer, SnowballStemmer

words = ['running', 'runs', 'runner', 'easily', 'fairly']

porter = PorterStemmer()
snowball = SnowballStemmer('english')

porter_stems = [porter.stem(word) for word in words]
snowball_stems = [snowball.stem(word) for word in words]

print('Original words:', words)
print('Porter stems:', porter_stems)
print('Snowball stems:', snowball_stems)
OutputSuccess
Important Notes

Stemming may produce roots that are not real words, but they help group similar words.

SnowballStemmer often gives cleaner stems than PorterStemmer.

Stemming is different from lemmatization, which returns real dictionary words.

Summary

Stemming reduces words to their base form to simplify text.

Porter and Snowball are popular stemmers with slightly different results.

Use stemming to improve text analysis and machine learning on text data.

Practice

(1/5)
1. What is the main purpose of stemming in Natural Language Processing?
easy
A. To reduce words to their base or root form
B. To translate text into another language
C. To count the number of words in a sentence
D. To generate synonyms for words

Solution

  1. Step 1: Understand stemming concept

    Stemming simplifies words by cutting off suffixes to get the root form.
  2. Step 2: Compare options with stemming goal

    Only To reduce words to their base or root form describes reducing words to their base form, which is the goal of stemming.
  3. Final Answer:

    To reduce words to their base or root form -> Option A
  4. Quick Check:

    Stemming = base form reduction [OK]
Hint: Stemming cuts word endings to find the root [OK]
Common Mistakes:
  • Confusing stemming with translation
  • Thinking stemming counts words
  • Mixing stemming with synonym generation
2. Which of the following is the correct way to import the Porter Stemmer from NLTK in Python?
easy
A. from nltk.stem import PorterStemmer
B. import nltk.PorterStemmer
C. from nltk import PorterStemmer
D. import PorterStemmer from nltk.stem

Solution

  1. Step 1: Recall correct import syntax in Python

    Python imports use 'from module import class' format for specific classes.
  2. Step 2: Match with NLTK Porter Stemmer import

    The correct import is 'from nltk.stem import PorterStemmer' as it imports the class from the stem module.
  3. Final Answer:

    from nltk.stem import PorterStemmer -> Option A
  4. Quick Check:

    Correct import uses 'from nltk.stem import PorterStemmer' [OK]
Hint: Use 'from module import class' for specific imports [OK]
Common Mistakes:
  • Using dot notation incorrectly in import
  • Trying to import class directly from nltk
  • Wrong order of import keywords
3. What is the output of the following Python code using Porter Stemmer?
from nltk.stem import PorterStemmer
ps = PorterStemmer()
words = ['running', 'runs', 'runner']
stemmed = [ps.stem(word) for word in words]
print(stemmed)
medium
A. ['run', 'run', 'run']
B. ['running', 'runs', 'runner']
C. ['run', 'run', 'runner']
D. ['runn', 'run', 'runn']

Solution

  1. Step 1: Apply Porter Stemmer to each word

    Porter Stemmer reduces 'running' and 'runs' to 'run', but 'runner' remains 'runner' because it is treated differently.
  2. Step 2: List the stemmed results

    The list becomes ['run', 'run', 'runner'] after stemming.
  3. Final Answer:

    ['run', 'run', 'runner'] -> Option C
  4. Quick Check:

    Porter stems 'running' and 'runs' to 'run' [OK]
Hint: Porter stems common verb forms to root, but some nouns stay [OK]
Common Mistakes:
  • Assuming all words stem to the same root
  • Confusing stemmed output with original words
  • Expecting 'runner' to stem to 'run'
4. Identify the error in this Snowball Stemmer usage code snippet:
from nltk.stem import SnowballStemmer
stemmer = SnowballStemmer('english')
words = ['happiness', 'happier', 'happy']
stemmed_words = [stemmer.stem(word) for word in words]
print(stemmed_words)
medium
A. The stem method should be called as stemmer.stem_word(word)
B. No error; code runs correctly and prints stemmed words
C. SnowballStemmer requires language name in uppercase
D. SnowballStemmer must be imported from nltk.stem.snowball

Solution

  1. Step 1: Check SnowballStemmer import and usage

    Importing from nltk.stem and initializing with 'english' is correct and case-insensitive.
  2. Step 2: Verify method call and output

    The stem method is correctly called as stemmer.stem(word), and the code prints stemmed words without error.
  3. Final Answer:

    No error; code runs correctly and prints stemmed words -> Option B
  4. Quick Check:

    SnowballStemmer usage is correct as shown [OK]
Hint: SnowballStemmer language is lowercase string, stem() method used [OK]
Common Mistakes:
  • Using uppercase language name incorrectly
  • Calling non-existent stem_word method
  • Wrong import path for SnowballStemmer
5. You want to preprocess text data by stemming words but keep the original word if it is shorter than 4 characters. Which Python code snippet using Porter Stemmer correctly implements this?
hard
A. stemmed = [ps.stem(word) for word in words if len(word) >= 4]
B. stemmed = [ps.stem(word) if len(word) < 4 else word for word in words]
C. stemmed = [word for word in words if len(word) < 4 else ps.stem(word)]
D. stemmed = [word if len(word) < 4 else ps.stem(word) for word in words]

Solution

  1. Step 1: Understand the condition for stemming

    Words shorter than 4 characters should remain unchanged; others should be stemmed.
  2. Step 2: Check list comprehension syntax

    stemmed = [word if len(word) < 4 else ps.stem(word) for word in words] uses correct if-else inside list comprehension: 'word if len(word) < 4 else ps.stem(word)'.
  3. Final Answer:

    stemmed = [word if len(word) < 4 else ps.stem(word) for word in words] -> Option D
  4. Quick Check:

    Keep short words, stem others with if-else [OK]
Hint: Use 'word if condition else stem(word)' in list comprehension [OK]
Common Mistakes:
  • Swapping if-else order in comprehension
  • Using if without else causing missing elements
  • Incorrect syntax mixing if-else and for