Sub topic 1
What Named Entity Recognition Predicts
Prerequisites
This chapter assumes that you know basic Python, including loops and functions. You should also be familiar with tokens, which are the words or pieces of words that a text-processing system reads. A general understanding of neural-network classifiers is useful, but no model training is required here.
The NER task
Named entity recognition (NER) identifies spans of text that refer to specific categories of things. A span is a continuous section of the original text. The model predicts both the span's boundaries and its entity type.
For example:
Apple opened an office in Dublin in 2024.A possible prediction is:
| Span | Entity type |
|---|---|
| Apple | ORG |
| Dublin | GPE |
| 2024 | DATE |
ORG means organization. GPE means geopolitical entity, such as a country, city, or state. DATE means a date expression. The available labels depend on the dataset and the task.
NER does not usually determine that Apple opened the office, or that Dublin is related to the office. Those are relation-extraction or information-extraction tasks. NER first marks the objects that later tasks may use.
Entity boundaries matter
A prediction is correct only when its boundaries and label are correct. In this sentence:
Satya Nadella leads Microsoft.Satya Nadella should be one PERSON entity. Predicting only Nadella misses part of the span. Predicting Satya Nadella leads includes text that is not part of the name.
Datasets can disagree about boundaries. For example, one dataset might label University of Oxford as an organization, while another might label only Oxford. Always treat the annotation rules as part of the task definition.
NER is also context-sensitive. The word Amazon can refer to an organization, a river, or a region. A system must use surrounding words to choose a label. Some strings are not named entities at all. In I bought a laptop, laptop might be a product in one application and ordinary text in another.
BIO tagging
Many NER systems represent predictions with BIO tags. BIO means:
B-TYPE: the beginning of an entity of typeTYPEI-TYPE: a continuation of that entityO: a token outside any entity
For the sentence Ada Lovelace worked in London, the tags might be:
| Token | Tag |
|---|---|
| Ada | B-PERSON |
| Lovelace | I-PERSON |
| worked | O |
| in | O |
| London | B-GPE |
The B- tag separates adjacent entities with the same type. In New York London, a dataset could represent two entities as B-GPE I-GPE B-GPE.
tokens = ["Ada", "Lovelace", "worked", "in", "London"]
tags = ["B-PERSON", "I-PERSON", "O", "O", "B-GPE"]
entities = []
start = None
label = None
for index, tag in enumerate(tags + ["O"]):
if tag.startswith("B-"):
if start is not None:
entities.append((" ".join(tokens[start:index]), label))
start = index
label = tag[2:]
elif tag.startswith("I-"):
if start is None or tag[2:] != label:
if start is not None:
entities.append((" ".join(tokens[start:index]), label))
start = index
label = tag[2:]
elif start is not None:
entities.append((" ".join(tokens[start:index]), label))
start = None
label = None
print(entities)
# [('Ada Lovelace', 'PERSON'), ('London', 'GPE')]What NER does not guarantee
A model's label is a prediction, not a fact. It can miss entities, assign the wrong type, or choose incorrect boundaries. Confidence scores help identify uncertain predictions, but they do not replace evaluation against labelled examples.
Exercise
In the sentence "Tesla hired Maria in Berlin", what BIO tags could represent Tesla as an organization, Maria as a person, and Berlin as a geopolitical entity?
Show answer
Tesla: B-ORG, hired: O, Maria: B-PERSON, in: O, Berlin: B-GPE.