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

Optimised show_topics #1028

Merged
merged 4 commits into from
Nov 27, 2016
Merged
Changes from 2 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
17 changes: 13 additions & 4 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,15 +800,24 @@ def show_topics(self, num_topics=10, num_words=10, log=False, formatted=True):
chosen_topics = sorted_topics[:num_topics // 2] + sorted_topics[-num_topics // 2:]

shown = []

topic = self.state.get_lambda()
for i in chosen_topics:
if formatted:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much code duplication
Just add an extra if formatted block in the end that changes formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, that was quite stupid of me 💩
Will fix!

topic = self.print_topic(i, topn=num_words)
topic_ = topic[i]
topic_ = topic_ / topic_.sum() # normalize to probability distribution
bestn = matutils.argsort(topic_, num_words, reverse=True)
topic_ = [(self.id2word[id], topic_[id]) for id in bestn]
topic_ = ' + '.join(['%.3f*"%s"' % (v, k) for k, v in topic_])
else:
topic = self.show_topic(i, topn=num_words)
topic_ = topic[i]
topic_ = topic_ / topic_.sum() # normalize to probability distribution
bestn = matutils.argsort(topic_, num_words, reverse=True)
topic_ = [(self.id2word[id], topic_[id]) for id in bestn]

shown.append((i, topic))
shown.append((i, topic_))
if log:
logger.info("topic #%i (%.3f): %s", i, self.alpha[i], topic)
logger.info("topic #%i (%.3f): %s", i, self.alpha[i], topic_)

return shown

Expand Down