Natural Language Processing / Tasks
Labeling whole texts and spans within them.
Reviewed by Yuvaraj
Text is messy, but two tasks tame most of it. Text classification reads a whole span of text and assigns it a single label, is this review positive or negative, is this email spam, does this support ticket belong to "billing" or "shipping"? Named Entity Recognition (NER) works one token at a time, tagging which words name a person, an organization, or a place. The first answers "what is this document about?"; the second answers "what specific things are named inside it?" Almost every applied NLP pipeline, search, moderation, analytics, knowledge extraction, is assembled from these two shapes.
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
The catch with NER is that entities span multiple tokens ("San Francisco", "Satya Nadella"), and two entities of the same type can sit side by side. A flat PER/ORG/LOC tag per token cannot say where one entity stops and the next begins. BIO tagging fixes this with three prefixes:
B- marks the Beginning token of an entity spanI- marks a token Inside (continuing) the current spanO marks a token Outside any entityCombined with an entity type you get labels like B-PER, I-PER, B-ORG, B-LOC. A person named across two tokens becomes B-PER I-PER; the next word drops back to O. This turns "find the entities" into a per-token classification problem that a sequence model can learn.
Take the sentence "Satya Nadella runs Microsoft from Redmond." Its gold BIO tags:
| Token | Tag |
|---|---|
| Satya | B-PER |
| Nadella | I-PER |
| runs | O |
| Microsoft | B-ORG |
| from | O |
| Redmond | B-LOC |
| . | O |
Decoding recovers three entities: Satya Nadella (PER), Microsoft (ORG), and Redmond (LOC). The same sequence written inline:
Satya/B-PER Nadella/I-PER runs/O Microsoft/B-ORG from/O Redmond/B-LOC ./O
NER is judged at the entity level: a predicted span is a true positive only if its boundaries and its type both match the gold span exactly. Predicting B-PER on "Satya" but missing "Nadella" is a boundary error, one false positive and one false negative, with no partial credit. Counting true positives, false positives, and false negatives over spans:
Precision asks how many predicted entities were correct; recall asks how many real entities you found; is their harmonic mean. Token-level accuracy looks deceptively high because most tokens are O, so always report entity-level for NER.
Common mistakes
O dominating, a model that predicts almost nothing still scores well, entity-level is the honest metric.I-ORG that follows an O or a B-PER; CRF decoders and constrained decoding enforce legal transitions.