Sub topic 10
Lemmatisation with a Lexicon
This chapter assumes basic Python, regular expressions, and common grammar terms such as noun, verb, adjective, plural, and tense. It also assumes you know that tokenisation turns text into tokens.
Lemmatisation reduces a word form to its lemma, or dictionary base form. For example, the lemmas of cats, running, and better can be cat, run, and good. Unlike stemming, lemmatisation uses a lexicon and language rules. The result is normally a valid dictionary word.
How a lemmatiser works
A lemmatiser usually combines:
- A lexicon, which lists words and their known base forms.
- Morphological rules, which describe changes such as plural
-sor verb endings. - Part-of-speech information, which identifies whether a token is a noun, verb, adjective, or another grammatical category.
The same spelling can have different lemmas depending on its part of speech. For example, saw is the past tense of see when it is a verb, but it is already a noun when it means a cutting tool.
A lemmatiser cannot reliably infer the correct base form from spelling alone. It needs grammatical context or a predicted part-of-speech tag.
Using WordNetLemmatizer
NLTK provides WordNetLemmatizer, which uses the WordNet lexical database. Install NLTK first if necessary:
python -m pip install nltkThe following example downloads the required dictionary and applies different part-of-speech values.
import nltk
from nltk.stem import WordNetLemmatizer
nltk.download("wordnet")
lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize("studies", pos="n"))
print(lemmatizer.lemmatize("running", pos="v"))
print(lemmatizer.lemmatize("better", pos="a"))
print(lemmatizer.lemmatize("saw", pos="v"))
print(lemmatizer.lemmatize("saw", pos="n"))The pos argument tells WordNet how to interpret the token:
| WordNet code | Part of speech | Example |
|---|---|---|
| `n` | noun | `studies` → `study` |
| `v` | verb | `running` → `run` |
| `a` | adjective | `better` → `good` |
| `r` | adverb | `quickly` → `quickly` |
If you omit pos, WordNet assumes the token is a noun. Therefore, lemmatize("running") may return running, because running is also a noun. This default is a common source of mistakes.
Supplying part-of-speech information
Real NLP pipelines often produce part-of-speech tags before lemmatisation. A tagger might label running as a verb in “They are running” and as a noun in “Running is healthy.”
NLTK uses detailed Penn Treebank tags such as VB, VBD, and NN, while WordNet expects a small set of codes. A mapping function can convert the first character of a Penn tag:
from nltk.corpus import wordnet
def penn_to_wordnet(tag):
if tag.startswith("J"):
return wordnet.ADJ
if tag.startswith("V"):
return wordnet.VERB
if tag.startswith("N"):
return wordnet.NOUN
if tag.startswith("R"):
return wordnet.ADV
return wordnet.NOUNThis mapping is useful, but it does not repair an incorrect tag. If a tagger labels a verb as a noun, the lemmatiser may return the wrong form.
Limits and failure modes
Lemmatisation works best for words covered by its lexicon. It may fail for:
- New product names, usernames, and technical terms.
- Spelling errors and informal text.
- Ambiguous words without reliable part-of-speech tags.
- Languages or dialects that the chosen lexicon does not support.
- Words whose meaning depends on sentence context.
Lemmatisation also does not solve word-sense disambiguation. The lemma may be correct while the intended meaning remains unknown. For example, both meanings of bank normally have the lemma bank.
Choose lemmatisation when readable base forms, grammatical accuracy, or linguistic analysis matter. Choose stemming when speed and simple matching matter more than valid words. For modern neural models that use subword tokenisation, neither step may be needed because the model learns representations for word pieces directly.
Exercise
Why might lemmatizer.lemmatize("running") return running, while lemmatizer.lemmatize("running", pos="v") returns run?
Show answer
Without a part of speech, WordNet assumes a noun. With pos="v", it interprets running as a verb and looks up its verb lemma, run.