【发布时间】: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