# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import random
import shelve
import gensim
from gensim.models.ldamodel import LdaModel
from gensim.models.ldamulticore import LdaMulticore

class LdaWrapper():
    def __init__(self, lda_filepath, dictionary_filepath, cache_filepath=None):
        self.lda = LdaMulticore.load(lda_filepath)
        self.dictionary = gensim.corpora.dictionary.Dictionary.load(dictionary_filepath)
        #self.cache = dict()
        #self.cache_max_size = cache_max_size
        self.cache_synch_prob = 2 # in percent, 1 to 100
        self.cache_filepath = cache_filepath
        self.cache = shelve.open(cache_filepath) if cache_filepath is not None else None
    
    def get_topics(self, text):
        if self.cache is None:
            return self.get_topics_uncached(text)
        else:
            _hash = str(hash(text))

            if self.cache.has_key(_hash):
                return self.cache[_hash]
            else:
                topics = self.get_topics_uncached(text)
                
                self.cache[_hash] = topics
                if random.randint(1, 100) <= self.cache_synch_prob:
                    self.synchronize_cache()
                
                return topics
            
    def get_topics_uncached(self, text):
        tokens = text.lower().split(" ")
        return self.lda[self.dictionary.doc2bow(tokens)]

    def synchronize_cache(self):
        self.cache.sync()

@@ -17,16 +17,72 @@
 f-8 -*-%0A
+%22%22%22Class that wraps a previously trained gensim LDA.%22%22%22%0A
 from __f
@@ -323,83 +323,608 @@
     
-def __init__(self, lda_filepath, dictionary_filepath, cache_filepath=None):
+%22%22%22Class that wraps a previously trained gensim LDA.%0A    %0A    This class uses a shelve cache to store generated results. This speeds up the generation%0A    of training examples, if the identical corpus, window sizes etc. are used.%0A    %22%22%22%0A    def __init__(self, lda_filepath, dictionary_filepath, cache_filepath=None):%0A        %22%22%22Initialize the LDA wrapper.%0A        Args:%0A            lda_filepath: Filepath to the trained LDA model.%0A            dictionary_filepath: Filepath to the dictionary of the LDA.%0A            cache_filepath: Optional filepath to a shelve cache for the LDA results.%0A        %22%22%22
 %0A   
@@ -1064,83 +1064,8 @@
 th)%0A
-        #self.cache = dict()%0A        #self.cache_max_size = cache_max_size%0A
     
@@ -1280,32 +1280,248 @@
 cs(self, text):%0A
+        %22%22%22Returns the topics of a small string text window.%0A        Args:%0A            text: A small text window as string.%0A        Returns:%0A            List of tuples of form (topic index, probability).%0A        %22%22%22%0A
         if self.
@@ -2059,32 +2059,275 @@
 ed(self, text):%0A
+        %22%22%22Returns the topics of a small string text window without querying the cache.%0A        Args:%0A            text: A small text window as string.%0A        Returns:%0A            List of tuples of form (topic index, probability).%0A        %22%22%22%0A
         tokens =
@@ -2442,16 +2442,100 @@
 (self):%0A
+        %22%22%22Synchronizes the shelve cache on the HDD with the version in the RAM.%22%22%22%0A
         

Add documentation for lda.py