【发布时间】:2020-11-26 06:23:11
【问题描述】:
我想做的事
我想提取在 spaCy、自然语言 OSS 上被赋予特定标签的单词。
在以下情况下,我希望打印单词English,因为选择了标签LANGUAGE。
English
问题
没有提取每个单词标签的示例代码。
如何解决以下错误?
TypeError: Argument 'string' has incorrect type (expected str, got spacy.tokens.token.Token)
当前代码
import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
words = ['America', 'American', 'Christmas', 'English']
words = nlp(words)
for w in words:
if w.label_=="LANGUAGE":
print(w) #English
我尝试了什么
我已经检查了每个标签和示例代码以进行可视化。
此外,非句子输入,` 可以在 the spaCy vizualizer on the web browser 上执行。
输出
American NORP speaks English LANGUAGE and celebrates Christmas DATE in America GPE .
此代码来自the sample code on the spaCy homepage。 代码
import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
text = "American speaks English and celebrates Christmas in America."
doc = nlp(text)
displacy.serve(doc, style="ent")
【问题讨论】:
标签: python python-3.x string nlp spacy