Search code examples
pythonpython-3.xnlpspacypos-tagger

Facing AttributeError: for 'tag_' using Spacy in Python


I'm using Spacy for "POS Tagging" and getting below error. I have a dataframe, which has the column "description" in which I need to extract the POS for each word

Dataframe :

No.      Description
1        My net is not working
2        I will be out for dinner
3        Can I order food
4        Wifi issue

Code :

import pandas as pd
read_data = pd.read_csv('C:\\Users\\abc\\def\\pqr\\Data\\training_data.csv', encoding="utf-8")
entity = []
for parsed_doc in read_data['Description']:
    doc = nlp(parsed_doc)
    a = [(X.text, X.tag_) for X in doc.ents]
    entity.append(a)

The above code is throwing error:

Error : AttributeError: 'spacy.tokens.span.Span' object has no attribute 'tag_'

However, the same code is working fine for "Label" attribute and also if I use a single sentence

doc = nlp('can you please help me to install wifi')
for i in doc:
    print (i.text, i.tag_)

Solution

  • This is because things like ents or chunks are Spans i.e. collections of tokens. Hence you need to iterate over their individual tokens to get their attributes like tag or tag_

    >>> doc = nlp(u'Mr. Best flew to New York on Saturday morning.')
    >>> [(X.text, X.tag_) for X in doc.ents]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 1, in <listcomp>
    AttributeError: 'spacy.tokens.span.Span' object has no attribute 'tag_'
    >>> [(X.text, X.tag_) for Y in doc.ents for X in Y]
    [('Best', 'NNP'), ('New', 'NNP'), ('York', 'NNP'), ('Saturday', 'NNP'), ('morning', 'NN')]