1. 程式人生 > >python 實現倒排索引

python 實現倒排索引

程式碼如下:

#encoding:utf-8

fin = open('1.txt', 'r')

'''
建立正向索引:
 “文件1”的ID > 單詞1:出現位置列表;單詞2:出現位置列表;…………
 “文件2”的ID > 此文件出現的關鍵詞列表。
'''
forward_index = {}
for line in fin:
    line = line.strip().split()
    forward_index[int(line[0])] = {}
    words = line[1].split(',')
    for i, index in enumerate(words):
        if
int(index) not in forward_index[int(line[0])].keys(): forward_index[int(line[0])][int(index)] = [i] else: forward_index[int(line[0])][int(index)].append(i) print 'forward_index:', forward_index ''' 建立倒排索引: “關鍵詞1”:“文件1”的ID,“文件2”的ID,………… “關鍵詞2”:帶有此關鍵詞的文件ID列表。 ''' inverted_index = {} for
doc_id, words in forward_index.items(): for word_id in words.keys(): if word_id not in inverted_index.keys(): inverted_index[word_id] = [doc_id] elif doc_id not in inverted_index[word_id]: inverted_index[word_id].append(doc_id) print 'inverted_index:', inverted_index

輸入(文件id:單詞id):
1 3,4
2 3,4,2,4
3 2

輸出:
forward_index: {1: {3: [0], 4: [1]}, 2: {2: [2], 3: [0], 4: [1, 3]}, 3: {2: [0]}}
inverted_index: {2: [2, 3], 3: [1, 2], 4: [1, 2]}