python3:智能搜索文本文件

2024-04-20 00:47:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大的文本文件(data),其布局如下:a|b|c|d|e|f(尽管b中可以有管道)。我用以下代码搜索文本文件:

results = 0
Data_List = []
searchphrase = input("Search: ")
with open('data', 'r', encoding="utf8") as inF:
    for line in inF:
        if searchphrase in line:
            a, *b, c, d, e, f = line.strip().split('|')
            b = '|'.join(b)
            results += 1
            print("\n\n", results, "\n", "A: " + a + "\n", "B: " + b + "\n", "C: " + c + "\n", "D: " + d + "\n", "E: " + e + "\n", "F: " + f + "\n\n")
            Data_List.append(f)

b是一段文本,其中包含用户在上面代码中真正搜索的标题(例如:The Lion King)。但是,搜索非常具体,只返回确切的结果(换句话说,如果我搜索The Lion King,那么{}将不会返回)。我怎样才能使搜索变得不那么具体,更通用(想想谷歌搜索)?在


Tags: the代码indata管道line布局results
1条回答
网友
1楼 · 发布于 2024-04-20 00:47:55

你可以看看Whoosh。在

Whoosh is a library of classes and functions for indexing text and then searching the index. It allows you to develop custom search engines for your content. For example, if you were creating blogging software, you could use Whoosh to add a search function to allow users to search blog entries.

它可以处理:

更多。。。Whoosh完全兼容Python和python3。在

不幸的是,模糊搜索与NLP相关,后者是CS中最复杂的主题之一,因此它不像使用一些神奇的正则表达式技巧那么简单。在

NLP很难,句号。这就是为什么Google使用Pigeon Cluster来对结果进行排名,而不是使用计算机算法(LoL)。在

相关问题 更多 >