Sub topic 3
Part-of-Speech Tags: pos_ Versus tag_
This chapter assumes you can load a spaCy pipeline and iterate over a Doc, as covered earlier in the tutorial. It also assumes you know basic grammar terms such as noun, verb, and adjective. Here you will learn why every token carries two part-of-speech labels, what each one means, and when to use which.
Two labels on every token
A spaCy Token exposes two part-of-speech attributes:
token.pos_is the coarse-grained tag. It comes from the Universal Dependencies (UD) tag set, which has 17 tags and is shared across languages.token.tag_is the fine-grained tag. It comes from a tag set specific to the language and training corpus. English pipelines use the Penn Treebank tags, such asNN,NNS,VBD, andVBZ.
Run the following with the small English pipeline (installed with python -m spacy download en_core_web_sm):
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.dep_, token.head.text)The first three columns of a typical result look like this. Exact values can differ slightly between model versions.
| Token | pos_ | tag_ |
|---|---|---|
| The | DET | DT |
| quick | ADJ | JJ |
| fox | NOUN | NN |
| jumps | VERB | VBZ |
| over | ADP | IN |
| dog | NOUN | NN |
| . | PUNCT | . |
Each pos_ value is a broad class. Each tag_ value adds detail. VBZ says the verb is a third-person singular present form. NN says the noun is a singular common noun.
Looking up labels
You do not need to memorize either tag set. spacy.explain returns a short description, or None if the label is unknown.
import spacy
print(spacy.explain("nsubj"))
print(spacy.explain("VERB"))
print(spacy.explain("NNS"))This prints nominal subject, verb, and noun, plural. Use it whenever you meet an unfamiliar label in output.
The integer and string forms
Every attribute with a trailing underscore returns a readable string. The same name without the underscore returns an integer ID from spaCy's string store.
token.pos_returns"VERB", andtoken.posreturns an integer.token.tag_returns"VBZ", andtoken.tagreturns an integer.
The integers are fast to compare and are used internally. In your own code, prefer the string forms, because they are readable and stable across runs.
Where the labels come from
In the v3 English pipelines, two components cooperate:
- The
taggeris a statistical component that predictstag_from context. - The
attribute_rulerapplies mapping rules that setpos_(and some morphological features) from the predictedtag_plus a few word-specific exceptions.
You can check which components are active:
import spacy
nlp = spacy.load("en_core_web_sm")
print(nlp.pipe_names)A typical list is ['tok2vec', 'tagger', 'parser', 'attribute_ruler', 'lemmatizer', 'ner']. The consequence matters in practice. If you disable tagger, tag_ is not predicted, and pos_ typically comes back empty too, because the rules that fill it depend on the tags. Disabling components is covered in a later chapter.
Why the same word gets different tags
Part-of-speech tagging uses context. The word form alone does not decide the tag. The tagger looks at neighboring words, so one string can receive different labels:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("I read a book, and then I will book a flight.")
for token in doc:
if token.text == "book":
print(token.i, token.text, token.pos_, token.tag_)The first book should come out as NOUN / NN, and the second as VERB / VB. The two tokens have the same text and the same lemma string, but different roles. This is why later steps such as lemmatization depend on POS.
Choosing between pos_ and tag_
Use pos_ when you want broad categories that work across languages and models. Use tag_ when you need distinctions that the coarse tag drops.
| Goal | Better attribute | Reason |
|---|---|---|
| Keep only nouns, verbs, and adjectives as keywords | `pos_` | Few stable categories |
| Separate past tense from present tense verbs | `tag_` | `VBD` versus `VBZ` or `VBP` |
| Find proper nouns | `pos_` (`PROPN`) | Simple and portable |
| Separate singular from plural nouns | `tag_` (`NN` versus `NNS`) | The coarse tag is `NOUN` for both |
| Code that must run on several languages | `pos_` | Tag sets differ by language |
For features such as tense, number, or case, the token.morph attribute is often cleaner than parsing tag strings. The next chapters cover it.
A filtering example that uses both attributes:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The engineers fixed three bugs, and the manager approves the release.")
content = [t.text for t in doc if t.pos_ in {"NOUN", "PROPN", "VERB", "ADJ"}]
past_verbs = [t.text for t in doc if t.tag_ == "VBD"]
print(content)
print(past_verbs)The first list holds the content words. The second holds verbs tagged as simple past tense, which here should be fixed.
Tags you may not expect
Some categories surprise people the first time they appear.
- AUX versus VERB. Auxiliary verbs such as
will,has, or theisin "is running" getpos_ofAUX. The main verb getsVERB. If you filter onVERBalone, you miss auxiliaries. If you want every verb form, check both. - PART. The word
toin "to run" and the possessive'sgetPART, the particle tag. The prepositiontoin "went to Paris" getsADP. - PROPN versus NOUN. Capitalized names usually get
PROPN. Sentence-initial or unusual capitalization can confuse the tagger. - SPACE. Whitespace tokens, such as a double space or a newline, are tokens too. They get
pos_ofSPACEand a special tag such as_SP. Filter them out before counting words. - PUNCT. Punctuation has
pos_ofPUNCT, but itstag_varies. A period keeps., a comma gets,, and quotes use''and ```. Usepos_ == "PUNCT"` when you want to drop all punctuation. - X. The tag
Xmarks tokens the model cannot classify, such as foreign words or fragments.
Errors and edge cases
A tagger is a statistical model. It predicts the most probable tag given context, and it can be wrong. The typical failure patterns are:
- Domain shift. A model trained on news and web text can mislabel tokens in tweets, code comments, medical notes, or product titles. Short headlines such as "Fed raises rates" are hard because
raisescan be a noun or a verb. - Noun-verb ambiguity. Words such as
run,book,plan, andpermitcan be either. Wrong choices often appear in short, fragmentary text. - Unusual casing. All-caps or all-lowercase text removes cues the tagger relies on for proper nouns.
- Tokenization problems. A wrong token boundary produces wrong tags, because the tagger sees the tokens it is given.
Because of this, treat tags as evidence and not as ground truth. Check a sample of your own data. Print tokens in a table, look at the rare tags, and read the cases where a downstream step fails. A useful habit is to count tags over a corpus and look for anomalies:
import spacy
from collections import Counter
nlp = spacy.load("en_core_web_sm")
texts = ["Fed raises rates.", "She raises goats.", "Plan the trip."]
counts = Counter()
for doc in nlp.pipe(texts):
counts.update((t.pos_, t.tag_) for t in doc)
for (pos, tag), n in counts.most_common():
print(pos, tag, n)A rare or surprising (pos, tag) pair is a pointer to a place where the model may have struggled.
Summary of what to remember
pos_is the coarse UD tag andtag_is the fine-grained, language-specific tag.- In v3 English pipelines, the
taggerpredictstag_, and theattribute_rulerfills inpos_from it. - Use
spacy.explainto decode any label. - Prefer the underscore (string) attributes in your code.
- Check tag quality on your own text before you build on it.
Exercise
You run a pipeline on the sentence "She will plan the trip." and want a list of all verb forms, including auxiliaries. Which pos_ values must you test for, and why does testing only VERB fall short?
Show answer
Test for both VERB and AUX. The main verb plan gets pos_ of VERB, but the auxiliary will gets AUX, so a filter on VERB alone would miss it. For example: [t.text for t in doc if t.pos_ in {"VERB", "AUX"}].
Exercise
A colleague writes if token.tag_ == "NN" to find nouns and plans to reuse the code on a German model. Name two problems with this approach and give a more portable alternative.
Show answer
First, NN is a Penn Treebank tag for singular common nouns, so the check misses plural nouns (NNS) and proper nouns. Second, German models use a different tag set, so the string does not carry the same meaning. A portable alternative is token.pos_ in {"NOUN", "PROPN"}, which uses the shared Universal POS tags.