简体   繁体   中英

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.

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.

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).

As a starting step, you can Look at 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

More docs: https://spacy.io/usage/linguistic-features#named-entities

Visualise here: https://explosion.ai/demos/displacy-ent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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