Sub topic 11
Why Part of Speech Matters for Lemmas
Assume you know basic Python, regular expressions, and common grammar terms such as noun, verb, and adjective. You should also understand that an NLP task may involve search, classification, or text analysis.
A lemma is the dictionary form of a word. For example, studies can have the lemma study, and running can have the lemma run. A lemmatiser uses a lexicon, which is a dictionary of words and their relationships. It may also need the word's part of speech, or POS. A POS describes how a word functions in a sentence, such as noun, verb, adjective, or adverb.
The same spelling can have different lemmas
A word's spelling does not always identify its base form. Consider these sentences:
- The dog
leavesthe garden. - The tree has many
leaves.
In the first sentence, leaves is a verb. Its lemma is leave. In the second sentence, it is a plural noun. Its lemma is leaf.
The word meeting also changes according to its role:
- We are
meetingat noon. The lemma ismeet. - The
meetingstarts at noon. The lemma ismeeting.
A lemmatiser that ignores POS must guess. Many dictionary-based lemmatisers use a default POS, often noun. That default can produce a valid result for some words but the wrong result for others.
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
words = ["leaves", "meeting", "better", "running"]
for word in words:
print(
word,
"as noun:", lemmatizer.lemmatize(word, pos="n"),
"as verb:", lemmatizer.lemmatize(word, pos="v"),
"as adjective:", lemmatizer.lemmatize(word, pos="a"),
)The pos argument tells the lemmatiser which lexical entry to use. WordNet, the lexical database used here, uses these short labels:
| WordNet label | Part of speech | Example |
|---|---|---|
| `n` | noun | `leaves` as a noun |
| `v` | verb | `leaves` as a verb |
| `a` | adjective | `better` as an adjective |
| `r` | adverb | `better` as an adverb |
POS tags from a tagger
In real text, you usually do not provide POS labels by hand. A POS tagger predicts them from the surrounding words. For example, the Penn Treebank tag VBG means a present participle or gerund, while NNS means a plural noun.
WordNet uses a smaller tag set, so an NLP pipeline must map one set to the other. The following example performs that mapping.
import nltk
from nltk.stem import WordNetLemmatizer
nltk.download("wordnet")
nltk.download("averaged_perceptron_tagger_eng")
def to_wordnet_pos(tag):
if tag.startswith("J"):
return "a"
if tag.startswith("V"):
return "v"
if tag.startswith("R"):
return "r"
return "n"
sentence = "The leaves are meeting near better houses".split()
tagged_words = nltk.pos_tag(sentence)
lemmatizer = WordNetLemmatizer()
for word, tag in tagged_words:
pos = to_wordnet_pos(tag)
lemma = lemmatizer.lemmatize(word.lower(), pos=pos)
print(f"{word:10} {tag:4} {pos} {lemma}")The mapping is useful but imperfect. A tagger can make mistakes, especially with short sentences, unusual names, technical terms, or dialects. The mapping also sends every unknown tag to n, which is a practical fallback rather than a guarantee.
POS affects downstream results
Suppose a search system lemmatises every word as a noun. A query for leaves may then match documents about tree leaves but miss documents describing someone leaving. If it lemmatises every word as a verb, the opposite problem can occur.
POS-aware lemmatisation is most useful when grammatical meaning matters. It can improve search, grammatical analysis, information extraction, and feature creation for classification. It adds complexity because the system must run a tagger and handle tagging errors.
A simple decision process is:
- Tokenise the text.
- Predict a POS tag for each token.
- Convert the tag to the lemmatiser's tag set.
- Lemmatise with that tag.
- Evaluate the complete pipeline on representative examples.
Exercise
Why might lemmatising "leaves" with the noun label produce a different result from lemmatising it with the verb label?
Show answer
The noun and verb are different dictionary entries. The noun can have the lemma "leaf", while the verb can have the lemma "leave".