0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| from flashtext import KeywordProcessor
text = 'I love my city and my country.'
kp = KeywordProcessor()
kp.add_keyword('my city') kp.add_keyword('my country')
print(kp.extract_keywords(text)) print(kp.extract_keywords(text, span_info=True))
kp.add_keyword('my city', 'beijing') kp.add_keyword('my country', 'china')
new_sentence = kp.replace_keywords(text) print(new_sentence)
kp.remove_keyword('my city')
new_sentence2 = kp.replace_keywords(text) print(new_sentence2)
|