Skip to content

Commit

Permalink
rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
markroxor committed Oct 13, 2016
1 parent abe194d commit c25d636
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 10 additions & 0 deletions gensim/models/basemodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class BaseTopicModel():
def print_topic(self, topicno, topn=10):
"""
Return a single topic as a formatted string. See `show_topic()` for parameters.
>>> lsimodel.print_topic(10, topn=5)
'-0.340 * "category" + 0.298 * "$M$" + 0.183 * "algebra" + -0.174 * "functor" + -0.168 * "operator"'
"""
return ' + '.join(['%.3f*"%s"' % (v, k) for k, v in self.show_topic(topicno, topn)])
8 changes: 3 additions & 5 deletions gensim/models/ldamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import numbers

from gensim import interfaces, utils, matutils
from gensim.models import basemodel

from itertools import chain
from scipy.special import gammaln, psi # gamma function utils
from scipy.special import polygamma
Expand Down Expand Up @@ -193,7 +195,7 @@ def get_Elogbeta(self):
# endclass LdaState


class LdaModel(interfaces.TransformationABC):
class LdaModel(interfaces.TransformationABC,basemodel.BaseTopicModel):
"""
The constructor estimates Latent Dirichlet Allocation model parameters based
on a training corpus:
Expand Down Expand Up @@ -833,10 +835,6 @@ def get_topic_terms(self, topicid, topn=10):
bestn = matutils.argsort(topic, topn, reverse=True)
return [(id, topic[id]) for id in bestn]

def print_topic(self, topicid, topn=10):
"""Return the result of `show_topic`, but formatted as a single string."""
return ' + '.join(['%.3f*%s' % (v, k) for k, v in self.show_topic(topicid, topn)])

def top_topics(self, corpus, num_words=20):
"""
Calculate the Umass topic coherence for each topic. Algorithm from
Expand Down
14 changes: 3 additions & 11 deletions gensim/models/lsimodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
from scipy.sparse import sparsetools

from gensim import interfaces, matutils, utils
from gensim.models import basemodel

from six import iterkeys
from six.moves import xrange

Expand Down Expand Up @@ -221,7 +223,7 @@ def merge(self, other, decay=1.0):
#endclass Projection


class LsiModel(interfaces.TransformationABC):
class LsiModel(interfaces.TransformationABC,basemodel.BaseTopicModel):
"""
Objects of this class allow building and maintaining a model for Latent
Semantic Indexing (also known as Latent Semantic Analysis).
Expand Down Expand Up @@ -490,16 +492,6 @@ def show_topic(self, topicno, topn=10):
most = matutils.argsort(numpy.abs(c), topn, reverse=True)
return [(self.id2word[val], 1.0 * c[val] / norm) for val in most]

def print_topic(self, topicno, topn=10):
"""
Return a single topic as a formatted string. See `show_topic()` for parameters.
>>> lsimodel.print_topic(10, topn=5)
'-0.340 * "category" + 0.298 * "$M$" + 0.183 * "algebra" + -0.174 * "functor" + -0.168 * "operator"'
"""
return ' + '.join(['%.3f*"%s"' % (v, k) for k, v in self.show_topic(topicno, topn)])

def show_topics(self, num_topics=-1, num_words=10, log=False, formatted=True):
"""
Return `num_topics` most significant topics (return all by default).
Expand Down

0 comments on commit c25d636

Please sign in to comment.