简体   繁体   English

如何从给定的句子中删除关键字

[英]How to remove a keyword from a given sentence

How can I remove a keyword from a phrase?如何从短语中删除关键字

For example,例如,

Lionel Andrés Messi is an Argentine professional footballer who plays as a forward and captains both Spanish club Barcelona and the Argentina national team.莱昂内尔·安德烈斯·梅西 (Lionel Andrés Messi) 是阿根廷职业足球运动员,担任前锋,同时担任西班牙俱乐部巴塞罗那和阿根廷国家队的队长。

How can I remove a keyword (except for the name of the person) from this sentence, such as "American", "footballer", "Barcelona" to name a few.如何从这句话中删除关键字(人名除外),例如“美国人”、“足球运动员”、“巴塞罗那”等等。

I have realised that the keyword must be a noun , and I came across a library called NLTK, and maybe that can help me what I want to achieve.我意识到关键字必须是名词,并且我遇到了一个名为 NLTK 的库,也许这可以帮助我实现我想要实现的目标。

Function example:功能示例:

remove(sentence, word_to_not_remove)
>>> sentence = 'Lionel Andrés Messi is an Argentine professional footballer who plays as a forward and captains both Spanish club Barcelona and the Argentina national team.'
>>> remove(sentence, 'Lionel Andrés Messi')
footballer

I think what you need here is NER(Named Entity Recognition).我认为您在这里需要的是 NER(命名实体识别)。

As a starting step, you can Look at Spacy [ https://explosion.ai/demos/displacy-ent ]作为开始步骤,您可以查看 Spacy [ https://explosion.ai/demos/displacy-ent ]

import spacy
text = "Lionel Andrés Messi is an Argentine professional footballer who plays as a forward and captains both Spanish club Barcelona and the Argentina national team."
nlp = spacy.load("en_core_web_sm")
doc = nlp(text)

for ent in doc.ents:
    print(ent.text, ent.start_char, ent.end_char, ent.label_)
Andrés Messi 7 19 PERSON
Argentine 26 35 NORP
Spanish 101 108 NORP
Barcelona 114 123 GPE
Argentina 132 141 GPE

PS: If you need a specific Entity Extraction, you may have to train it for your specific use case PS:如果您需要特定的实体提取,您可能需要针对您的特定用例进行训练

More docs: https://spacy.io/usage/linguistic-features#named-entities更多文档: https : //spacy.io/usage/linguistic-features#named-entities

Visualise here: https://explosion.ai/demos/displacy-ent在此处可视化: https : //explosion.ai/demos/displacy-ent

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

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