【问题标题】:Named entity recognition in SpacySpacy中的命名实体识别
【发布时间】:2018-01-11 05:48:10
【问题描述】:

我正在尝试为以下句子查找命名实体

import spacy.lang.en
parser = spacy.lang.en.English()
ParsedSentence = parser(u"Alphabet is a new startup in China")
for Entity in  ParsedSentence.ents:  
    print (Entity.label, Entity.label_, ' '.join(t.orth_ for t in Entity))

我希望得到结果“Alphabet”、“China”,但结果却是一个空集。我在这里做错了什么

【问题讨论】:

  • NER 基于训练输入数据。因此,对于您的示例,它可能无法从有限的上下文中知道“Alphabet”是named entity。尝试更多示例。

标签: python named-entity-recognition spacy


【解决方案1】:

根据 spacy documentation 的名称实体识别,这里是提取名称实体的方法

import spacy
nlp = spacy.load('en') # install 'en' model (python3 -m spacy download en)
doc = nlp("Alphabet is a new startup in China")
print('Name Entity: {0}'.format(doc.ents))

结果
Name Entity: (China,)

要使“Alphabet”成为“名词”,请在其后面加上“The”。

doc = nlp("The Alphabet is a new startup in China")
print('Name Entity: {0}'.format(doc.ents))

Name Entity: (Alphabet, China)

【讨论】:

  • 但是如果输入的句子是I love biscuits, chocolate and bicyles.,不应该识别PRODUCT实体吗(对于biscuitschocolatebicycles)? doc 表明 PRODUCT 用于食品、车辆等。但是,doc.ents 不识别任何实体。
【解决方案2】:

在 Spacy 版本 3 中,Hugging Face 中的变形金刚针对 Spacy 在之前版本中提供的操作进行了微调,但效果更好。

Transformers 目前(2020 年)是自然语言处理领域的最新技术,即通常我们有(one-hot-encode -> word2vec -> glove | fast text)然后是(循环神经网络、递归神经网络、门控循环单元、长短期记忆、双向长短期记忆等)和现在的 Transformers + Attention(BERT、RoBERTa、XLNet、XLM、CTRL、AlBERT、T5、Bart、GPT、GPT-2、GPT -3) - 这只是为了说明“为什么”你应该考虑变形金刚,我知道有很多我没有提到的东西,比如 Fuzz、知识图等等

安装依赖:

sudo apt install libncurses5
pip install spacy-transformers --pre -f https://download.pytorch.org/whl/torch_stable.html
pip install spacy-nightly # I'm using 3.0.0rc2

下载模型:

python -m spacy download en_core_web_trf # English Transformer pipeline, Roberta base

这是list 的可用型号。

然后像往常一样使用它:

import spacy


text = 'Type something here which can be related to something, e.g Stack Over Flow organization'

nlp = spacy.load('en_core_web_trf')

document = nlp(text)

print(document.ents)

参考资料:

了解Transformers and Attention

阅读有关不同Trasnformers architectures 的摘要。

了解 Spacy 完成的 Transformers fine-tune

【讨论】:

    猜你喜欢
    • 2021-05-06
    • 2023-03-24
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多