Skip to content

Commit

Permalink
lsimodel: Only log top words that actually exist in <id2word>
Browse files Browse the repository at this point in the history
In some pathological cases, we might try to log the top N words, even
though we haven't seen N words yet.  In these cases, we can just exit
the loop early.

Closes #3090.
  • Loading branch information
kmurphy4 committed Mar 25, 2021
1 parent f8110c0 commit 9278555
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion gensim/models/lsimodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def show_topic(self, topicno, topn=10):
c = np.asarray(self.projection.u.T[topicno, :]).flatten()
norm = np.sqrt(np.sum(np.dot(c, c)))
most = matutils.argsort(np.abs(c), topn, reverse=True)
return [(self.id2word[val], 1.0 * c[val] / norm) for val in most]
return [(self.id2word[val], 1.0 * c[val] / norm) for val in most if val in self.id2word]

def show_topics(self, num_topics=-1, num_words=10, log=False, formatted=True):
"""Get the most significant topics.
Expand Down

0 comments on commit 9278555

Please sign in to comment.