Named entity recognition (NER) answers a specific question about new text: Which words name a person, organization, or place, and where do those words appear? An extractor returns one record per mention, not one category for the whole document. A newsroom could use those records to index stories by the organizations they mention; the character positions let an editor check each highlighted name in context.
From a sentence to labeled spans
Take this fictional news brief as the entire input, including the period:
Nora Bell left Cedar Works in Lima with Jo.Our manual reference uses three labels: PER for a person, ORG for an organization, and LOC for a location. In this story, Cedar Works is the employer and Lima is the city. Count characters in the original string from zero, including spaces. In [start, end), start is included and end is excluded: text[15:26] gives Cedar Works without the following space.
| Mention | Character range | Type |
|---|---|---|
| Nora Bell | [0, 9) | PER |
| Cedar Works | [15, 26) | ORG |
| Lima | [30, 34) | LOC |
| Jo | [40, 42) | PER |
Making this reference is entity annotation. Extraction runs in the opposite direction: the system receives the sentence without the table and predicts its own spans. Neither step establishes which real person a name identifies; resolving a mention to a particular record is entity linking.
Run a three-entry rule baseline
This Python 3 example uses a case-sensitive dictionary and matches whole words. Each entry has one fixed type. Paste the code into a Python interpreter or save it as a script. It needs no downloads.
import re
text = 'Nora Bell left Cedar Works in Lima with Jo.'
patterns = [('Nora Bell', 'PER'), ('Cedar', 'ORG'), ('Lima', 'ORG')]
def extract(line):
return sorted((m.start(), m.end(), label) for name, label in patterns
for m in re.finditer(r'\b' + re.escape(name) + r'\b', line))
print(extract(text))Output, as (start, end, type) tuples:
[(0, 9, 'PER'), (15, 20, 'ORG'), (30, 34, 'ORG')]The first prediction extracts the full name. The second stops after Cedar: its type is right but its boundary is wrong. The third locates Lima exactly but calls the city an organization. There is no prediction for Jo, which is not in the dictionary.
What counts as an exact match?
Count a hit only when both offsets and the type agree. There is one true positive (Nora Bell), two false positives (the incorrect predictions for Cedar and Lima), and three false negatives (the expected records for Cedar Works, Lima, and Jo). A near miss leaves one prediction unsupported and one reference record unrecovered; it does not earn partial credit.
- Precision: 1 exact match / 3 predictions = 33.3%.
- Recall: 1 exact match / 4 reference mentions = 25%.
Adding Jo to the dictionary would recover this mention but would not fix the other two errors. Replacing Cedar with Cedar Works would fix this boundary; it would not tell the system whether a future mention of Cedar refers to an employer. These numbers describe one deliberately constructed sentence, not performance on unseen documents.
When would a trained recognizer help?
A dictionary is easy to inspect when the vocabulary is stable. A trained recognizer can use surrounding words and examples of name structure to predict types and boundaries even for names absent from a lookup list. For example, in Lima gives a location cue that this rule ignores. A learned system can still miss unfamiliar names or choose the wrong boundary. We ran only the rules above, not a trained model; there is no model benchmark here.
For an implementation reference, spaCy’s EntityRecognizer documentation describes the labeled, non-overlapping spans it stores in Doc.ents. Its Span API exposes start_char and end_char; token indices are not interchangeable with these character offsets. For context, see real-world NLP applications or compare entity labels with other layers of linguistic annotation.
For a book-length treatment of transformer-based NLP, including NER: As an Amazon Associate I earn from qualifying purchases. Natural Language Processing with Transformers, Revised Edition, by Lewis Tunstall, Leandro von Werra, and Thomas Wolf (paperback).



