Session Ready
Exercise

Assigning roles using spaCy's parser

In this exercise you'll use spaCy's powerful syntax parser to assign roles to the entities in your users' messages. To do this, you'll define two functions, find_parent_item() and assign_colors(). In doing so, you'll use a parse tree to assign roles, similar to how Alan did in the video.

Recall that you can access the ancestors of a word using its .ancestors attribute.

Instructions
100 XP
  • Create a spacy document called doc by passing the message "let's see that jacket in red and some blue jeans" to the nlp object.
  • In the find_parent_item(word) function, iterate over the ancestors of each word until an entity_type() of "item" is found.
  • In the assign_colors(doc) function, iterate over the doc until an entity_type of "color" is found. Then, find the parent item of this word.
  • Pass in the spacy document to the assign_colors() function.