Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LsiModel: Only log top words that actually exist in the dictionary #3091

Merged
merged 5 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes
=======

## Unreleased

- LsiModel: Only log top words that actually exist in the dictionary (PR [#3091](https://github.com/RaRe-Technologies/gensim/pull/3091), [@kmurphy4](https://github.com/kmurphy4))

## 4.0.1, 2021-04-01

Bugfix release to address issues with Wheels on Windows:
Expand Down
4 changes: 3 additions & 1 deletion gensim/models/lsimodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ 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]

# Output only (word, score) pairs for `val`s that are within `self.id2word`. See #3090 for details.
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
3 changes: 3 additions & 0 deletions gensim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ def __getitem__(self, val):
return str(val)
raise ValueError("internal id out of bounds (%s, expected <0..%s))" % (val, self.num_terms))

def __contains__(self, val):
return 0 <= val < self.num_terms

def iteritems(self):
"""Iterate over all keys and values.

Expand Down