繁体   English   中英

spacy 仅删除组织和人名

[英]spacy remove only org and person names

我写了下面的 function,它从文本中删除了所有命名实体。 我如何修改它以仅删除组织和人名? 我不想从下面的$6中删除6 谢谢

import spacy
sp = spacy.load('en_core_web_sm')
def NER_removal(text):
    document = sp(text)
    
    text_no_namedentities = []
    
    ents = [e.text for e in document.ents]
    for item in document:
        if item.text in ents:
            pass
        else:
            text_no_namedentities.append(item.text)
    return (" ".join(text_no_namedentities))


NER_removal("John loves to play at Sofi stadium at 6.00 PM and he earns $6")
'loves to play at stadium at 6.00 PM and he earns $'

我认为item.ent_type_在这里会有用。

import spacy
sp = spacy.load('en_core_web_sm')
def NER_removal(text):
    document = sp(text)
    text_no_namedentities = []
    # define ent types not to remove
    ent_types_to_stay = ["MONEY"]
    ents = [e.text for e in document.ents]
    for item in document:
        # add condition to leave defined ent types
        if all((item.text in ents, item.ent_type_ not in ent_types_to_stay)):
            pass
        else:
            text_no_namedentities.append(item.text)
    return (" ".join(text_no_namedentities))

print(NER_removal("John loves to play at Sofi stadium at 6.00 PM and he earns $6"))
# loves to play at Sofi stadium at 6.00 PM and he earns $ 6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM