1. Learn
  2. /
  3. Courses
  4. /
  5. Advanced NLP with spaCy

Exercise

Setting extension attributes (2)

Let's try setting some more complex attributes using getters and method extensions. The nlp object has already been created for you and the Doc, Token and Span classes are already imported.

Remember that if you run your code more than once, you might see an error message that the extension already exists. That's because DataCamp will re-run your code in the same session. To solve this, you can set force=True on set_extension, or reload to start a new Python session. None of this will affect the answer you submit.

Instructions 1/2

undefined XP
  • 1
    • Complete the has_number function .
    • Use Doc.set_extension to register 'has_number' (getter get_has_number) and print its value.
  • 2
    • Use Span.set_extension to register 'to_html' (method to_html).
    • Call it on doc[0:2] with the tag 'strong'.