Sub topic 4
Morphological features with `token.morph`
Assume you know basic Python, can run a spaCy pipeline, and understand nouns, verbs, subjects, and objects. This chapter uses the trained model en_core_web_sm.
Morphology describes the grammatical form of a word. It records features such as tense, number, person, gender, and case. In spaCy, a token's morph attribute stores these features as a MorphAnalysis object.
Reading morphological features
Load a document and inspect each token:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The researchers were testing new models.")
for token in doc:
print(f"{token.text:12} POS={token.pos_:6} morph={token.morph}")A typical result includes entries like these:
| Token | POS | Morphological features | Column | Column |
|---|---|---|---|---|
| researchers | NOUN | Number=Plur | ||
| were | AUX | Mood=Ind\ | Tense=Past\ | VerbForm=Fin |
| testing | VERB | Aspect=Prog\ | Tense=Pres\ | VerbForm=Part |
| models | NOUN | Number=Plur |
The exact output depends on the language and trained pipeline. English has fewer visible inflections than languages such as Spanish, German, or Russian. For example, English usually marks plural with -s, but it rarely marks noun case.
A feature has a name and one or more values. Number=Plur means plural number. Tense=Past means past tense. VerbForm=Part means participle form. Multiple features are separated by a vertical bar.
Querying individual features
Use token.morph.get() to retrieve a feature. It returns a list because some grammatical features can have multiple values.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Cats chased mice. The cat sleeps.")
for token in doc:
number = token.morph.get("Number")
tense = token.morph.get("Tense")
if number or tense:
print(token.text, "number=", number, "tense=", tense)You can also test whether a feature has a particular value:
for token in doc:
if "Number=Plur" in str(token.morph):
print(token.text, "is plural")For more structured code, prefer get():
plural_nouns = [
token.text
for token in doc
if token.pos_ == "NOUN" and token.morph.get("Number") == ["Plur"]
]
print(plural_nouns)The feature names follow the Universal Dependencies feature inventory. Common names include Number, Person, Tense, Mood, Aspect, Case, Gender, and VerbForm.
Why morphology matters
Morphology helps distinguish grammatical roles that surface text alone cannot reliably identify. It can support rules such as:
- Find plural nouns for agreement checks.
- Separate past-tense verbs from present-tense verbs.
- Detect infinitives with
VerbForm=Inf. - Filter pronouns by person or number.
- Compare grammatical patterns across languages.
The relationship between a token and its surrounding words is often useful. For example, a finite verb may agree with a subject in number, but the parser and tagger can still make mistakes.
Common errors
Do not assume every token has every feature. English nouns may have Number, while punctuation usually has no useful morphology. A feature can also be absent because the model does not need it for that token.
Do not confuse morphology with the lemma. The lemma is the dictionary form, such as run for running. Morphology describes the form, such as progressive aspect or participle verb form. Use both when a rule needs meaning and grammar.
Check the model's output on representative examples before building an extractor around it. Test contractions, irregular forms, names, headlines, and informal text. If a feature is central to your application, log uncertain or unexpected cases for review.
Exercise
Write a list comprehension that returns the text of all tokens in doc whose morphological feature VerbForm is Inf.
Show answer
[token.text for token in doc if token.morph.get("VerbForm") == ["Inf"]]