26 lines
497 B
Python
26 lines
497 B
Python
# 倒排索引
|
|
index = {}
|
|
# 文档存储
|
|
documents = {}
|
|
|
|
|
|
def add_document(doc_id, content):
|
|
documents[doc_id] = content
|
|
for word in content.lower().split():
|
|
if word not in index:
|
|
index[word] = []
|
|
if doc_id not in index[word]:
|
|
index[word].append(doc_id)
|
|
|
|
|
|
def serch(keyword):
|
|
return index.get(keyword.lower(), [])
|
|
|
|
|
|
add_document(1, "hello world")
|
|
add_document(2, "world of python")
|
|
add_document(3, "hello python")
|
|
|
|
print(index)
|
|
print(documents)
|