首页 > 解决方案 > Python Gensim槌

问题描述

我正在尝试使用 Python 上的 Gensim 的 Mallet 包装器将 LDA 应用于主题建模。我正在运行的代码如下:

MALLET_PATH = 'C:/mallet-2.0.8/bin/mallet'
lda_mallet = gensim.models.wrappers.LdaMallet(mallet_path=MALLET_PATH, corpus=bow_corpus, 
                                              num_topics=TOTAL_TOPICS, id2word=dictionary,
                                              iterations=500, workers=16)

Mallet 安装在 C 盘并在命令提示符 (C:\mallet-2.0.8\bin\mallet) 上运行。帮助命令也有效(import-dir --)。还安装了 Java。还为 Mallet 和 Java 设置了环境变量和路径。但输出显示以下错误。

CalledProcessError: Command 'mallet-2.0.8/bin/mallet import-file --preserve-case --keep-sequence --remove-stopwords --token-regex "\S+" --input C:\Users\imibh\AppData\Local\Temp\a8b7e6_corpus.txt --output C:\Users\imibh\AppData\Local\Temp\a8b7e6_corpus.mallet' returned non-zero exit status 1.

已经尝试了对过去此类堆栈溢出查询的所有响应,但没有任何改进。

非常感谢任何帮助。

马尼特

标签: python-3.xjupyter-notebookgensimmallet

解决方案


确保您安装了 Java Developers Kit ( JDK )。

安装 JDK 后,LDA Mallet 的以下代码就像魅力一样!

import os
from gensim.models.wrappers import LdaMallet

os.environ.update({'MALLET_HOME':r'C:/mallet/mallet-2.0.8/'})
mallet_path = r'C:/mallet/mallet-2.0.8/bin/mallet.bat'

lda_mallet = LdaMallet(
        mallet_path,
        corpus = corpus_bow,
        num_topics = n_topics,
        id2word = dct,
    )

这归功于另一个答案


推荐阅读