我有这个代码可以在一个大文本文件中搜索文本:
y = input("Apellido/s:").upper()
for line in fread:
if y in line:
print(line)
如果未找到任何内容,我如何实现搜索类似文本/自动更正的方法。不像谷歌那样广泛,只是搜索一个单词中可能还有 1 个字母或重音的文本。我无法想象算法可以自己完成。
我现在必须添加一个 if 但我要求逻辑
答案 0 :(得分:0)
您可以使用 find_near_matches 中的 fuzzysearch
from fuzzysearch import find_near_matches
y = input("Apellido/s:").upper()
for line in fread:
if find_near_matches(y, line, max_l_dist=2):
print(line)
find_near_matches
如果找到匹配项,则返回匹配项列表,如果未找到匹配项,则返回一个评估为 false 的空数组。
max_l_dist
选项表示 the total number of substitutions, insertions and deletions(又名 the Levenshtein distance)