NLP · Named entity recognition
NER as token-level sequence labelling with BIO tags, running spaCy's pretrained pipeline, and why entity spans make evaluation tricky.
Named entity recognition finds spans like Pandit Deendayal Energy University (ORG), Gandhinagar (GPE, a geopolitical entity) or 4 August 2023 (DATE).
It’s framed as sequence labelling with BIO tags:
Pandit/B-ORG Deendayal/I-ORG Energy/I-ORG University/I-ORG is/O in/O Gandhinagar/B-GPE
The model classifies each token, and the spans are rebuilt from the tags. Evaluation is usually span-level F1: a span counts only if both its boundaries and its type are right (the CoNLL-2003 convention).
I ran spaCy’s small English pipeline (en_core_web_sm, a CNN-based model trained on OntoNotes) on a few sentences, including one where I’d expect it to struggle.
import spacy
nlp = spacy.load("en_core_web_sm")
texts = [
"Pandit Deendayal Energy University is in Gandhinagar, Gujarat, and classes started on 4 August 2023.",
"Google DeepMind and Hugging Face released new models in London last week for $0.",
"Apple pie is my favourite, but I bought an Apple laptop.",
]
for t in texts:
doc = nlp(t)
print([(e.text, e.label_) for e in doc.ents])
doc = nlp(texts[0])
print([f"{t.text}/{t.ent_iob_}{'-' + t.ent_type_ if t.ent_type_ else ''}" for t in doc][:8])
print("label meaning GPE:", spacy.explain("GPE"))
Output:
[('Pandit Deendayal Energy University', 'ORG'), ('Gandhinagar', 'GPE'), ('Gujarat', 'GPE'), ('4 August 2023', 'DATE')]
[('London', 'GPE'), ('last week', 'DATE'), ('0', 'MONEY')]
[('Apple', 'ORG'), ('Apple', 'ORG')]
['Pandit/B-ORG', 'Deendayal/I-ORG', 'Energy/I-ORG', 'University/I-ORG', 'is/O', 'in/O', 'Gandhinagar/B-GPE', ',/O']
label meaning GPE: Countries, cities, states
The first sentence comes out right, including the four-word university name. The second shows the limits of a small model: “Google DeepMind” and “Hugging Face” aren’t recognised at all, and “$0” becomes MONEY with only the “0”. In the third, both “Apple”s are tagged ORG, although the first is a pie. Context-sensitive cases like that are where larger Transformer-based NER models do better.
Sources
- spaCy: Named Entity Recognition — Explosion
- Tjong Kim Sang & De Meulder (2003), Introduction to the CoNLL-2003 Shared Task — ACL Anthology
- Speech and Language Processing (Jurafsky & Martin), ch. 17 — Stanford University