3
def makecounter():
     return collections.defaultdict(int)

class RankedIndex(object):
  def __init__(self):
    self._inverted_index = collections.defaultdict(list)
    self._documents = []
    self._inverted_index = collections.defaultdict(makecounter)


def index_dir(self, base_path):
    num_files_indexed = 0
    allfiles = os.listdir(base_path)
    self._documents = os.listdir(base_path)
    num_files_indexed = len(allfiles)
    docnumber = 0
    self._inverted_index = collections.defaultdict(list)

    docnumlist = []
    for file in allfiles: 
            self.documents = [base_path+file] #list of all text files
            f = open(base_path+file, 'r')
            lines = f.read()

            tokens = self.tokenize(lines)
            docnumber = docnumber + 1
            for term in tokens:  
                if term not in sorted(self._inverted_index.keys()):
                    self._inverted_index[term] = [docnumber]
                    self._inverted_index[term][docnumber] +=1                                           
                else:
                    if docnumber not in self._inverted_index.get(term):
                        docnumlist = self._inverted_index.get(term)
                        docnumlist = docnumlist.append(docnumber)
            f.close()
    print '\n \n'
    print 'Dictionary contents: \n'
    for term in sorted(self._inverted_index):
        print term, '->', self._inverted_index.get(term)
    return num_files_indexed
    return 0

このコードを実行すると、インデックス エラーが発生します: リスト インデックスが範囲外です。

上記のコードは、「用語」をキーとして格納する辞書インデックスと、その用語が出現するドキュメント番号をリストとして生成します。例: 「cat」という用語がドキュメント 1.txt、5.txt、および 7.txt にある場合、辞書には次のようになります: cat <- [1,5,7]

ここで、用語の頻度を追加するように変更する必要があるため、cat という単語が文書 1 で 2 回、文書 5 で 3 回、文書 7 で 1 回出現する場合: 期待される結果: term <-[[docnumber, term freq], [docnumber, term freq]] <--辞書内のリストのリスト!!! 猫 <- [[1,2]、[5,3]、[7,1]]

コードをいじってみましたが、何も機能しません。上記を実現するためにこのデータ構造を変更する手がかりがありません。

前もって感謝します。

4

3 に答える 3

6

まず、ファクトリを使用します。皮切りに:

def makecounter():
    return collections.defaultdict(int)

そして後で使用する

self._inverted_index = collections.defaultdict(makecounter)

そしてfor term in tokens:ループとして、

        for term in tokens:  
                self._inverted_index[term][docnumber] +=1

これは、self._inverted_index[term]次のようなdictをそれぞれに残します

{1:2,5:3,7:1}

あなたの例の場合。代わりに各self._inverted_index[term]リストのリストが必要なため、ループの終了直後に次を追加します。

self._inverted_index = dict((t,[d,v[d] for d in sorted(v)])
                            for t in self._inverted_index)

いったん作成すると (この方法またはその他の方法 -- 簡単な作成方法を示しているだけです!)、このデータ構造は実際には使いにくくなります。 dict は構築するだけでなく、はるかに便利で使いやすく、簡単です)、しかし、ねえ、人の肉 &c;-)。

于 2010-10-05T03:14:44.930 に答える
1

これは使用できる一般的なアルゴリズムですが、コードの一部をそれに適応させる必要があります。各ファイルの単語数の辞書を含む dict を生成します。

filedicts = {}
for file in allfiles:
  filedicts[file] = {}

  for term in terms:
    filedict.setdefault(term, 0)
    filedict[term] += 1
于 2010-10-05T03:09:32.390 に答える
0

おそらく、(docname、frequency)の単純なクラスを作成できます。

次に、辞書にこの新しいデータ型のリストを含めることができます。リストのリストを作成することもできますが、別のデータ型の方がきれいです。

于 2010-10-05T03:06:17.127 に答える