gpt4 book ai didi

python - 从文本字符串中查找关键字列表并查找不完全匹配

转载 作者:行者123 更新时间:2023-11-28 16:50:40 25 4
gpt4 key购买 nike

我有一个要在文本字符串中查找的关键字列表。完全匹配工作正常,但是否有人知道可以帮助进行近似匹配的库,例如,如果我提供的单词列表是[“你好”,“再见”]我希望它能观察文本字符串是否有 hlelo 到一定程度的“接近度”有什么建议吗?

最佳答案

这是我会做的。首先,定义一个字符串来搜索并删除无关的字符:

>>> 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 - 从文本字符串中查找关键字列表并查找不完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com