问题描述
我想对带有单词的字符串进行模糊匹配。
目标字符串可能像这样。
“你好,我今天要去看电影。”
我要搜索的单词在哪里。
“ flim toda”。
希望这将返回“今天的电影”作为搜索结果。
import difflib
def matches(large_string,query_string,threshold):
words = large_string.split()
matched_words = []
for word in words:
s = difflib.SequenceMatcher(None,word,query_string)
match = ''.join(word[i:i+n] for i,j,n in s.get_matching_blocks() if n)
if len(match) / float(len(query_string)) >= threshold:
matched_words.append(match)
return matched_words
large_string = "Hello,I am going to watch a film today"
query_string = "film"
print(list(matches(large_string,0.8)))
这仅适用于一个单词,只有很少的声音才会返回。
有什么方法可以对单词进行模糊匹配吗?
解决方法
您正在考虑的功能称为“查询建议”,它确实依赖于拼写检查,但它依赖于从搜索引擎查询日志中构建的markov链。
话虽如此,您使用的方法类似于此答案中描述的方法:https://stackoverflow.com/a/58166648/140837