Entity Span Extractor
Extract entity spans from a BIO-tagged token sequence.
Given tokens and their BIO tags, extract all entity spans as (text, type) tuples, where text is the concatenation of entity tokens joined by spaces.
Input format:
- Line 1: Space-separated tokens
- Line 2: Space-separated BIO tags
Output: A list of tuples: (entity_text, entity_type), in order of appearance.
Example
John Smith lives in New York City today B-PER I-PER O O B-LOC I-LOC I-LOC O
[('John Smith', 'PER'), ('New York City', 'LOC')]Scan through tags:
- "John" B-PER: start new PER entity
- "Smith" I-PER: continue PER entity
- "lives" O: end entity -> emit ("John Smith", "PER")
- "in" O: nothing
- "New" B-LOC: start new LOC entity
- "York" I-LOC: continue LOC entity
- "City" I-LOC: continue LOC entity
- "today" O: end entity -> emit ("New York City", "LOC")
Constraints
- B-TYPE starts a new entity (even if previous tag was I-TYPE of different type)
- I-TYPE continues the current entity only if it matches the type of the previous B/I tag
- If I-TYPE follows O or mismatches type, treat it as B-TYPE
- Output entities in order of appearance