- 构建Trie字典:KeywordProcessor
- 新增关键词:add_keyword
- 关键词抽取:extract_keywords
- 关键词替换:replace_keywords
- 删除关键词:remove_keywords
示例:
from flashtext import KeywordProcessor
keyword_processor = KeywordProcessor(case_sensitive=False)
keyword_processor.add_keyword(one_kw,)
keywords_found = keyword_processor.extract_keywords(one_str,span_info=True)
extract_keywords返回的是匹配到的关键词,replace_keywords是直接返回一整个句子,相当于关键词定位+替换:
kw_list=['健康','美味']
keyword_processor=KeywordProcessor()
for kl in kw_list:
keyword_processor.add_keyword(kl)
keyword_processor.add_keyword('健康','建康')
// 查询
text="这个菜,真是健康又美味,很健康"
new_sentence=keyword_processor.replace_keywords(text)
print(new_sentence)
new_sentence=keyword_processor.extract_keywords(text)
print(new_sentence)
>>> 这个菜,真是建康又美味,很建康
>>> ['建康', '美味', '建康']
keyword_processor.remove_keyword('banana')
keyword_processor.remove_keywords_from_dict({"food": ["bread"]})
keyword_processor.remove_keywords_from_list(["basketball"])