【问题标题】:how to extract a PERSON named entity after certain word with spacy?如何在带有spacy的某个单词之后提取一个PERSON命名实体?
【发布时间】:2021-03-29 04:30:48
【问题描述】:

我有这个文本(代码中的text2),它有3个'by'字,我想用Spacy提取人名(全名,即使是3个字,有些种族使用长名字,在这个案例2)。代码如下,我的模式显示错误。我的意图:首先用 ORTH 修复“by”词,然后告诉程序接下来的任何内容都是名为 PERSON 的词性实体。如果有人帮忙,我会很高兴:

import spacy
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
text2 = 'All is done by Emily Muller, the leaf is burned by fire. we were not happy, so we cut     relations by saying bye bye'
def extract_person(nlp_doc):
     pattern = [{'ORTH': 'by'}, {'POS': 'NOUN'}}]
     # second possible pattern:
     #pattern = [{"TEXT": "by"}, {"NER": "PERSON"}]
     matcher.add('person_only', None, pattern)
     matches = matcher(nlp_doc)
     for match_id, start, end in matches:
         span = nlp_doc[start:end]
         return span.text
target_doc = nlp(text2)
extract_person(target_doc)

我认为这个问题可以换个方式问:how to use NER tags in pattern in Matcher in spacy?

【问题讨论】:

    标签: nlp extract spacy matcher named-entity-recognition


    【解决方案1】:

    如果您想使用全名,您应该在开头合并实体。您可以拨打:nlp.add_pipe("merge_entities", after="ner")

    然后在你的模式中而不是:

    pattern = [{"TEXT": "by"}, {"NER": "PERSON"}]
    

    用途:

    pattern = [{"TEXT": "by"}, {"ENT_TYPE": "PERSON"}]
    

    完整代码:

    nlp.add_pipe("merge_entities", after="ner")
    
    text2 = 'All is done by Emily Muller, the leaf is burned by fire. we were not happy, so we cut relations by saying bye bye'
    
    doc = nlp(text2)
    
    pattern = [{"TEXT": "by"}, {"ENT_TYPE": "PERSON"}]
    
    matcher = Matcher(nlp.vocab)
    
    matcher.add('person_only', [pattern])
    matches = matcher(doc)
    for match_id, start, end in matches:
        print(doc[start:end])
    
    

    【讨论】:

    • 我这样做了:data='TEXT PREVIOUS' doc = nlp(data) for ent in doc.ents: print(ent.text, ent.start_char, ent.end_char, ent.label_) nlp .add_pipe(nlp.create_pipe('merge_entities')) #nlp.add_pipe("merge_entities", after="ner") # after='ner' 显示错误模式 = [{"TEXT": "by"}, {" ENT_TYPE": "PERSON"}] matcher.add("by", None, pattern) matches = matcher(doc) print("Matches:", [doc[start:end].text for match_id, start, end in matches ]) #Result: #Emily Muller 15 27 PERSON #Matches: ['by Emily']
    • 您能否提供一些您正在使用的数据?您也可以尝试将名词短语与:nlp.add_pipe("merge_noun_chunks", last=True) 合并
    • 谢谢,我会努力的。这是数据。目标是在“by”之后提取 PERSON 名称(有时有 3-4 个名称,大多数时候是 2 个): text2 = 'All is done by Emily Muller,leaf is done by fire.我们不高兴,所以我们通过说再见来断绝关系
    • 合并名词块在这里可能无济于事,但合并实体应该注意这一点。
    • 我在此处给出的答案适用于您的示例文本
    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多