Named-entity recognition
Named-entity recognition pulls structured facts — people, organizations, places, dates and amounts — out of free text. Learn how it works as token labeling with the BIO scheme, compare rules with learned models, and evaluate an extractor the way production teams do.
In this lecture you will
- Explain what named-entity recognition extracts and where it is used
- Frame entity extraction as sequence labeling with B-, I- and O tags
- Compare rule-based, statistical and transformer approaches
- Evaluate an extractor with precision, recall and F1 under strict and lenient matching
A support ticket says “Acme Corp was double-billed $4,200 on 30 June”. A person reads the facts instantly: a customer, an amount, a date. Named-entity recognition (NER) teaches software to do the same — to find the spans of text that name things and label each with a type — so that free text can be searched, filtered, joined with databases and acted on.
What counts as an entity
Common entity types are people, organizations, locations, dates, amounts of money and products. Real projects add their own: drug names in medical notes, part numbers in maintenance logs, clause types in contracts. Turn the types on and off below, and switch to the token view to see what a model actually predicts.
The extracted records at the bottom are what downstream systems consume: structured facts pulled out of a sentence.
- Entity
- A span of text that refers to a specific thing, such as “Northwind Labs” or “3 March 2025”.
- Entity type
- The category of an entity: person, organization, location, date, money, product and so on.
- Span
- A run of consecutive tokens, identified by its start and end positions.
- Entity linking
- The next step after NER: connecting “Acme Corp” to the right record in a database or knowledge base.
NER as sequence labeling
Models do not draw boxes around text. They solve an easier problem: give every token one tag. The widely used BIO scheme marks the first token of an entity with B- (begin), the following tokens of the same entity with I- (inside), and everything else with O (outside). Adjacent B-/I- tags are then merged back into spans.
- One tag per tokenThe sentence is split into tokens. The model’s job is to give each token exactly one tag.
- The BIO schemeB- begins an entity, I- continues it and O means the token is not part of any entity. “Acme Corp” becomes B-ORG I-ORG.
- Context from both sidesModern taggers read the whole sentence in both directions before deciding. “June” on its own could be a name; after “by 30” it is clearly part of a date.
- Tags for every tokenThe model assigns all fourteen tags. Most tokens are O — entities are a small fraction of any text.
- Merge into entitiesConsecutive B-/I- tags of the same type are merged into spans, giving four entities: two organizations, an amount and a date.
Three ways to build an extractor
Rules and dictionaries. Regular expressions catch well-formatted things — amounts, dates, e-mail addresses, ticket numbers — and word lists catch known names. Rules are fast, predictable and need no training data, but they cannot see context and break on anything they have not been told about.
Statistical sequence models. Conditional random fields (CRFs) learn from labeled examples which features predict each tag — capitalization, word shape, neighboring words. For a decade they were the standard approach.
Transformers. Fine-tuned models such as BERT read the whole sentence at once and give each token a contextual embedding, then classify it. With a few thousand labeled sentences they usually outperform everything else — and pretrained models for the common types are free to download.
import spacy
nlp = spacy.load("en_core_web_trf") # a pretrained transformer pipeline
doc = nlp("Acme Corp agreed to pay $4.2 million to Globex by 30 June.")
for ent in doc.ents:
print(ent.text, ent.label_, ent.start_char, ent.end_char)
# Acme Corp ORG 0 9
# $4.2 million MONEY 24 36
# Globex ORG 40 46
# 30 June DATE 50 57
Evaluating an extractor
NER is evaluated on entities, not tokens, because a system that gets most tokens right can still produce broken spans. Each predicted span is compared with the human “gold” labels:
- Precision — of the spans the model predicted, how many were correct?
- Recall — of the real entities, how many did the model find?
- F1 — the harmonic mean of the two, a single score that punishes a weakness in either.
The deciding question is what counts as correct. Strict scoring demands exact boundaries and the right type; lenient scoring accepts any overlap with the right type. Always report which one you used.
Compare the rule-based system with the transformer, then switch between strict and lenient scoring. Deleting a spurious span raises precision; deleting a correct one lowers recall.
Notice how much the rule-based system gains under lenient scoring: its main weakness is boundaries (“Priya” instead of “Priya Sharma”), while its type errors — “June” as a person, “Skyline” as a place — are exactly the context mistakes described above.
Entity extraction turns documents into facts. The last lecture of this module turns a whole collection of documents into something people can ask questions of: semantic search.
Key takeaways
- Named-entity recognition finds spans of text that refer to things such as people, organizations, places, dates and amounts, and assigns each a type.
- Models tag every token with B- (begins an entity), I- (inside it) or O (outside), then merge the tags into spans.
- Context decides the type — “June” is a date in “by 30 June” and a person in “June Park said” — which is why learned models beat word lists.
- Evaluate at the entity level with precision, recall and F1, and state whether boundaries must match exactly.
Check your understanding
4 questions · answers explainedQ1In the BIO scheme, how is the two-word organization “Acme Corp” tagged?
Q2A model predicts “March 2025” as a DATE, but the human label is “3 March 2025”. Under strict scoring this counts as…
Q3Why do dictionary-based extractors struggle with a sentence like “June Park joined Acme in June”?
Q4A contract-review team says missing a payment amount is far worse than flagging a few extra spans. Which metric should they watch most closely?