Python の gensim ライブラリを使用して、潜在的なセマンティック インデックス作成を行っています。ウェブサイトのチュートリアルに従ったところ、かなりうまく機能しました。今、私はそれを少し修正しようとしています。ドキュメントが追加されるたびに lsi モデルを実行したい。
これが私のコードです:
stoplist = set('for a of the and to in'.split())
num_factors=3
corpus = []
for i in range(len(urls)):
print "Importing", urls[i]
doc = getwords(urls[i])
cleandoc = [word for word in doc.lower().split() if word not in stoplist]
if i == 0:
dictionary = corpora.Dictionary([cleandoc])
else:
dictionary.addDocuments([cleandoc])
newVec = dictionary.doc2bow(cleandoc)
corpus.append(newVec)
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
lsi = models.LsiModel(corpus_tfidf, numTopics=num_factors, id2word=dictionary)
corpus_lsi = lsi[corpus_tfidf]
geturls は、Web サイトのコンテンツを文字列として返す関数です。繰り返しますが、tfidf と lsi を実行する前にすべてのドキュメントを処理するまで待っていれば機能しますが、それは私が望んでいることではありません。私は各反復でそれをやりたいです。残念ながら、次のエラーが表示されます。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "streamlsa.py", line 51, in <module>
lsi = models.LsiModel(corpus_tfidf, numTopics=num_factors, id2word=dictionary)
File "/Library/Python/2.6/site-packages/gensim-0.7.8-py2.6.egg/gensim/models/lsimodel.py", line 303, in __init__
self.addDocuments(corpus)
File "/Library/Python/2.6/site-packages/gensim-0.7.8-py2.6.egg/gensim/models/lsimodel.py", line 365, in addDocuments
self.printTopics(5) # TODO see if printDebug works and remove one of these..
File "/Library/Python/2.6/site-packages/gensim-0.7.8-py2.6.egg/gensim/models/lsimodel.py", line 441, in printTopics
self.printTopic(i, topN = numWords)))
File "/Library/Python/2.6/site-packages/gensim-0.7.8-py2.6.egg/gensim/models/lsimodel.py", line 433, in printTopic
return ' + '.join(['%.3f*"%s"' % (1.0 * c[val] / norm, self.id2word[val]) for val in most])
File "/Library/Python/2.6/site-packages/gensim-0.7.8-py2.6.egg/gensim/corpora/dictionary.py", line 52, in __getitem__
return self.id2token[tokenid] # will throw for non-existent ids
KeyError: 1248
通常、エラーは 2 番目のドキュメントで表示されます。私はそれが私に言っていることを理解していると思います(辞書のインデックスは悪いです)が、なぜなのかわかりません。私はさまざまなことを試しましたが、何もうまくいかないようです。誰が何が起こっているのか知っていますか?
ありがとう!