这与上一个问题类似,但不一样。相反,它建立在它之上(虽然从技术上讲它更容易)。
我将此链接到此处,以便您可以了解: -
Creating a simple searching program
仍在使用我的两个嵌套词典: -
wordFrequency = {'bit':{1:3,2:4,3:19,4:0},'red':{1:0,2:0,3:15,4:0},'dog':{1:3,2:0,3:4,4:5}}
search = {1:{'bit':1},2:{'red':1,'dog':1},3:{'bit':2,'red':3}}
第一个字典链接单词文件编号和它们在该文件中出现的次数。第二个包含将单词与当前搜索中出现的次数相关联的搜索。
我想使用二元独立模型,这里有一个很好的解释: -
http://en.wikipedia.org/wiki/Binary_Independence_Model
它比我以前的模型更简单,因为特定单词出现在搜索或文件中的次数是无关紧要的,只有存在或不存在很重要,即它是布尔值。因此它是相似的,但如果一个单词在搜索或文件中出现不止一次,它仍被视为1。
预期的输出又是字典: -
{1: [3, 2, 1, 4], 2: [3, 4, 1, 2], 3: [3, 2, 1, 4]}
这是前一个矢量空间模型程序的输出,因此输出可能不同。
到目前为止我的代码: -
from collections import Counter
def retrieve():
wordFrequency = {'bit':{1:3,2:4,3:19,4:0},'red':{1:0,2:0,3:15,4:0},'dog':{1:3,2:0,3:4,4:5}}
search = {1:{'bit':1},2:{'red':1,'dog':1},3:{'bit':2,'red':3}}
results = {}
for search_number, words in search.iteritems():
file_relevancy = Counter()
for word, num_appearances in words.iteritems():
num_appearances = 1
for file_id, appear_in_file in wordFrequency.get(word, {}).iteritems():
appear_in_file = 1
file_relevancy[file_id] += num_appearances * appear_in_file
results[search_number] = [file_id for (file_id, count) in file_relevancy.most_common()]
return results
print retrieve
但是,我只得到
的输出{1: [1, 2, 3, 4], 2: [1, 2, 3, 4], 3: [1, 2, 3, 4]}
这是不正确的,它只是按数字顺序返回文件?