【问题标题】:Gensim mallet CalledProcessError: returned non-zero exit statusGensim mallet CalledProcessError:返回非零退出状态
【发布时间】:2019-08-12 19:08:20
【问题描述】:

我在尝试访问 jupyter 笔记本中的 gensims mallet 时遇到错误。我在与笔记本相同的文件夹中有指定的文件“mallet”,但似乎无法访问它。我尝试从 C 盘路由到它,但我仍然得到同样的错误。请帮忙:)

import os
from gensim.models.wrappers import LdaMallet

#os.environ.update({'MALLET_HOME':r'C:/Users/new_mallet/mallet-2.0.8/'})

mallet_path = 'mallet' # update this path

ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=bow_corpus, num_topics=20, id2word=dictionary)

result = (ldamallet.show_topics(num_topics=3, num_words=10,formatted=False))
for each in result:
    print (each)

【问题讨论】:

  • 在 Python 堆栈跟踪之前是否有任何其他错误输出?如果在收到错误后立即尝试显示的命令行 (mallet import-file ...") 会怎样——是否显示了更多信息?
  • @gojomo 感谢您与我联系,我很感激。我尝试了您列出的命令,但仍然遇到相同的错误:(
  • @gojomo 在命令提示符下,命令返回“mallet”不是内部或外部命令、可运行程序或批处理文件。
  • 这表明必要的mallet 可执行文件要么未安装,要么无法从 Python 解释器的位置找到,而您在手动重试时正在执行。确定安装了吗?您能否将您的 mallet_path 变量修复为真正成为 mallet 可执行文件的有效路径?
  • @gojomo 据我所知,mallet 已安装,但我似乎找不到可执行文件。我什至编写了路径环境变量,并三倍检查了我的路径。

标签: python windows jupyter-notebook gensim mallet


【解决方案1】:

更新路径:

mallet_path = 'C:/mallet/mallet-2.0.8/bin/mallet.bat'

并编辑 mallet 2.0.8 文件夹中的记事本 mallet.bat 以:

@echo off

rem This batch file serves as a wrapper for several
rem  MALLET command line tools.

if not "%MALLET_HOME%" == "" goto gotMalletHome

echo MALLET requires an environment variable MALLET_HOME.
goto :eof

:gotMalletHome

set MALLET_CLASSPATH=C:\mallet\mallet-2.0.8\class;C:\mallet\mallet-2.0.8\lib\mallet-deps.jar
set MALLET_MEMORY=1G
set MALLET_ENCODING=UTF-8

set CMD=%1
shift

set CLASS=
if "%CMD%"=="import-dir" set CLASS=cc.mallet.classify.tui.Text2Vectors
if "%CMD%"=="import-file" set CLASS=cc.mallet.classify.tui.Csv2Vectors
if "%CMD%"=="import-svmlight" set CLASS=cc.mallet.classify.tui.SvmLight2Vectors
if "%CMD%"=="info" set CLASS=cc.mallet.classify.tui.Vectors2Info
if "%CMD%"=="train-classifier" set CLASS=cc.mallet.classify.tui.Vectors2Classify
if "%CMD%"=="classify-dir" set CLASS=cc.mallet.classify.tui.Text2Classify
if "%CMD%"=="classify-file" set CLASS=cc.mallet.classify.tui.Csv2Classify
if "%CMD%"=="classify-svmlight" set CLASS=cc.mallet.classify.tui.SvmLight2Classify
if "%CMD%"=="train-topics" set CLASS=cc.mallet.topics.tui.TopicTrainer
if "%CMD%"=="infer-topics" set CLASS=cc.mallet.topics.tui.InferTopics
if "%CMD%"=="evaluate-topics" set CLASS=cc.mallet.topics.tui.EvaluateTopics
if "%CMD%"=="prune" set CLASS=cc.mallet.classify.tui.Vectors2Vectors
if "%CMD%"=="split" set CLASS=cc.mallet.classify.tui.Vectors2Vectors
if "%CMD%"=="bulk-load" set CLASS=cc.mallet.util.BulkLoader
if "%CMD%"=="run" set CLASS=%1 & shift

if not "%CLASS%" == "" goto gotClass

echo Mallet 2.0 commands: 
echo   import-dir        load the contents of a directory into mallet instances (one per file)
echo   import-file       load a single file into mallet instances (one per line)
echo   import-svmlight   load a single SVMLight format data file into mallet instances (one per line)
echo   info              get information about Mallet instances
echo   train-classifier  train a classifier from Mallet data files
echo   classify-dir      classify data from a single file with a saved classifier
echo   classify-file     classify the contents of a directory with a saved classifier
echo   classify-svmlight classify data from a single file in SVMLight format
echo   train-topics      train a topic model from Mallet data files
echo   infer-topics      use a trained topic model to infer topics for new documents
echo   evaluate-topics   estimate the probability of new documents given a trained model
echo   prune             remove features based on frequency or information gain
echo   split             divide data into testing, training, and validation portions
echo   bulk-load         for big input files, efficiently prune vocabulary and import docs
echo Include --help with any option for more information


goto :eof

:gotClass

set MALLET_ARGS=

:getArg

if "%1"=="" goto run
set MALLET_ARGS=%MALLET_ARGS% %1
shift
goto getArg

:run

"C:\Program Files\Java\jdk-12\bin\java" -ea -Dfile.encoding=%MALLET_ENCODING% -classpath %MALLET_CLASSPATH% %CLASS% %MALLET_ARGS%

:eof

在命令行中,这些命令有助于弄清楚发生了什么:

notepad mallet.bat
java
C:\Program Files\Java\jdk-12\bin\java
dir /OD
cd %userdir%
cd %userpath%
cd\
cd users
cd your_username
cd appdata\local\temp\2
dir /OD

问题在于没有正确安装 java 或路径不包括 java 和 mallet 类路径未正确定义。更多信息在这里:https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html。这解决了我的错误,希望它可以帮助其他人:)

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我所做的是将槌文件夹的位置更改为 c://new_mallet 所以效果很好

        import os
        os.environ.update({'MALLET_HOME': r'C:/new_mallet/mallet-2.0.8/'})
        mallet_path = 'C:/new_mallet/mallet-2.0.8/bin/mallet'  # update this path
        ldamallet = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=10, id2word=id2word)
    

    【讨论】:

      【解决方案3】:

      在带有 Python 的 Jupyter Notebook 中,我运行了一个

      conda uninstall gensim
      conda install gensim
      

      以管理员身份在 cmd 中重新启动我的内核。在我花费大量时间在线搜索之后,我的工作就像魅力一样。

      【讨论】:

      • 您还记得您切换的版本吗?我似乎无法用这个解决方案做对。我的 gensim 版本是 3.8.3,因为 4.0.1 没有包装器
      【解决方案4】:

      确保您安装了 Java 开发人员工具包 (JDK)。

      感谢this another answer

      安装 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,
          )
      

      【讨论】:

      • 安装JDK后,你添加到路径了吗?我也有 jre,我已经将它添加到 JAVA_HOME 所以不确定我应该保留它还是使用 JDK 路径...
      【解决方案5】:

      对我来说,这不是导入或路径问题。

      我花了几个小时试图解决它。 试过这个solution 没有任何效果。

      查看我之前对 LDA Mallet 的成功调用,我注意到没有设置一些参数,然后我将其设置为:

      gensim.models.wrappers.LdaMallet(mallet_path=mallet_path, corpus=corpus, num_topics=num_topics, id2word=id2word, prefix='temp_file_', workers=4)

      我真的希望它可以帮助你。找到解决这个问题的方法很痛苦。

      【讨论】:

        【解决方案6】:

        对于 linux,我发现需要明确定义二进制槌路径。以下代码有效。

        from gensim.test.utils import common_corpus, common_dictionary
        from gensim.models.wrappers import LdaMallet
        
        mallet_path = "/path/Mallet/bin/mallet"
        model = LdaMallet(mallet_path=mallet_path, corpus=common_corpus, num_topics=2, id2word=common_dictionary)
        

        【讨论】:

          【解决方案7】:

          对于仍在苦苦挣扎并花费数小时尝试许多不同建议的其他人,我终于成功了!

          按照此处的说明进行操作(我在 mac 上)

          https://ps.au.dk/fileadmin/ingen_mappe_valgt/installing_mallet.pdf

          我在开始之前也关闭了anaconda,不知道这是否重要。

          在终端中我收到以下错误:

          (base) myname-MacBook-Air:mallet-2.0.8 myname$ ./bin/mallet
          -bash: ./bin/mallet: /bin/bash: bad interpreter: Operation not permitted
          

          然后我按照这些说明取消隔离

          “bad interpreter: Operation not permitted” Error on El Capitan

          重新打开 anaconda,一切正常!

          【讨论】:

          • 欢迎来到 Stack Overflow!在这里,没有人应该通过链接来获得他们需要的答案。将所有步骤和代码放入您的答案中
          【解决方案8】:

          【讨论】:

          【解决方案9】:

          我遇到了同样的错误,因为我忘记在我的 ubuntu 上安装 java。

          【讨论】:

            猜你喜欢
            • 2019-10-02
            • 1970-01-01
            • 2019-02-09
            • 2021-01-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-05
            相关资源
            最近更新 更多