1

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が提供しなければならない多くの機能に気づいていません。

何か案は?

4

2 に答える 2

1

複数のトークンに一般化したい

def boolean_search_and_multi(self, text):
    and_tokens = self.tokenize(text)
    results = set(self._inverted_index[and_tokens[0]])
    for tok in and_tokens[1:]:
        results.intersection_update(self._inverted_index[tok])
    return list(results)
于 2010-09-13T04:20:54.837 に答える
0

ビルトインセットタイプで動作しますか?

$ python
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = set([1,2,5,6])
>>> world = set([1,3,5,7,8])
>>> hello & world
set([1, 5])
于 2010-09-13T04:19:51.673 に答える