Sub topic 2
Tokens and Their Attributes
Assume you know basic Python, can run a spaCy pipeline, and understand terms such as noun, verb, subject, and object. This chapter uses the trained English model en_core_web_sm.
What a token is
A token is one unit produced when spaCy splits text. A token is often a word, but it can also be punctuation, a number, or part of a contraction.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The quick fox doesn't jump.")
for token in doc:
print(repr(token.text))The Doc object stores the complete processed text. Iterating over it returns Token objects in their original order. In this example, punctuation and the parts of "doesn't" may receive their own tokens.
A token keeps its original spelling in token.text. This matters when you need to display text exactly as the user entered it. For normalized processing, use attributes such as lower_ or lemma_.
The main token attributes
spaCy attaches linguistic predictions to tokens. The most useful attributes are shown below.
| Attribute | Meaning | Example value |
|---|---|---|
| `text` | Original token text | `running` |
| `pos_` | Coarse universal part of speech | `VERB` |
| `tag_` | Fine-grained, language-specific tag | `VBG` |
| `lemma_` | Base dictionary form | `run` |
| `dep_` | Dependency relation to another token | `ROOT` |
| `head` | Token this token depends on | `runs` |
Attributes ending in an underscore return readable strings. Their versions without an underscore, such as token.pos, return integer IDs used internally.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The quick brown fox jumps over the lazy dog.")
for token in doc:
print(
token.text,
token.pos_,
token.tag_,
token.lemma_,
token.dep_,
token.head.text,
)token.head is another Token, so you can inspect its attributes. The head is usually the word that controls the token's grammatical role. For example, the noun fox may have the verb jumps as its head.
Coarse and fine-grained part-of-speech tags
pos_ uses the Universal Dependencies vocabulary. Common values include:
NOUN: common nounPROPN: proper nounVERB: verbADJ: adjectiveADV: adverbDET: determinerADP: adposition, such as a prepositionPUNCT: punctuation
tag_ gives a more detailed tag based on the language and model. For English, NN usually means a singular noun, while NNS means a plural noun. VBG commonly identifies an -ing verb form.
Use spacy.explain when a label is unfamiliar.
import spacy
print(spacy.explain("nsubj"))
print(spacy.explain("VERB"))
print(spacy.explain("NNS"))The function returns a short explanation or None when the label is unknown. Do not assume that every language uses the same fine-grained tag set.
Reading attributes safely
Attributes are most useful when combined. For example, this code finds tokens predicted as plural nouns and prints their lemmas.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Several researchers published papers.")
plural_nouns = [
(token.text, token.lemma_)
for token in doc
if token.pos_ == "NOUN" and token.tag_ == "NNS"
]
print(plural_nouns)A prediction can be wrong because of spelling mistakes, unusual wording, ambiguity, or a domain the model did not see during training. Inspect representative examples before trusting an extraction rule.
Exercise
Which attribute should you use to preserve the original spelling, and which attribute usually gives a base dictionary form?
Show answer
Use token.text for the original spelling and token.lemma_ for the base dictionary form.