![](/img/trans.png)
[英]how to search for a specific from and to keyword in a file and print the sentence in python
[英]How to remove a keyword from a given sentence
如何從短語中刪除關鍵字?
例如,
萊昂內爾·安德烈斯·梅西 (Lionel Andrés Messi) 是阿根廷職業足球運動員,擔任前鋒,同時擔任西班牙俱樂部巴塞羅那和阿根廷國家隊的隊長。
如何從這句話中刪除關鍵字(人名除外),例如“美國人”、“足球運動員”、“巴塞羅那”等等。
我意識到關鍵字必須是名詞,並且我遇到了一個名為 NLTK 的庫,也許這可以幫助我實現我想要實現的目標。
功能示例:
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
我認為您在這里需要的是 NER(命名實體識別)。
作為開始步驟,您可以查看 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:如果您需要特定的實體提取,您可能需要針對您的特定用例進行訓練
更多文檔: https : //spacy.io/usage/linguistic-features#named-entities
在此處可視化: https : //explosion.ai/demos/displacy-ent
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.