我是python的新手。给定文档标记列表,我需要创建一个倒排索引函数。该索引将每个唯一单词映射到文档 ID 列表,按升序排序。
我的代码:
def create_index(tokens):
inverted_index = {}
wordCount = {}
for k, v in tokens.items():
for word in v.lower().split():
wordCount[word] = wordCount.get(word,0)+1
if inverted_index.get(word,False):
if k not in inverted_index[word]:
inverted_index[word].append(k)
else:
inverted_index[word] = [k]
return inverted_index, wordCount
注意:当输入参数的形式为{1:"Madam I am Adam",2: "I have never been afraid of him"}
我为上面的例子得到的输出:
{'madam': [1], 'afraid': [2], 'i': [1, 2], 'of': [2], 'never': [2], 'am': [1], 'been': [2], 'adam': [1], 'have': [2], 'him': [2]}
根据我的代码 K,v 对应于列表的键和值
当我们使用参数调用 create_index 函数时所需的输出:
index = create_index([['a', 'b'], ['a', 'c']])
>>> sorted(index.keys())
['a', 'b', 'c']
>>> index['a']
[0, 1]
index['b']
[0]
index['c']
[1]