【问题标题】:How can I add tolerance to an input search engine如何向输入搜索引擎添加容差
【发布时间】:2021-02-13 03:29:01
【问题描述】:

我有这段代码可以在大文本文件中搜索文本:

y = input("Apellido/s:").upper()
    for line in fread:

        if y in line:
            print(line)

如果找不到任何内容,我如何实现搜索类似文本/自动更正的方法。不像谷歌那样广泛,但只需搜索可能有 1 个字母或单词中的重音的文本。我无法想象算法可以自己完成。

我现在必须添加一个 if 但我要求逻辑

【问题讨论】:

标签: python python-3.x search logic


【解决方案1】:

您可以使用来自fuzzysearchfind_near_matches 来做到这一点

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多