파이썬에서 주제 모델링을 위해 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'
누구든지 문제를 파악하도록 도와 주시겠습니까?
저장 작업 중에 "해당 파일 또는 디렉토리 없음"이 발생하면 일반적으로 출력 파일의 컨테이너로 지정한 디렉토리 경로가 존재하지 않음을 의미합니다. 이 경우 self.dictionary_path = "models/dictionary.dict"
상대 경로를 명확하게 지정했습니다 . 이 경로에 저장하는 중 오류는 "models"디렉토리가 현재 작업 디렉토리에 상대적으로 존재하지 않기 때문에 "models"내에 파일을 저장할 수 없음을 의미합니다.
현재 작업 디렉토리를 찾으려면을 사용할 수 있습니다 os.getcwd
. 디렉토리가 존재하는지 테스트하려면을 사용할 수 있습니다 os.path.isdir
. 디렉토리를 만들려면 os.mkdir
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다