Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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_Information Retrieval_Inverted Index - Fatal编程技术网

Python 在倒排索引中搜索普通查询

Python 在倒排索引中搜索普通查询,python,information-retrieval,inverted-index,Python,Information Retrieval,Inverted Index,我有一个嵌套python字典形式的完整倒排索引。其结构是: {word:{doc\u name:[位置列表]}} 例如,将字典称为索引,则对于单词“spam”,条目如下所示: {垃圾邮件:{doc1.txt:[102300399],doc5.txt:[200587]} 因此,包含任何单词的文档都可以通过索引[word].keys()给出,并通过len(索引[word][document])给出该文档中的频率 现在我的问题是,如何在这个索引中实现普通的查询搜索。i、 e.给定一个包含4个单词的查询

我有一个嵌套python字典形式的完整倒排索引。其结构是:

{word:{doc\u name:[位置列表]}}

例如,将字典称为索引,则对于单词“spam”,条目如下所示:

{垃圾邮件:{doc1.txt:[102300399],doc5.txt:[200587]}

因此,包含任何单词的文档都可以通过索引[word].keys()给出,并通过len(索引[word][document])给出该文档中的频率

现在我的问题是,如何在这个索引中实现普通的查询搜索。i、 e.给定一个包含4个单词的查询,查找包含所有四个匹配项的文档(按总出现频率排序),然后查找包含3个匹配项的文档,依此类推

**

使用S.Lott的答案添加了此代码。 这是我写的代码。它的工作完全符合我的要求(只需要一些输出格式),但我知道它可以改进

**

请评论。。。。 Thanx.

这是一个开始:

doc_has_word = [ (index[word].keys(),word) for word in wordlist ]
这将建立(单词、文档)对的列表。因为每个文档都会出现很多次,所以你很难用它来编一本字典

但是


可能会有帮助

以下是查找类似文档的解决方案(最难的部分):

import itertools

index = {...}

def query(*args):
    result = []

    doc_count = [(doc, len(index[word][doc])) for word in args for doc in index[word]]
    doc_group = itertools.groupby(doc_count, key=lambda doc: doc[0])

    for doc, group in doc_group:
        result.append((doc, sum([elem[1] for elem in group])))

    return sorted(result, key=lambda x:x[1])[::-1]
wordMatches
获取一个列表,其中每个元素都是一个文档字典,用于匹配其中一个单词

similarDocs
是一组文档,其中包含要查询的所有单词。这是通过从
wordMatches
列表中的每个词典中只提取文档名称,将这些文档名称列表表示为集合,然后将集合相交以查找公共文档名称来实现的

一旦找到了相似的文档,就应该能够使用defaultdict(如S.Lott的回答所示)将每个单词和每个文档的所有匹配列表附加在一起

相关链接:

  • 。defaultdict(列表)的工作方式基本相同

“正常查询搜索”?你可能想从中去掉“正常”。这只是一个疑问。到目前为止,您编写了哪些代码?你写的代码有什么问题。这不是为我的网站做我的家庭作业。请发布你的代码和关于代码的详细问题。@S.洛特:如果这个问题看起来像是“为我做家庭作业”之类的问题,那就很抱歉了。我已经意识到我的错误,并添加了一些问题所在的细节。@S.洛特:使用您的答案添加了代码。我已经为问题中给出的代码设置了。将S.Lott的答案标记为已接受。doc_单词[d]。附加(w)。。。。。。d是一个不可损坏的列表。Thanx代表代码。这帮了大忙。我已经编辑了问题并发布了我用你的代码编写的代码。有关最终代码,请参阅问题。那里的代码是使用此代码编写的。将此标记为acceptedI已经能够获得查询中所有单词的匹配文档,但忘记了在post中提到这一点。
from collections import defaultdict
doc_words = defaultdict(list)
for d, w in doc_has_word:
    doc_words[tuple(d.items())].append(w)
import itertools

index = {...}

def query(*args):
    result = []

    doc_count = [(doc, len(index[word][doc])) for word in args for doc in index[word]]
    doc_group = itertools.groupby(doc_count, key=lambda doc: doc[0])

    for doc, group in doc_group:
        result.append((doc, sum([elem[1] for elem in group])))

    return sorted(result, key=lambda x:x[1])[::-1]
wordList = ['spam','eggs','toast'] # our list of words to query for
wordMatches = [index.get(word, {}) for word in wordList]
similarDocs = reduce(set.intersection, [set(docMatch.keys()) for docMatch in wordMatches])