PIXELBANKv9.1.0
Menu

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:

Input:
John Smith lives in New York City today
B-PER I-PER O O B-LOC I-LOC I-LOC O
Output:
[('John Smith', 'PER'), ('New York City', 'LOC')]
Reasoning:

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
🔒

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

0/0
Run code to see test results.