Facing AttributeError: for 'tag_' using Spacy in Python

Facing AttributeError: for 'tag_' using Spacy in Python

我正在为 "POS Tagging" 使用 Spacy 并遇到以下错误。我有一个数据框,其中包含 "description" 列,我需要在其中提取每个单词的 POS

数据框:

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

代码:

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)

以上代码抛出错误:

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

但是,相同的代码对于 "Label" 属性以及如果我使用单个句子

都可以正常工作
doc = nlp('can you please help me to install wifi')
for i in doc:
    print (i.text, i.tag_)

这是因为像 entschunks 这样的东西是 Span,即标记的集合。因此,您需要遍历它们的各个标记以获取它们的属性,例如 tagtag_

>>> 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')]