Mue*_*nze 6 python search-engine python-3.x booleanquery boolean-search
我有一个倒排索引(作为字典),我想将布尔搜索查询作为输入来处理它并产生结果。
倒排索引是这样的:
{
Test : { FileName1: [213, 1889, 27564], FileName2: [133, 9992866, 27272781, 78676818], FileName3: [9211] },
Try : { FileName4 ...
.....
}
Run Code Online (Sandbox Code Playgroud)
现在,给定布尔搜索查询,我必须返回结果。
例子:
布尔搜索查询:test AND try
结果应该是所有带有单词test and try的文档。
布尔搜索查询:test OR try
结果应该是所有具有测试或尝试的文档。
布尔搜索查询:test AND NOT try
结果应该是所有经过测试但没有尝试的文档。
如何构建此搜索引擎来处理给定的布尔搜索查询?
提前致谢!
考虑到您有倒排索引,并且这是一个以test
和try
作为键的字典,您可以定义以下函数并使用它们:
def intersection(list1, list2):
return list(set(list1).intersection(list2))
def union(list1, list2):
return list(set(list1).union(list2))
def notin(list1, list2)
return [filter(lambda x: x not in list1, sublist) for sublist in list2]
intersection(inverted_index['people'].keys(), intersection(inverted_index['test'].keys(), inverted_index['try'].keys()))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1775 次 |
最近记录: |