v9.1.1
Menu

BIO Entity Span Extractor

easyFREENamed Entity RecognitionNLP 2: Applications

Given a sentence's words and their BIO tags, extract all entity spans with their types.

Input:

  • words: a list of words (the sentence, already split on spaces)
  • tags: a list of BIO tags of the same length, one per word (B-TYPE, I-TYPE, or O)

Output: A list of strings, one per entity in sentence order, each formatted as "WORDS: TYPE" (the entity's words separated by single spaces). If there are no entities, return ["No entities found"].

Example

Input
John Smith works at New York
Output
John Smith: PERSON
New York: LOC
Reasoning
  • words is the sentence split into 6 words, and tags holds one BIO label per word at the same position: B-PERSON, I-PERSON, O, O, B-LOC, I-LOC.
  • Each label has a prefix (B begins an entity, I continues the entity just begun, O is outside any entity) and, for B and I, a type after the hyphen.
  • Words 1 and 2: "John" begins a PERSON and "Smith" continues it. Word 3, "works", is O, which closes the span, giving "John Smith" with type PERSON.
  • Word 5, "New", begins a LOC, "York" continues it, and the sentence ends, which also closes the span: "New York" with type LOC.
  • The result is a list of two strings in sentence order, each the span's words separated by single spaces, then a colon, a space and the type:
    John Smith: PERSON
    New York: LOC
  • Notice that a span also ends when a fresh B appears, so tags B-PERSON, B-PERSON mean two one-word people, not one. If every tag is O, the list holds just No entities found.

Constraints

  • B-TYPE starts a new entity
  • I-TYPE continues the previous entity
  • O means no entity
  • Output format: "WORDS: TYPE" per line

Learn this in

More from NLP 2: Applications

solution.py

Test results

Sign in to run your code against the test cases.