我在gethon中使用gensim包进行主题建模。
我正在尝试使用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'
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
当&#34;没有这样的文件或目录&#34;在保存操作期间发生,通常表示您指定为输出文件的容器的目录路径不存在。在这种情况下,你清楚地给它self.dictionary_path = "models/dictionary.dict"
这是一个相对路径。保存到此路径的错误可能意味着文件无法保存在&#34;模型&#34;因为目录&#34;模型&#34;相对于当前工作目录不存在。
要查找当前工作目录,您可以使用os.getcwd
。要测试目录是否存在,可以使用os.path.isdir
。要创建目录,您可以使用os.mkdir
。