Sub topic 8
The Porter and Snowball stemmers in practice
This chapter assumes you know basic Python, lists, functions, and regular expressions. You should also know that a natural language processing task can include search, classification, or text matching. Basic grammar terms such as noun, verb, plural, and tense are useful.
What a stemmer does
A stemmer reduces related word forms to a shorter string called a stem. It uses rules about spelling, especially suffixes. It does not usually consult a dictionary.
For example, a stemmer may map these words to a shared form:
connectconnectedconnectingconnection
The result is useful when a search system should treat related forms as similar. However, a stem is not necessarily a valid English word. For example, Porter stemming changes studies to studi.
Stemming is therefore a fast approximation of morphological normalisation. Morphology is the study of how words change form and meaning.
Porter and Snowball
The Porter stemmer was published by Martin Porter in 1980. It applies a sequence of suffix-removal rules. The rules are ordered, so one step can change the input for a later step.
The Snowball stemmer is produced by the Snowball language and framework. Its English algorithm is commonly called the English stemmer or Porter2. It revises the original Porter algorithm and usually gives more consistent results for modern English. Snowball also provides stemmers for several other languages.
| Stemmer | Main characteristic | Typical use |
|---|---|---|
| Porter | Older, compact rule set | Reproducing an older information-retrieval system |
| Snowball English | Revised English rules | New English search or text-matching systems |
| Snowball in another language | Language-specific rules | Multilingual preprocessing |
Neither stemmer understands a sentence's meaning. Both treat stemming as a spelling transformation.
Applying both stemmers
The Natural Language Toolkit, or NLTK, provides implementations of both algorithms. Install it with pip install nltk if it is not already available.
from nltk.stem import PorterStemmer, SnowballStemmer
porter = PorterStemmer()
snowball = SnowballStemmer("english")
words = [
"caresses",
"ponies",
"running",
"studies",
"relational",
"easily",
]
print(f"{'word':<12}{'Porter':<12}{'Snowball'}")
for word in words:
print(f"{word:<12}{porter.stem(word):<12}{snowball.stem(word)}")The exact outputs reveal the difference between the algorithms. Both stemmers commonly reduce running to run, but they can disagree on less regular forms. The important result is not whether a stem looks like a word. The important result is whether words that should match receive compatible stems.
Apply the same stemmer consistently during indexing and searching. If documents are stemmed with Porter but queries are stemmed with Snowball, some matches may be lost.
Understanding stemming errors
A stemmer can make two kinds of errors.
Overstemming happens when unrelated words are reduced to the same stem. For example, an aggressive rule might make words with different meanings look identical. This can increase recall, which means finding more relevant documents, but reduce precision, which means returning more irrelevant documents.
Understemming happens when related words remain separate. A search for organize might fail to match organizing if the algorithm does not reduce both forms to the same stem. This can reduce recall.
The right balance depends on the task:
- Search often accepts some overstemming because finding related documents matters.
- A sentiment classifier may perform better with unstemmed words or carefully selected features.
- A language-generation system usually should not stem its input because unnatural stems damage meaning and fluency.
- A small storage-constrained system may prefer stemming because stems reduce the number of distinct features.
Evaluate before choosing
Inspect a sample of real data rather than judging an algorithm from a few words. Create a table containing the original word and its stem, then look for:
- Related words that fail to share a stem.
- Unrelated words that collapse to one stem.
- Stems that remove too much useful information.
- Domain terms, names, and abbreviations that are damaged.
Compare task metrics such as search precision and recall, or classification accuracy and F1 score. Also measure processing time if the dataset is large. Snowball is often a sensible default for new English search systems, but evaluation should decide the final choice.
Exercise
You are building an English document-search system. Should you stem documents with Porter and queries with Snowball? Why or why not?
Show answer
No. Use the same normalisation strategy on both documents and queries. Start with Snowball for new English systems, then compare it with Porter and with no stemming on representative search queries.