Welcome back! In our previous lesson, we established what Named Entity Recognition is and familiarized ourselves with the common types of entities like PER, ORG, and LOC. We saw how NER systems can extract these entities from raw text.
This brings us to a fundamental question: how do we represent this information for a machine learning model? If we have the sentence "New York City is a major hub," how do we teach a model that "New York City" is one single location entity spanning three words? Simply labeling all three words as LOC is ambiguous. Does this refer to one three-word entity or three separate one-word entities?
In this lesson, we will solve this exact problem. You will learn how to convert raw text annotations into the BIO (Beginning, Inside, Outside) tagging scheme, the standard method for encoding entity boundaries. This is the critical data formatting step that makes it possible to train the sequence labeling models we'll encounter later in the course.
The Problem of Spans and the BIO Solution
NER is a sequence labeling task. The goal is to assign a label to every single token (or word) in a sentence. The challenge, as mentioned, is representing entities that span multiple tokens.
The BIO tagging scheme elegantly solves this by using three simple prefixes:
- B-: Marks the beginning of an entity.
- I-: Marks a token inside an entity (but not the first token).
- O: Marks a token outside of any entity.
These prefixes are combined with the entity type. For example, a person entity would be marked with B-PER and I-PER.
This image provides a clear illustration of BIO tagging in action:

To get a formal introduction to the scheme and its rationale, let's turn to an excellent article on the topic.
This article by Michael Brenndoerfer provides a thorough and clear explanation of BIO tagging. We'll start with the introduction and the core mechanics.
Please read the first two sections of the article: Start with the section titled "BIO Tagging" to understand the fundamental problem it solves. Continue with the section "The BIO Scheme". Pay close attention to the code example and visualization that break down the sentence "Barack Obama visited New York City yesterday."
As the article explains, the true power of the BIO scheme becomes apparent when dealing with adjacent entities of the same type. This is a common scenario in real-world text (e.g., lists of names or locations).
Let's look at the section of the article that deals with this specific problem.
This section highlights the critical ambiguity that BIO tagging resolves.
Read the short section "Why Not Just Use Entity Types?". The example of "Barack Obama Michelle Obama" makes the necessity of the 'B' tag crystal clear.
Without the B prefix, a sequence of PER PER PER PER is indistinguishable from a single, four-word person entity. With BIO tags, B-PER I-PER B-PER I-PER unambiguously represents two distinct two-word entities.
The 'O' Tag and Class Imbalance
The O tag is straightforward—it labels everything that isn't a named entity. However, its prevalence has an important implication for training machine learning models. Since you have a background in neural networks, you'll recognize this as a classic case of class imbalance.
The role and frequency of the 'O' tag introduce a practical challenge in NER.
Read the section on "The O Tag". Focus on the bar chart visualization and the explanation of why the high frequency of 'O' tags can be a problem for model training.
In most texts, the vast majority of words are not named entities. This means a model could achieve high accuracy simply by predicting 'O' for every token, even though it would be completely useless for our goal of extracting entities. We'll see later in the course how techniques like weighted loss functions are used to counteract this.
Converting Spans to BIO Tags
Now for the main task of this lesson: converting raw annotations into BIO format. Typically, annotation data comes in the form of tokenized text and a list of "spans". A span consists of a start token index, an end token index (exclusive), and an entity label.
For example:
- Tokens:
["John", "Smith", "works", "at", "Google", "Inc."] - Spans:
[(0, 2, "PER"), (4, 6, "ORG")]
The algorithm to convert this to BIO tags is:
- Create a list of tags, the same length as the list of tokens, and initialize all its values to
"O". - Iterate through each span
(start, end, type). - For the token at the
startindex, set its tag tof"B-{type}". - For all tokens from
start + 1toend - 1, set their tags tof"I-{type}".
Applying this to our example:
tags = ["O", "O", "O", "O", "O", "O"]- Process
(0, 2, "PER"):tags[0] = "B-PER"tags[1] = "I-PER"
- Process
(4, 6, "ORG"):tags[4] = "B-ORG"tags[5] = "I-ORG"
- Final result:
["B-PER", "I-PER", "O", "O", "B-ORG", "I-ORG"]
This process is a fundamental preprocessing step in almost any NER pipeline. The following reading provides a Python implementation of this logic.
Here you'll find a practical Python function that implements the conversion logic we just discussed.
Please read the section "Converting Spans to BIO Tags". Study the spans_to_bio function and the edge case examples that follow. Understanding this function is key to mastering today's outcome.
To reinforce this concept, here is another example from a research paper. It shows the same conversion logic in a slightly different but equivalent implementation.
[PDF] A Converting data to BIO format for NER B Metrics
This PDF supplement from a NeurIPS paper provides a concise, academic example of the same span-to-BIO conversion process.
In the PDF, please review two parts on the first page: First, read the paragraph under the heading "Converting data to BIO format for NER". Then, examine the Python code in Listing 1, which shows the get_labels function. Notice how it uses bisect to find token indices from character offsets, but the core logic of assigning B- and I- tags remains the same.
A Note on Extended Schemes: BIOES
While BIO is the most common tagging scheme, you may also encounter variants. The most popular is BIOES, which adds two more prefixes:
- E-: Marks the end of a multi-token entity.
- S-: Marks a single-token entity.
Using BIOES, our examples would look like this:
- "New York City":
B-LOC I-LOC E-LOC - "Paris":
S-LOC
The primary benefit of BIOES is that it provides a more explicit signal to the model about where entities end, which can sometimes lead to slightly better performance on boundary detection. We won't be using it in this course, but it is important to be aware of its existence.
This reading briefly introduces the BIOES scheme and provides a clear comparison.
Please skim the following two sections: The section on "Extended Tagging Schemes", focusing on the comparison table. The section "BIOES Conversion", which shows a code example comparing the output of a spans_to_bioes function with the spans_to_bio we saw earlier.
Conclusion
In this lesson, we focused on the crucial step of preparing data for an NER model. You have learned how to transform raw entity annotations into a structured format that a machine can understand.
Key Takeaways:
- NER is a sequence labeling task where each token in a text gets a tag.
- The BIO scheme is the standard for representing entity boundaries, using
B-(Beginning),I-(Inside), andO(Outside) prefixes. - This scheme unambiguously handles multi-word and adjacent entities.
- The conversion from raw spans (start index, end index, type) to BIO tags is a fundamental preprocessing step in NER.
- The prevalence of the 'O' tag creates a class imbalance, a common challenge in training NER models.
Preview of the Next Lesson:
Now that you understand how individual sentences are annotated, we need to see what a full dataset for NER looks like. In the next lesson, we will explore the structure and annotation conventions of a classic NER benchmark dataset, CoNLL-2003. This will prepare you for the next step: training and evaluating your first NER models.