def boolean_search_and(self, text):
results = []
and_tokens = self.tokenize(text)
tokencount = len(and_tokens)
term1 = and_tokens[0]
print ' term 1:', term1
term2 = and_tokens[1]
print ' term 2:', term2
#for term in and_tokens:
if term1 in self._inverted_index.keys():
resultlist1 = self._inverted_index[term1]
print resultlist1
if term2 in self._inverted_index.keys():
resultlist2 = self._inverted_index[term2]
print resultlist2
#intersection of two sets casted into a list
results = list(set(resultlist1) & set(resultlist2))
print 'results:', results
return str(results)
このコードは、text = "Hello World"のように、tokens = ['hello'、'world']の2つのトークンに最適です。複数のトークンに一般化して、テキストを文にすることも、テキストファイル全体にすることもできます。
self._inverted_indexは、トークンをキーとして保存する辞書であり、値は、キー/トークンが発生するDocIDです。
hello- > [1,2,5,6]
world-> [1,3,5,7,8]
結果:
hello AND world-> [1,5]
次の結果を達成したい:(((hello AND computer)AND science)AND world)
私はこれを2つだけではなく複数の単語で機能させるように取り組んでいます。私は今朝Pythonで作業を始めたので、Pythonが提供しなければならない多くの機能に気づいていません。
何か案は?