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
.
Este ejercicio forma parte del curso
Building Chatbots in Python
Instrucciones del ejercicio
- Create a dictionary called
ents
to hold the entities by callingdict.fromkeys()
withinclude_entities
as the sole argument. - Create a
spacy
document calleddoc
by passing themessage
to thenlp
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 theents
dictionary.
Ejercicio interactivo práctico
Prueba este ejercicio completando el código de muestra.
# 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'))