Sub topic 3
Part-of-Speech Tags: `pos_` Versus `tag_`
This chapter assumes basic Python, familiarity with nouns and verbs, and a working spaCy installation. You should also have the English model installed:
python -m spacy download en_core_web_smA spaCy token has several linguistic attributes. Two of the most useful are token.pos_ and token.tag_. Both describe a token's grammatical role, but they use different tag systems.
Coarse universal tags
token.pos_ contains a coarse part-of-speech tag. It follows the Universal Dependencies tag set, which is designed to work across languages.
Common values include:
| `pos_` value | Meaning | Example |
|---|---|---|
| `NOUN` | Common noun | `dog` |
| `VERB` | Verb | `runs` |
| `ADJ` | Adjective | `quick` |
| `DET` | Determiner | `the` |
| `ADV` | Adverb | `quickly` |
| `PROPN` | Proper noun | `London` |
The coarse tag is useful when your code should work across languages or when you need a broad category. For example, this condition finds every verb without caring whether it is past or present tense:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The researchers published a report and presented their results.")
verbs = [token.text for token in doc if token.pos_ == "VERB"]
print(verbs)The output is likely:
['published', 'presented']The tagger determines these values from the word and its context. The word record, for example, can be a noun in “Keep a record” or a verb in “Record the meeting.”
Fine-grained tags
token.tag_ contains a finer-grained tag. For English, the small model uses the Penn Treebank tag set. These tags distinguish details that pos_ combines.
For example, several tokens can have pos_ == "NOUN" but different tag_ values:
NN: singular or mass nounNNS: plural nounNNP: singular proper nounNNPS: plural proper noun
Verb tags also show grammatical details:
VB: base formVBD: past tenseVBG: gerund or present participleVBN: past participleVBP: non-third-person presentVBZ: third-person singular present
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The teams test models, and the analyst tested one yesterday.")
for token in doc:
if token.pos_ in {"NOUN", "VERB"}:
print(token.text, token.pos_, token.tag_)Use tag_ when a rule depends on a detail such as plural nouns or past-tense verbs. Use pos_ when the broader category is enough.
Looking up tag meanings
The spacy.explain function gives a short description of many labels:
import spacy
print(spacy.explain("NNS"))
print(spacy.explain("VBD"))
print(spacy.explain("VERB"))This is useful while exploring a new model. It is not a substitute for checking the actual tags produced by that model.
How the two levels relate
The relationship is many-to-one. Several fine-grained tags usually map to one coarse tag.
Do not build important extraction rules from tags alone. A statistical tagger can misclassify unusual names, abbreviations, slang, or domain-specific terms. Inspect representative examples and test both the broad and fine-grained values before using them in production.
Exercise
You need to find plural common nouns, but not singular nouns or proper nouns. Should you test token.pos_ == "NOUN" or token.tag_ == "NNS"?
Show answer
Test token.tag_ == "NNS" for the English model, because pos_ == "NOUN" includes singular and plural common nouns.