我有一个要在文本字符串中找到的关键字列表.完全匹配可以正常工作,但是没有人知道可以帮助近似匹配的库,例如,如果我提供的单词列表是
[“你好再见”]
我希望它可以观察文本字符串是否在一定程度上具有“紧密度”
有什么建议吗?
解决方法:
这就是我要做的.首先,定义一个字符串以搜索并删除多余的字符:
>>> tosearch = "This is a text string where I typed hlelo but I meant to type hello."
>>> import string
>>> exclude = set(string.punctuation)
>>> tosearch = ''.join(ch for ch in tosearch if ch not in exclude)
>>> tosearch
'This is a text string where I typed hlelo but I meant to type hello'
>>> words = set(tosearch.split(" "))
接下来,您可以使用difflib库查找与给定单词的紧密匹配项:
>>> import difflib
>>> difflib.get_close_matches('hello', words)
['hello', 'hlelo']
标签:python,django
来源: https://codeday.me/bug/20191202/2085961.html