1. Learn
  2. /
  3. Courses
  4. /
  5. Building Chatbots in Python

Exercise

Using spaCy's entity recognizer

In this exercise, you'll use spaCy's built-in entity recognizer to extract names, dates, and organizations from search queries. The spaCy library has been imported for you, and its English model has been loaded as nlp.

Your job is to define a function called extract_entities(), which takes in a single argument message and returns a dictionary with the included entity types as keys, and the extracted entities as values. The included entity types are contained in a list called include_entities.

Instructions

100 XP
  • Create a dictionary called ents to hold the entities by calling dict.fromkeys() with include_entities as the sole argument.
  • Create a spacy document called doc by passing the message to the nlp object.
  • Iterate over the entities in the document (doc.ents).
  • Check whether the entity's .label_ is one we are interested in. If so, assign the entity's .text attribute to the corresponding key in the ents dictionary.