Repository: biolab/orange3-text

Issue:
Reference term LSA



##### Expected behavior

We would like to to measure coherence of uttered speech to questions or a reference term.

##### Actual behavior

So far,  there is no way of include such a reference term or sentence.


##### Steps to reproduce the behavior



##### Additional info (worksheets, data, screenshots, ...)





Discussion:

Resolving patch:
diff --git a/orangecontrib/text/topics/topics.py b/orangecontrib/text/topics/topics.py
--- a/orangecontrib/text/topics/topics.py
+++ b/orangecontrib/text/topics/topics.py
@@ -31,6 +31,7 @@ def __init__(self, **kwargs):
         self.kwargs = kwargs
         self.model = None
         self.topic_names = []
+        self.n_words = 0
         self.running = False
 
     def fit(self, corpus, **kwargs):
@@ -44,6 +45,7 @@ def fit(self, corpus, **kwargs):
         self.reset_model(corpus)
         self.running = True
         self.update(corpus.ngrams_corpus, **kwargs)
+        self.n_words = len(corpus.dictionary)
         self.topic_names = ['Topic{} ({})'.format(i, ', '.join(words))
                             for i, words in enumerate(self._topics_words(3), 1)]
         self.running = False
@@ -102,6 +104,26 @@ def get_topics_table_by_id(self, topic_id):
         t.name = 'Topic_{}'.format(topic_id + 1)
         return t
 
+    def get_all_topics_table(self):
+        """ Transform all topics from gensim model to table. """
+        all_words = self._topics_words(self.n_words)
+        all_weights = self._topics_weights(self.n_words)
+        sorted_words = sorted(all_words[0])
+
+        X = []
+        for words, weights in zip(all_words, all_weights):
+            weights = [we for wo, we in sorted(zip(words, weights))]
+            X.append(weights)
+        X = np.array(X).T
+
+        domain = Domain([ContinuousVariable(n) for n in self.topic_names],
+                        metas=[StringVariable('Word')])
+        t = Table.from_numpy(domain,
+                             X=X,
+                             metas=np.array(sorted_words)[:, None])
+        t.name = 'All topics'
+        return t
+
     def get_top_words_by_id(self, topic_id, num_of_words=10):
         topics = self._topics_words(num_of_words=num_of_words)
         if not 0 <= topic_id < self.num_topics:
diff --git a/orangecontrib/text/widgets/owtopicmodeling.py b/orangecontrib/text/widgets/owtopicmodeling.py
--- a/orangecontrib/text/widgets/owtopicmodeling.py
+++ b/orangecontrib/text/widgets/owtopicmodeling.py
@@ -108,7 +108,8 @@ class Inputs:
 
     class Outputs:
         corpus = Output("Corpus", Table)
-        topic = Output("Topic", Topic)
+        selected_topic = Output("Selected Topic", Topic)
+        all_topics = Output("All Topics", Table)
 
     want_main_area = True
 
@@ -207,9 +208,11 @@ def on_start(self):
     def on_result(self, corpus):
         self.progressBarFinished(None)
         self.Outputs.corpus.send(corpus)
+        self.Outputs.all_topics.send(self.model.get_all_topics_table())
         if corpus is None:
             self.topic_desc.clear()
-            self.Outputs.topic.send(None)
+            self.Outputs.selected_topic.send(None)
+            self.Outputs.all_topics.send(None)
         else:
             self.topic_desc.show_model(self.model)
 
@@ -225,7 +228,7 @@ def send_report(self):
 
     def send_topic_by_id(self, topic_id=None):
         if self.model.model and topic_id is not None:
-            self.Outputs.topic.send(self.model.get_topics_table_by_id(topic_id))
+            self.Outputs.selected_topic.send(self.model.get_topics_table_by_id(topic_id))
 
 
 class TopicViewerTreeWidgetItem(QTreeWidgetItem):


Test patch: