【发布时间】:2021-01-04 12:10:48
【问题描述】:
我想对带有单词的字符串进行模糊匹配。
目标字符串可能是这样的。
“你好,我今天去看电影。”
我要搜索的词在哪里。
“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, query_string, 0.8)))
这仅适用于一个单词,并且在噪音很小时返回。
有什么办法可以对单词进行这种模糊匹配吗?
【问题讨论】:
标签: python nlp full-text-search string-matching fuzzy-search