Sub topic 2
Entity Spans and BIO Tags
This chapter assumes you know basic Python, tokenization, and the idea that a classifier assigns labels to inputs. Named entity recognition (NER) predicts both an entity's type and its exact boundaries in text.
Entity spans
An entity span is a substring identified by its start position, end position, and label. Most Python NLP libraries use half-open character offsets: the start index is included, but the end index is excluded.
For example, in this sentence:
Ada Lovelace worked in London.The span for Ada Lovelace is (0, 12, "PERSON"). Character positions 0 through 11 belong to the entity. The span for London is (24, 30, "GPE").
A label describes the entity category. Common labels include:
PERSON: a person's nameORG: an organizationGPE: a geopolitical entity, such as a country or cityDATE: a datePRODUCT: a named product
The label set depends on the dataset and application. A medical system might use DRUG and DISEASE, while a support system might use TICKET_ID and SOFTWARE.
Spans are useful because they preserve the exact text and boundaries:
| Text | Start | End | Label |
|---|---|---|---|
| Ada Lovelace | 0 | 12 | PERSON |
| London | 24 | 30 | GPE |
A boundary error is a real prediction error. For example, labeling Lovelace instead of Ada Lovelace does not produce the correct PERSON entity.
BIO tagging
Many token-classification models represent spans with BIO tags. BIO means:
B-X: the beginning of an entity with typeXI-X: a continuation token inside that entityO: a token outside every entity
The sentence above becomes:
| Token | Tag |
|---|---|
| Ada | B-PERSON |
| Lovelace | I-PERSON |
| worked | O |
| in | O |
| London | B-GPE |
The first token of every entity receives a B- tag, even when the entity contains only one token. A one-token entity such as London therefore uses B-GPE, not I-GPE.
BIO tags encode boundaries through transitions. A new B- tag starts an entity. An I- tag continues the current entity. An O tag ends any entity in progress.
tokens = ["Ada", "Lovelace", "worked", "in", "London"]
tags = ["B-PERSON", "I-PERSON", "O", "O", "B-GPE"]
entities = []
current_tokens = []
current_label = None
for token, tag in zip(tokens, tags):
if tag.startswith("B-"):
if current_tokens:
entities.append((" ".join(current_tokens), current_label))
current_tokens = [token]
current_label = tag[2:]
elif tag.startswith("I-") and current_tokens:
current_tokens.append(token)
else:
if current_tokens:
entities.append((" ".join(current_tokens), current_label))
current_tokens = []
current_label = None
if current_tokens:
entities.append((" ".join(current_tokens), current_label))
print(entities)The output is:
[('Ada Lovelace', 'PERSON'), ('London', 'GPE')]Common tagging errors
A model can identify the right words but assign the wrong structure. For example, B-ORG, I-PERSON gives one span conflicting entity types. Some tools repair invalid sequences; others evaluate them as errors.
Tokenization also affects alignment. If a tokenizer splits Lovelace into several pieces, the training labels must be mapped from the original word to those subword tokens. This alignment step is important when fine-tuning transformer models.
Exercise
What BIO tags represent the entity "New York City" when it is labeled GPE?
Show answer
New gets B-GPE, York gets I-GPE, and City gets I-GPE.