【问题标题】:How to use implemented labels on spaCy for each word?如何在 spaCy 上为每个单词使用已实现的标签?
【发布时间】:2020-11-26 06:23:11
【问题描述】:

我想做的事

我想提取在 spaCy、自然语言 OSS 上被赋予特定标签的单词。

specific labels on spaCy

在以下情况下,我希望打印单词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


    【解决方案1】:

    你可以这样做:

    import spacy
    nlp = spacy.load("en_core_web_sm")
    
    words = ['America', 'American', 'Christmas', 'English']
    words = nlp('. '.join(words) + '.')
    for w in words.ents:
        if w.label_=="LANGUAGE":
            print(w.text) #English
    

    请记住,spacy 通过查看整个上下文和语法来查找文本中的命名实体。例如,一个命名实体可以是“The United States of America”,即 4 个单词。如果您希望逐字查看,则需要对该“文本”赋予正确的语法意义。这就是为什么我通过在每个单词后使用句点来分隔文本(您的单词列表)。

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多