1

I've imported all the packages I need

from gensim import corpora
from gensim import models
from gensim.models import LdaModel
from gensim.models import TfidfModel
from gensim.models import CoherenceModel

and then I need to run the LdaMallet model so I import them like this

from gensim.models.wrappers import LdaMallet

when run the code below, I've got some Namerror:

mallet_path = 'mallet-2.0.8/bin/mallet' # update this path

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

Error occurred:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-1c656d4f8c21> in <module>()
      1 mallet_path = 'mallet-2.0.8/bin/mallet' # update this path
      2 
----> 3 ldamallet = gensim.models.wrappers.LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary)

NameError: name 'gensim' is not defined

I thought I've imported all the things that I need, and the lda model ran well before I tried to use mallet. So what's the problem?

elixenide
  • 44,308
  • 16
  • 74
  • 100
Helix Herry
  • 327
  • 1
  • 4
  • 14
  • 1
    I'm not a Python expert, but I'm pretty sure that doing `from gensim import models` means you need to refer to `models.wrappers`, etc., not `gensim.models.wrappers`. Please let me know if that's helpful, and I can add it as an answer. – elixenide Jan 27 '19 at 07:09
  • ModuleNotFoundError: No module named 'gensim.wrappers' – Helix Herry Jan 27 '19 at 07:11
  • now I have a new problem,I just import gensim and run it again,a new error occorred'CalledProcessError: Command 'mallet-2.0.8/bin/mallet import-file --preserve-case --keep-sequence --remove-stopwords --token-regex "\S+" --input /var/folders/br/txtq3sps39sgn70v4tlm17q40000gn/T/2ab601_corpus.txt --output /var/folders/br/txtq3sps39sgn70v4tlm17q40000gn/T/2ab601_corpus.mallet' returned non-zero exit status 1.' – Helix Herry Jan 27 '19 at 07:13
  • it seems that I need to install the JDK to use java command line,but how can I install it?I've tried in various ways before but failed – Helix Herry Jan 27 '19 at 07:15

2 Answers2

2

Because you have this import:

from gensim import models

you would need to refer to wrappers in your code as models.wrappers, etc., not gensim.models.wrappers.

But you're also doing this:

from gensim.models.wrappers import LdaMallet

so you can just refer to LdaMallet directly, as in:

ldamallet = LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary)

Note that I left out the gensim.models.wrappers. here; you don't need it.

elixenide
  • 44,308
  • 16
  • 74
  • 100
1

Just use LdaMallet(mallet_path,corpus=corpus, num_topics=20, id2word=dictionary) straightaway because you already have imported the required method from gensim.models.wrappers

Ashu Grover
  • 737
  • 1
  • 11
  • 26