Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python快速生成自动完成建议_Python_Algorithm_Autocomplete - Fatal编程技术网

使用Python快速生成自动完成建议

使用Python快速生成自动完成建议,python,algorithm,autocomplete,Python,Algorithm,Autocomplete,我有一套大约650万字的所有单词。如何使用Python快速生成以给定字符串开头的单词列表 很明显,我可以做类似的事情 def completions(word_start): ell = len(word_start) return [w for w in all_words if w[: ell] == word_start] 这是可行的,但需要一秒钟的时间。生成完整列表的更快方法是什么?您可以使用Python生成器() 在开始使用之前,您不必生成所有单词。假设您有一个按字典

我有一套大约650万字的
所有单词。如何使用Python快速生成以给定字符串开头的单词列表

很明显,我可以做类似的事情

def completions(word_start):
    ell = len(word_start)
    return [w for w in all_words if w[: ell] == word_start]

这是可行的,但需要一秒钟的时间。生成完整列表的更快方法是什么?

您可以使用Python生成器()


在开始使用之前,您不必生成所有单词。假设您有一个按字典顺序排序的列表,您可以获取最初的几个结果并开始使用它们。并“按需”获取更多结果。

一种快速方法是通过第一个
n
字符预先编制索引:

words_by_first3 = {}
for word in word_set:
    first3 = word[:3]
    if first3 not in words_by_first3:
        words_by_first3[first3] = set()
    words_by_first3[first3].add(word) 
然后使用它来查找完成项:

def completions(word):
    ell = len(word)
    return set(w for w in words_by_first3[word[:3]] if w[: ell] == word)

在我的例子中,这会很快给出结果,但会占用大量内存。

我想解决这类问题的最快和最节省空间的数据结构是使用。将单词集合解析到树中后,查找时间应该非常快。甚至似乎还有一个问题。

您可能想签出我的开源库:

它非常容易使用:

>>> from fast_autocomplete import AutoComplete
>>> words = {'book': {}, 'burrito': {}, 'pizza': {}, 'pasta':{}}
>>> autocomplete = AutoComplete(words=words)
>>> autocomplete.search(word='b', max_cost=3, size=3)
[['book'], ['burrito']]
>>> autocomplete.search(word='bu', max_cost=3, size=3)
[['burrito']]
>>> autocomplete.search(word='barrito', max_cost=3, size=3)  # mis-spelling
[['burrito']]

如果数据集相对较小,则蛮力线性搜索不会太糟糕。但是,对于大型数据集(如本例中),您将很快遇到内存和速度限制

正如其他一些答案所提到的,用于此目的的最佳数据结构是Trie——它将允许您高效地进行前缀搜索


然而,在纯Python中实现内存高效的trie是很困难的(特别是如果您想支持更新的话)。如果您不介意使用通过Python客户端访问的外部进程,可以使用Typesense:

这是web服务后端的一部分。我想尽快给出完整的结果。内存问题不是绝对的交易破坏因素,但我更喜欢内存友好的解决方案。第一块代码可以通过
words\u by\u first3=defaultdict(set)简化;对于word\u集合中的单词:words\u by\u first3[word[:3]]。添加(word)
它真的能实现搜索词建议吗?我在API或文档中找不到这方面的任何提及。怎么做?我想你想从用户查询日志中索引搜索词,并使其作为自动完成建议进行搜索?或者您想从现有数据(如标题)生成合成搜索术语建议吗?我想从现有数据生成术语建议,这在elasticsearch中很容易做到