Repository: piskvorky/gensim

Issue:
LdaMallet wrapper giving AttributeError
This is on `develop` and it is in sync with upstream. Here's the code:

``` python
from gensim.corpora.dictionary import Dictionary
texts = [['human', 'interface', 'computer'],
         ['survey', 'user', 'computer', 'system', 'response', 'time'],
         ['eps', 'user', 'interface', 'system'],
         ['system', 'human', 'system', 'eps'],
         ['user', 'response', 'time'],
         ['trees'],
         ['graph', 'trees'],
         ['graph', 'minors', 'trees'],
         ['graph', 'minors', 'survey']]
dictionary = Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lm = LdaMallet('/home/devashish/mallet-2.0.8RC3/bin/mallet', corpus=corpus, num_topics=7, id2word=dictionary)
```

Here's the traceback:

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-a3fdd828cc09> in <module>()
----> 1 lm = LdaMallet('/home/devashish/mallet-2.0.8RC3/bin/mallet', corpus=corpus, num_topics=7, id2word=dictionary)

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in __init__(self, mallet_path, corpus, num_topics, alpha, id2word, workers, prefix, optimize_interval, iterations, topic_threshold)
     98 
     99         if corpus is not None:
--> 100             self.train(corpus)
    101 
    102     def finferencer(self):

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in train(self, corpus)
    166         logger.info("training MALLET LDA with %s", cmd)
    167         check_output(cmd, shell=True)
--> 168         self.word_topics = self.load_word_topics()
    169         # NOTE - we are still keeping the wordtopics variable to not break backward compatibility.
    170         # word_topics has replaced wordtopics throughout the code; wordtopics just stores the values of word_topics when train is called.

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in load_word_topics(self)
    205                 tokenid = word2id[token]
    206                 word_topics[int(topic), tokenid] += 1.0
--> 207         self.print_topics(15)
    208         return word_topics
    209 

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in print_topics(self, num_topics, num_words)
    209 
    210     def print_topics(self, num_topics=10, num_words=10):
--> 211         return self.show_topics(num_topics, num_words, log=True)
    212 
    213     def load_document_topics(self):

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in show_topics(self, num_topics, num_words, log, formatted)
    237         for i in chosen_topics:
    238             if formatted:
--> 239                 topic = self.print_topic(i, num_words=num_words)
    240             else:
    241                 topic = self.show_topic(i, num_words=num_words)

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in print_topic(self, topicid, num_words)
    255 
    256     def print_topic(self, topicid, num_words=10):
--> 257         return ' + '.join(['%.3f*%s' % v for v in self.show_topic(topicid, num_words)])
    258 
    259 

/home/devashish/EXPERIMENTATION/gensim/gensim/models/wrappers/ldamallet.pyc in show_topic(self, topicid, num_words)
    246 
    247     def show_topic(self, topicid, num_words=10):
--> 248         if self.word_topics is None:
    249             logger.warn("Run train or load_word_topics before showing topics.")
    250         topic = self.word_topics[topicid]

AttributeError: 'LdaMallet' object has no attribute 'word_topics'
```
#767 might be causing this.



Discussion:
So the reason this is happening is because in line 207, `self.print_topics(15)` is called before `word_topics` is assigned to the `LdaMallet` object.

Working to fix this.


Resolving patch:
diff --git a/gensim/models/wrappers/ldamallet.py b/gensim/models/wrappers/ldamallet.py
--- a/gensim/models/wrappers/ldamallet.py
+++ b/gensim/models/wrappers/ldamallet.py
@@ -95,7 +95,6 @@ def __init__(self, mallet_path, corpus=None, num_topics=100, alpha=50, id2word=N
         self.workers = workers
         self.optimize_interval = optimize_interval
         self.iterations = iterations
-
         if corpus is not None:
             self.train(corpus)
 
@@ -204,7 +203,6 @@ def load_word_topics(self):
                     continue
                 tokenid = word2id[token]
                 word_topics[int(topic), tokenid] += 1.0
-        self.print_topics(15)
         return word_topics
 
     def print_topics(self, num_topics=10, num_words=10):


Test patch: