Sub topic 5
Lemmas and Why They Depend on POS
This chapter assumes basic Python, spaCy pipeline use, and grammar terms such as noun, verb, subject, and object. You should also know that a spaCy Doc contains Token objects produced by a trained language model.
What a lemma is
A lemma is the dictionary form of a word. Lemmatization maps related word forms to a shared base form.
For example:
walk,walks,walked, andwalkingcan have the lemmawalk.carscan have the lemmacar.wascan have the lemmabe.
The surface form is the text that appears in the document. The lemma is the normalized form that can help you group word forms during search or analysis.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The researchers analyzed results and published findings.")
for token in doc:
print(f"{token.text:12} POS={token.pos_:6} lemma={token.lemma_}")The lemma_ attribute returns the lemma as a string. The related lemma attribute returns spaCy's internal integer identifier for that lemma.
Lemmas are useful when exact spelling should not determine whether two words match. A search for the lemma publish, for example, can include published and publishes.
Why part of speech matters
The same spelling can have different lemmas depending on its part of speech, or POS. A POS is a grammatical category such as noun, verb, adjective, or adverb.
Consider saw:
- As a verb,
sawis the past tense ofsee, so its lemma is usuallysee. - As a noun,
sawnames a cutting tool, so its lemma issaw.
A lemmatizer therefore needs grammatical information. The POS tag helps the model decide which dictionary entry and rule apply.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("I saw a saw. They saw the wood.")
for token in doc:
if token.text.lower() == "saw":
print(token.text, token.pos_, token.tag_, token.lemma_)The exact result depends on the installed model. A statistical pipeline may make a POS or lemma error, especially with short or ambiguous sentences.
Lemmatization in the pipeline
spaCy components run in an order. Tokenization creates tokens first. The tagger and parser then analyze their grammatical roles. The lemmatizer uses those analyses, along with vocabulary and language-specific rules.
If you disable or remove a component that supplies information needed by the lemmatizer, lemma quality can change. Check the pipeline before relying on the output.
import spacy
nlp = spacy.load("en_core_web_sm")
print(nlp.pipe_names)
doc = nlp("The children were running quickly.")
for token in doc:
print(token.text, token.pos_, token.morph, token.lemma_)Checking lemmas before using them
Lemmas are model output, not guaranteed truth. Inspect representative examples from your own data. Include ambiguous words, irregular forms, domain terms, names, spelling errors, and informal text.
You can compare a lemma with the token's POS and morphology:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The mice were running.")
for token in doc:
print(
token.text,
"| POS:", token.pos_,
"| morph:", token.morph,
"| lemma:", token.lemma_,
)The morph attribute contains morphological features such as tense, number, and person. These features help explain why a token receives a particular lemma, but they do not replace manual validation.
Exercise
Why might the word "saw" receive different lemmas in "I saw it" and "a saw"?
Show answer
The sentences use different POS categories. In the first sentence, "saw" is a verb related to "see"; in the second, it is a noun naming a tool.