Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 Toptimizer这个词_Python_Search_Text - Fatal编程技术网

Python Toptimizer这个词

Python Toptimizer这个词,python,search,text,Python,Search,Text,上面的代码片段是我用来在包含查询中所有标记的海量文本数据中查找相关句子的核心算法。例如,对于一个查询happy apple,它会查找恰好包含一个或多个给定标记的所有句子,即happy和apple。我的方法很简单:找到公共相交集,看看它们是否匹配。然而,我没有得到足够的表现。如果有人看到过针对此类问题的优化,我将非常感谢您提供的任何方向或链接-提前感谢您的时间您可以做一些事情来提高顺序搜索的性能,但真正的提升将来自索引标记 差异 使用not local_q_set.differences_标记而不

上面的代码片段是我用来在包含查询中所有标记的海量文本数据中查找相关句子的核心算法。例如,对于一个查询happy apple,它会查找恰好包含一个或多个给定标记的所有句子,即happy和apple。我的方法很简单:找到公共相交集,看看它们是否匹配。然而,我没有得到足够的表现。如果有人看到过针对此类问题的优化,我将非常感谢您提供的任何方向或链接-提前感谢您的时间

您可以做一些事情来提高顺序搜索的性能,但真正的提升将来自索引标记

差异

使用not local_q_set.differences_标记而不是将交集与原始集合进行比较可能会更快一些

正则表达式过滤器

如果您的句子很长,使用正则表达式可能会提供一些速度改进,方法是在根据标记集进行检查之前,将潜在的标记从句子中分离出来:

for s_index, s in enumerate(sentences):
        s_tokens = s.split()
        if (local_q_set.intersection(set(s_tokens)) == local_q_set):
            q_results.append(s_index)
缓存句子词集

要改进对同一句子列表进行多个查询时的顺序搜索,可以构建与句子对应的词集缓存。这将消除分析句子的工作,同时通过它们找到匹配项

result = []
tokenSet = set(queryTokens)
for index, sentence in enumerate(sentences):
     if any( token not in sentence for token in queryTokens) \
     or tokenSet.difference(sentence.split()):
         continue
     result.append(index)
标记索引

如果要对同一个句子列表执行多个查询,那么在标记和句子索引之间创建映射将更有效。您可以使用字典执行此操作,然后通过与查询标记的句子索引相交,直接获得查询结果:

cachedWords = []

queryTokens = ["happy","apple"]

queryTokenSet = set(queryTokens)
if not cachedWords:
    cachedWords = [ set(sentence.split()) for sentence in sentences ]
result = [ index for index,words in enumerate(cachedWords) if not queryTokenSet.difference(words) ]
这将允许您使用集合运算符经济地实现复杂查询。例如:

tokenIndexes = dict()
for index,sentence in enumerate(sentences):
    for token in sentence.lower().split():
        tokenIndexes.setdefault(token,[]).append(index)

def tokenSet(token): return set(tokenIndexes.get(token,[]))

queryTokens = ["happy","apple"]

from functools import reduce
result = reduce(set.intersection , (tokenSet(token) for token in queryTokens) )
性能测试:

我做了一些性能测试,在80000个单词中的一句话中找到了两个标记:

import re

querySring = " happy & ( apple | orange | banana ) "
result = eval(re.sub("(\w+)",r"tokenSet('\1')", querySring)) 

# re.sub(...) transforms the query string into " tokenSet('happy') & ( tokenSet('apple') | tokenSet('orange') | tokenSet('banana') ) "

因此,如果您要对同一个句子执行多个查询,使用标记索引,那么一旦建立了标记索引字典,您的响应速度将提高1.4万倍。

您可以做一些事情来提高顺序搜索的性能,但真正的提升将来自标记索引

差异

使用not local_q_set.differences_标记而不是将交集与原始集合进行比较可能会更快一些

正则表达式过滤器

如果您的句子很长,使用正则表达式可能会提供一些速度改进,方法是在根据标记集进行检查之前,将潜在的标记从句子中分离出来:

for s_index, s in enumerate(sentences):
        s_tokens = s.split()
        if (local_q_set.intersection(set(s_tokens)) == local_q_set):
            q_results.append(s_index)
缓存句子词集

要改进对同一句子列表进行多个查询时的顺序搜索,可以构建与句子对应的词集缓存。这将消除分析句子的工作,同时通过它们找到匹配项

result = []
tokenSet = set(queryTokens)
for index, sentence in enumerate(sentences):
     if any( token not in sentence for token in queryTokens) \
     or tokenSet.difference(sentence.split()):
         continue
     result.append(index)
标记索引

如果要对同一个句子列表执行多个查询,那么在标记和句子索引之间创建映射将更有效。您可以使用字典执行此操作,然后通过与查询标记的句子索引相交,直接获得查询结果:

cachedWords = []

queryTokens = ["happy","apple"]

queryTokenSet = set(queryTokens)
if not cachedWords:
    cachedWords = [ set(sentence.split()) for sentence in sentences ]
result = [ index for index,words in enumerate(cachedWords) if not queryTokenSet.difference(words) ]
这将允许您使用集合运算符经济地实现复杂查询。例如:

tokenIndexes = dict()
for index,sentence in enumerate(sentences):
    for token in sentence.lower().split():
        tokenIndexes.setdefault(token,[]).append(index)

def tokenSet(token): return set(tokenIndexes.get(token,[]))

queryTokens = ["happy","apple"]

from functools import reduce
result = reduce(set.intersection , (tokenSet(token) for token in queryTokens) )
性能测试:

我做了一些性能测试,在80000个单词中的一句话中找到了两个标记:

import re

querySring = " happy & ( apple | orange | banana ) "
result = eval(re.sub("(\w+)",r"tokenSet('\1')", querySring)) 

# re.sub(...) transforms the query string into " tokenSet('happy') & ( tokenSet('apple') | tokenSet('orange') | tokenSet('banana') ) "

因此,如果您要对同一个句子执行多个查询,并使用标记索引,那么一旦构建了标记索引字典,您的响应速度将提高1.4万倍。

您的运行时基本正常。我真的不知道除了给这个问题增加并行化,你还能快多少。此外,缓存可能会有所帮助,特别是当您执行了大量重复查询时。我同意,谢谢您的评论。我忘了补充一点,同一组句子有多个查询。这会改变优化的逻辑吗?我正在研究可能的循环展开,您的运行时基本上处于打开状态。我真的不知道除了给这个问题增加并行化,你还能快多少。此外,缓存可能会有所帮助,特别是当您执行了大量重复查询时。我同意,谢谢您的评论。我忘了补充一点,同一组句子有多个查询。这会改变优化的逻辑吗?我正在研究可能的循环展开非常感谢你的精彩解释。这救了我的命。总有一天我希望有足够的经验像你一样为社区做出贡献。非常感谢你的精彩解释。这救了我的命。总有一天我希望有足够的经验像你一样为社区做出贡献。