Introduction
News search, CV screening, medical record analysis and question answering all need to know who and what a text is about. Named-entity recognition (NER) finds mentions of people, organizations, places, dates, amounts and more: in "Ada Lovelace moved from London to New York", it should find a PERSON and two LOCATIONS. Closely related is chunking: grouping words into short phrases such as noun phrases ("the old dog"), which is often the first step to finding entities and facts.
How it works
An entity can span several words, but taggers label one word at a time. BIO tagging bridges the two:
- B-TYPE: the first word of an entity of that type.
- I-TYPE: a later word inside the same entity.
- O: outside any entity.
"New York" becomes New/B-LOC York/I-LOC. Two neighbouring entities stay separate because the second starts with B: "New York New Jersey" is B-LOC I-LOC B-LOC I-LOC. To read spans back, start a span at each B, extend it with each I of the same type, and close it at O. A stray I without a matching entity before it (a common model error) is usually treated as the start of a new span.
Ways to find entities:
- A gazetteer: a list of known names with types. Match the longest listed phrase first, so "New York" is not found as just "York".
- Rules: four-digit numbers are years, capitalized words after "Mr." are people, words before "Inc." are companies.
- Trained sequence models: HMMs, CRFs and today neural networks, which predict BIO tags from the words and their context, and generalize to names they never saw.
Chunking groups POS-tagged words into phrases with patterns. A simple noun phrase is an optional determiner, any adjectives, then one or more nouns: DET? ADJ* NOUN+. A neat trick is to write each tag as a letter and use a regular expression over the tag string.
To evaluate NER, compare predicted spans with gold spans. A predicted entity counts as correct only if its type, start and end all match. Precision, recall and F1 are computed per type and overall (micro: from total counts).
Example
import re
GAZETTEER = {"ada lovelace": "PERSON", "london": "LOC", "new york": "LOC", "acme corp": "ORG"}
words = "Ada Lovelace moved from London to New York to join Acme Corp in 1843".split()
lower = [w.lower() for w in words]
tags = ["O"] * len(words)
i = 0
while i < len(words):
for size in (2, 1):
phrase = " ".join(lower[i:i + size])
if i + size <= len(words) and phrase in GAZETTEER:
tags[i] = "B-" + GAZETTEER[phrase]
for j in range(i + 1, i + size):
tags[j] = "I-" + GAZETTEER[phrase]
i += size
break
else:
if words[i].isdigit() and len(words[i]) == 4:
tags[i] = "B-DATE"
i += 1
for w, t in zip(words, tags):
print(f" {w:9}{t}")
spans, current = [], None
for w, t in zip(words, tags):
if t.startswith("B-"):
current = [t[2:], w]
spans.append(current)
elif t.startswith("I-") and current:
current[1] += " " + w
else:
current = None
print("entities:", [tuple(s) for s in spans])
pos = ["DET", "ADJ", "NOUN", "VERB", "DET", "ADJ", "ADJ", "NOUN"]
sentence = "the old dog chased a big red ball".split()
code = "".join({"DET": "D", "ADJ": "A", "NOUN": "N"}.get(p, "x") for p in pos)
print("tag code:", code)
for m in re.finditer(r"D?A*N+", code):
print("noun phrase:", " ".join(sentence[m.start():m.end()]))Line by line
- The loop tries a two-word phrase before a one-word phrase at each position: longest match first.
- A match writes B- on its first word and I- on the rest, then jumps past it;
for ... elseruns the year rule only when no phrase matched. - "1843" is four digits, so the rule tags it B-DATE. Every other word is O.
- Reading spans: B starts a new entity, I extends the current one, O closes it.
- For chunking, the tags become the string
DANxDAAN, and the regexD?A*N+finds "the old dog" and "a big red ball".
Expected result
Ada B-PERSON
Lovelace I-PERSON
moved O
from O
London B-LOC
to O
New B-LOC
York I-LOC
to O
join O
Acme B-ORG
Corp I-ORG
in O
1843 B-DATE
entities: [('PERSON', 'Ada Lovelace'), ('LOC', 'London'), ('LOC', 'New York'), ('ORG', 'Acme Corp'), ('DATE', '1843')]
tag code: DANxDAAN
noun phrase: the old dog
noun phrase: a big red ball
In the real world
spaCy finds entities and noun chunks with trained models (shown, not run: it is not available in StepVioz's browser Python):
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Ada Lovelace moved from London to New York in 1843.")
for ent in doc.ents:
print(ent.text, ent.label_)
for chunk in doc.noun_chunks:
print(chunk.text)
It uses labels such as PERSON, GPE (countries and cities), ORG and DATE. Gazetteers are still used next to models, for product catalogues, drug names or company lists. The standard benchmark, CoNLL-2003, scores systems with exactly the entity-level F1 you will compute in the exercises.
Common mistakes
- Matching short names first, so "New York" is tagged as just "York".
- Lowercasing before NER, which throws away the strongest clue to names.
- Scoring tokens instead of entities, which gives credit for half-found names.
- Treating I- after O as an error to drop instead of deciding a consistent rule.
Best practices
- Use BIO tags consistently, and write one function to read spans that every part of the code shares.
- Combine a gazetteer for known names with a model for unknown ones.
- Report precision, recall and F1 per entity type; some types are much harder than others.
- Look at boundary errors separately: they are often fixable with rules.
Summary
NER finds names of people, organizations, places and dates. BIO tags encode spans as word labels, so sequence models can predict them, and simple rules turn tags back into spans. Gazetteers and rules work for known names; trained models generalize. Chunking finds flat phrases such as noun phrases with tag patterns, and NER is scored at the entity level with precision, recall and F1.
Quick check
Key takeaways
- BIO tags turn span finding into word-by-word labelling, so sequence models such as HMMs can do it.
- Gazetteers and rules are precise but miss names they do not list; trained models generalize.
- Entities are scored as whole spans: a partly right boundary counts as a miss.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.