v9.1.1
Menu

Simple Entity Tagger

easyFREENamed Entity RecognitionNLP 2: Applications

Given a mapping of known entities to their types, tag each word in a sentence with its entity type using BIO format.

Input:

  • entities: a dict mapping lowercase words to entity types (e.g., {"john": "PERSON", "google": "ORG"})
  • sentence: the sentence to tag, with words separated by spaces

BIO format: B-TYPE for the first word of an entity, O for non-entities. For simplicity, all entities are single words. Matching is case-insensitive, so "John" in the sentence matches the entry "john".

Output: Return a string of space-separated tags, one per word.

Example

Input
2
John PERSON
Google ORG
John works at Google
Output
B-PERSON O O B-ORG
Reasoning
  • Inputs: entities maps each known word (stored in lowercase) to its type: here "john" is a PERSON and "google" is an ORG. sentence is John works at Google, which has 4 words separated by spaces, so the output will have exactly 4 tags.
  • Matching is case-insensitive: "John" with a capital J counts as the known word "john", and "Google" counts as "google". Capitalisation in the sentence never decides whether a word is an entity.
  • Tagging word by word: "John" is a known PERSON, so it becomes B-PERSON. "works" and "at" are not known entities, so each gets O. "Google" is a known ORG, so it becomes B-ORG.
  • Output: the 4 tags joined by single spaces, B-PERSON O O B-ORG, in the same order as the words.
  • Notice: every entity is one word, so only the B- prefix ever appears (no I- tags). With an empty entities mapping, every word would simply be O.

Constraints

  • All entities are single words
  • Case-insensitive matching
  • Tags: B-TYPE or O
  • Output tags space-separated

Learn this in

More from NLP 2: Applications

solution.py

Test results

Sign in to run your code against the test cases.