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.
This exercise is part of the course
Building Chatbots in Python
Exercise instructions
- Create a dictionary called
entsto hold the entities by callingdict.fromkeys()withinclude_entitiesas the sole argument. - Create a
spacydocument calleddocby passing themessageto thenlpobject. - 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.textattribute to the corresponding key in theentsdictionary.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define included_entities
include_entities = ['DATE', 'ORG', 'PERSON']
# Define extract_entities()
def extract_entities(message):
# Create a dict to hold the entities
ents = ____
# Create a spacy document
doc = ____
for ent in ____:
if ____ in ____:
# Save interesting entities
ents[____] = ____
return ents
print(extract_entities('friends called Mary who have worked at Google since 2010'))
print(extract_entities('people who graduated from MIT in 1999'))