类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是槌子中的 _io.BufferedReader

问题描述

我遵循了这里的槌 https://www.youtube.com/watch?v=TgXLq1XIdA0&t=823s 教程。 但是,运行python脚本后出现此错误

Traceback (most recent call last):
  File "tm.py",line 38,in <module> 
    lda_model = gensim.models.wrappers.ldamallet.Ldamallet(
  File "C:\Users\mmb\anaconda3\lib\site-packages\gensim\models\wrappers\ldamallet.py",line 126,in __init__
    self.train(corpus)
  File "C:\Users\mmb\anaconda3\lib\site-packages\gensim\models\wrappers\ldamallet.py",line 279,in train
    self.word_topics = self.load_word_topics()
  File "C:\Users\mmb\anaconda3\lib\site-packages\gensim\models\wrappers\ldamallet.py",line 337,in load_word_topics
    with utils.smart_open(self.fstate()) as fin:
  File "C:\Users\mmb\anaconda3\lib\site-packages\smart_open\smart_open_lib.py",line 138,in smart_open
    return file_smart_open(parsed_uri.uri_path,mode)
  File "C:\Users\mmb\anaconda3\lib\site-packages\smart_open\smart_open_lib.py",line 642,in file_smart_open
    return compression_wrapper(open(fname,mode),fname,line 630,in compression_wrapper
    return make_closing(GzipFile)(file_obj,mode)
  File "C:\Users\mmb\anaconda3\lib\gzip.py",line 173,in __init__
    fileobj = self.myfileobj = builtins.open(filename,mode or 'rb')
TypeError: expected str,bytes or os.pathLike object,not _io.BufferedReader

错误发生在 lda_mallet 模型的创建过程中。

mallet_path = os.path.join('C:\mallet','bin','mallet')

# create the mallet modeling object
lda_model = gensim.models.wrappers.ldamallet.Ldamallet(
    mallet_path,corpus=processed_corpus,id2word=corpus_dictionary,num_topics=number_of_topics,optimize_interval=10,prefix='fed_'
)

解决方法

我修复了自己的错误。 我在 anaconda 中创建了一个新环境并安装了 gensim 和其他所需的库。 它有效!