繁体   English   中英

根据查询将字典值的出现次数计算为嵌套列表

[英]Count number of occurrences of dictionary values as nested list based on a query

我建立了这个倒排索引:

{
    'experiment': {'d1': [1, [0]], ..., 'd30': [2, [12, 40]], ..., 'd123': [3, [11, 45, 67]], ...}, 

    'studi': {'d1': [1, [1]], 'd2': [2, [0, 36]], ..., 'd207': [3, [19, 44, 59]], ...}

}

例如,术语experiment在文档 1 中出现一次,索引为 0,在文档 30 中出现两次,索引为 12 和 40,等等。我想知道如何根据字典计算字典中每个术语的出现次数看起来像这样的查询:

{
    'q1'  : ['similar', 'law', ..., 'speed', 'aircraft'],
    'q2'  : ['structur', 'aeroelast', ..., 'speed', 'aircraft'], 
    ...
    'q225': ['design', 'factor', ..., 'number', '5']
}

所需的 output 看起来像这样:

{
    'q1'  : ['d51', 'd874', ..., 'd717'], 
    'q2'  : ['d51', 'd1147', ..., 'd14'],
    ...,
    'q225': ['d1313', 'd996', ..., 'd193']
}

使用代表查询的键和代表查询出现的文档的值,列表将按总词频的降序排序

Map 查询文档向量

文档向量是带有项目(document, word_count)的字典。 这些向量可以通过将匹配文档键的字数与默认 word_count 为 0 相加来相加。

将索引转换为文档向量

full_index = {
    'experiment': {'d1': [1, [0]],  'd30': [2, [12, 40]],  'd123': [3, [11, 45, 67]] } ,
    'study': {'d1': [1, [1]], 'd2': [2, [0, 36]],  'd207': [3, [19, 44, 59]]}
}

def count_only(docs):
    return {d: occurences[0] for d, occurences in docs.items()}

doc_vector_index = {w: count_only(docs) for w, docs in full_index.items()}

MAP 查询词列表到文档向量列表

for q, words in queries.items():
    vectors = [doc_vector_index[word] for word in words if word in doc_vector_index.keys()]

对文档向量求和并排序

def doc_vector_add(ldoc, rdoc):
    res = ldoc.copy()
    for doc, count in rdoc.items():
        res[doc] = ldoc.get(doc,0) + count
    return res

for q, words in queries.items():
    vectors = [doc_vector_index[word] for word in words if word in doc_vector_index.keys()]
    total_vector = dict(sorted(functools.reduce(doc_vector_add, vectors, {}).items(), 
        key=lambda item: item[1], 
        reverse=True))
    output[q] = list(total_vector.keys())

使用 reduce functools.reduce(doc_vector_add, vectors, {})处理文档向量的总和。 这会生成文档向量,它是查询中每个单词的各个向量的总和。 sorted用于对向量的键进行排序。

限于前 N 个文件

max_doc_limit = 10
output[q] = list(total_vector.keys())[:max_doc_limit]

在分配给 output 之前,可以通过切片来限制文档。

ORDER BY COUNT DESC, DOC_ID ASC

sorted(...,key=lambda item: (item[1], -1*int(item[0][1:]),...)

我们可以通过更改传递给sorted的键 function 来更改 output 的排序顺序。 我们使用将元组中的第二个元素乘以 -1 的技巧来反转从降序到升序的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM