0

The First example works exactly as expected:

class dog:

    def __init__(self,color):
        self.color = color
    
fido = dog("black")
setattr(fido,"breed","dalmatian")

print(fido.breed) #prints dalmatian

However if I try to do the same for my actual use case (I would like to add an attribute to spacy tokens)

import spacy
nlp = spacy.load('en_core_web_sm')

text = 'Dwayne "The Rock" Johnson'
doc = nlp(text)

setattr(doc[0],"Purpose","First Name") # Fails here

print(doc[0].Purpose)

I get an error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-b7bd5c7b8a5c> in <module>
      5 doc = nlp(text)
      6 
----> 7 setattr(doc[0],"Purpose","First Name")

AttributeError: 'spacy.tokens.token.Token' object has no attribute 'Purpose'

What is the difference?

schajan
  • 59
  • 5

0 Answers0