Skip to content

Commit

Permalink
Add malletmodel2ldamodel transformation function (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishd12 authored and tmylk committed Jul 6, 2016
1 parent ff3adb8 commit fb8e4e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gensim/models/wrappers/ldamallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from gensim import utils, matutils
from gensim.utils import check_output
from gensim.models.ldamodel import LdaModel

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -350,3 +351,27 @@ def read_doctopics(self, fname, eps=1e-6, renorm=True):
if total_weight:
doc = [(id_, float(weight) / total_weight) for id_, weight in doc]
yield doc


def malletmodel2ldamodel(mallet_model, gamma_threshold=0.001, iterations=50):
"""
Function to convert mallet model to gensim LdaModel. This works by copying the
training model weights (alpha, beta...) from a trained mallet model into the
gensim model.
Args:
----
mallet_model : Trained mallet model
gamma_threshold : To be used for inference in the new LdaModel.
iterations : number of iterations to be used for inference in the new LdaModel.
Returns:
-------
model_gensim : LdaModel instance; copied gensim LdaModel
"""
model_gensim = LdaModel(
id2word=mallet_model.id2word, num_topics=mallet_model.num_topics,
alpha=mallet_model.alpha, iterations=iterations,
gamma_threshold=gamma_threshold)
model_gensim.expElogbeta[:] = mallet_model.wordtopics
return model_gensim
12 changes: 12 additions & 0 deletions gensim/test/test_ldamallet_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ def testSparseTransform(self):
(i, sorted(vec), sorted(expected)))
self.assertTrue(passed)

def testMallet2Model(self):
if not self.mallet_path:
return
passed = False
tm1 = ldamallet.LdaMallet(self.mallet_path, corpus=corpus, num_topics=2, id2word=dictionary)
tm2 = ldamallet.malletmodel2ldamodel(tm1)
for document in corpus:
self.assertEqual(tm1[document][0], tm2[document][0])
self.assertEqual(tm1[document][1], tm2[document][1])
logging.debug('%d %d', tm1[document][0], tm2[document][0])
logging.debug('%d %d', tm1[document][1], tm2[document][1])


def testPersistence(self):
if not self.mallet_path:
Expand Down

0 comments on commit fb8e4e5

Please sign in to comment.