v9.1.1
Menu

BIO Tag Converter

easyPRONamed Entity RecognitionNLP 2: Applications

Convert entity annotations to BIO (Beginning-Inside-Outside) tag sequences.

In NER, each token gets a tag:

  • B-TYPE: Beginning of an entity of type TYPE
  • I-TYPE: Inside (continuation of) an entity of type TYPE
  • O: Outside any entity

Input:

  • tokens: a list of token strings
  • entities: a list of (start_index, end_index, entity_type) tuples (may be empty)

start_index and end_index are inclusive token indices.

Output: A list of BIO tags, one per token.

Example

Input
John Smith lives in New York City
2
0 1 PER
4 6 LOC
Output
['B-PER', 'I-PER', 'O', 'O', 'B-LOC', 'I-LOC', 'I-LOC']
Reasoning
  • Inputs: tokens is the sentence split into 7 words, positions 0 to 6: John, Smith, lives, in, New, York, City. entities holds two spans, each a (start, end, type) triple with both ends included: positions 0 to 1 are a person (PER), positions 4 to 6 are a location (LOC).
  • First span: it covers 2 tokens. The first, John, opens the entity and gets B-PER; the second, Smith, continues it and gets I-PER.
  • Second span: it covers 6−4+1=36 - 4 + 1 = 3 tokens. New opens it as B-LOC, then York and City both continue it as I-LOC.
  • Everything else: lives and in sit in no span, so they are O.
  • Output: one tag per token, in token order, so the list has length 7: ['B-PER', 'I-PER', 'O', 'O', 'B-LOC', 'I-LOC', 'I-LOC'].
  • Notice that a one-token span, such as a span from 3 to 3, produces only a B- tag and no I- tags, and a sentence with no spans is all O.

Constraints

  • Indices are 0-based and inclusive
  • Entities do not overlap
  • Tag format: B-TYPE for first token, I-TYPE for remaining tokens of entity
  • All non-entity tokens get tag "O"

Learn this in

More from NLP 2: Applications

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.