Python主题建模中Mallet出现错误

4

你好,我正在使用gensim进行主题建模,使用的是Mallet,并执行以下代码。我将Mallet解压到了C驱动器,并设置了环境变量MALLET_HOME。我执行的代码如下:

mallet_path = r'c:/mallet-2.0.8/bin/mallet'
ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, 
num_topics=20, id2word=id2word)

这会给我一个类似于这样的错误。
CalledProcessError                        Traceback (most recent call last)
<ipython-input-58-6e0dbb876ee6> in <module>()
----> 1 ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, 
corpus=corpus, 
   num_topics=20, id2word=id2word)

~\AppData\Local\Continuum\anaconda3\lib\site- 
packages\gensim\models\wrappers\ldamallet.py in __init__(self, mallet_path, 
corpus, num_topics, alpha, id2word, workers, prefix, optimize_interval, 
iterations, topic_threshold)
124         self.iterations = iterations
125         if corpus is not None:
--> 126             self.train(corpus)
127 
128     def finferencer(self):

~\AppData\Local\Continuum\anaconda3\lib\site- 
packages\gensim\models\wrappers\ldamallet.py in train(self, corpus)
265 
266         """
--> 267         self.convert_input(corpus, infer=False)
268         cmd = self.mallet_path + ' train-topics --input %s --num-topics 
%s  --alpha %s --optimize-interval %s '\
269             '--num-threads %s --output-state %s --output-doc-topics %s - 
-output-topic-keys %s '\

~\AppData\Local\Continuum\anaconda3\lib\site- 
packages\gensim\models\wrappers\ldamallet.py in convert_input(self, corpus, 
infer, serialize_corpus)
254             cmd = cmd % (self.fcorpustxt(), self.fcorpusmallet())
255         logger.info("converting temporary corpus to MALLET format with 
%s", cmd)
--> 256         check_output(args=cmd, shell=True)
257 
258     def train(self, corpus):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\utils.py in 
check_output(stdout, *popenargs, **kwargs)
1804             error = subprocess.CalledProcessError(retcode, cmd)
1805             error.output = output
-> 1806             raise error
1807         return output
1808     except KeyboardInterrupt:`

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

求救!!!

2个回答

0

我之前也遇到了这个错误,但现在它已经可以正常工作了。我不确定我具体做了什么来让它开始工作,但我会详细说明我所做的一切。

首先,我按照这个链接中的所有步骤进行操作,其中包括三个步骤的答案(如下所示): Error when implementing gensim.LdaMallet

  1. 确保已设置MALLET_HOME

  2. 在Python中设置mallet_path时,请转义斜杠

    mallet_path = 'c:\\mallet-2.0.8\\bin\\mallet'
    LDA_model = gensim.models.LdaMallet(mallet_path, ...
    
  3. 另外,修改Python\Lib\site-packages\gensim\models\ldamallet.py中的第142行可能会很有用:将--token-regex '\S+'更改为--token-regex \"\S+\"

但我仍然收到错误信息。接下来,我的电脑是双启动的,所以我启动了Lubuntu,安装了Java、Python3、Gensim,并将Mallet文件夹复制到了Lubuntu分区。我在Lubuntu终端中使用相同的代码运行了一个测试Python文件,但使用了新的Mallet目录/home/Desktop/mallet-2.0.8/bin/mallet。它有效地工作了。然后我重新启动回到Windows系统,突然间它也可以工作了。


0

请检查您是否正确安装了Java,并设置了JAVA_HOME系统变量,以及是否将%JAVA_HOME%\bin添加到全局PATH系统变量中。

完成上述步骤后,请重新启动计算机以使更改生效。

这对我很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接