簡體   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