BIO Tag Converter
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
John Smith lives in New York City 2 0 1 PER 4 6 LOC
['B-PER', 'I-PER', 'O', 'O', 'B-LOC', 'I-LOC', 'I-LOC']
- Inputs:
tokensis the sentence split into 7 words, positions 0 to 6:John,Smith,lives,in,New,York,City.entitiesholds 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 getsB-PER; the second,Smith, continues it and getsI-PER. - Second span: it covers tokens.
Newopens it asB-LOC, thenYorkandCityboth continue it asI-LOC. - Everything else:
livesandinsit in no span, so they areO. - 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 noI-tags, and a sentence with no spans is allO.
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"