我正在使用 gensim 包在 python 中进行主题建模。
我正在尝试使用 gensim 训练主题模型。下面是 train.py 模块:
class Corpus(object):
def __init__(self, cursor, reviews_dictionary, corpus_path):
self.cursor = cursor
self.reviews_dictionary = reviews_dictionary
self.corpus_path = corpus_path
def __iter__(self):
self.cursor.rewind()
for review in self.cursor:
yield self.reviews_dictionary.doc2bow(review["words"])
def serialize(self):
BleiCorpus.serialize(self.corpus_path, self, id2word=self.reviews_dictionary)
return self
class Dictionary(object):
def __init__(self, cursor, dictionary_path):
self.cursor = cursor
self.dictionary_path = dictionary_path
def build(self):
self.cursor.rewind()
dictionary = corpora.Dictionary(review["words"] for review in self.cursor)
dictionary.filter_extremes(keep_n=10000)
dictionary.compactify()
corpora.Dictionary.save(dictionary, self.dictionary_path)
return dictionary
class Train:
def __init__(self):
pass
@staticmethod
def run(lda_model_path, corpus_path, num_topics, id2word):
corpus = corpora.BleiCorpus(corpus_path)
lda = gensim.models.LdaModel(corpus, num_topics=num_topics, id2word=id2word)
lda.save(lda_model_path)
return lda
运行此模块时出现以下错误:
> Traceback (most recent call last):
File "train.py", line 74, in <module>
main()
File "train.py", line 68, in main
dictionary = Dictionary(reviews_cursor, dictionary_path).build()
File "train.py", line 38, in build
corpora.Dictionary.save(dictionary, self.dictionary_path)
File "/usr/local/lib/python2.7/dist-packages/gensim/utils.py", line 288, in save
pickle(self, fname)
File "/usr/local/lib/python2.7/dist-packages/gensim/utils.py", line 666, in pickle
with smart_open(fname, 'wb') as fout: # 'b' for binary, needed on Windows
File "/usr/local/lib/python2.7/dist-packages/gensim/utils.py", line 661, in smart_open
return open(fname, mode)
IOError: [Errno 2] No such file or directory: 'models/dictionary.dict'
谁能帮我解决这个问题?