<commit_before>from gensim import corpora, models, matutils

from Orange.widgets.widget import OWWidget
from Orange.widgets.settings import Setting
from Orange.widgets import gui
from Orange.data import Table, ContinuousVariable, Domain
from Orange.widgets.data.contexthandlers import DomainContextHandler
from orangecontrib.text.corpus import Corpus
from orangecontrib.text.preprocess import Preprocessor


class Output:
    DATA = "Data"


class OWLDA(OWWidget):
    # Basic widget info
    name = "LDA"
    description = "Latent Dirichlet Allocation topic model."
    icon = "icons/mywidget.svg"

    settingsHandler = DomainContextHandler()

    # Input/output
    inputs = [("Corpus", Table, "set_data"),  # hack to accept input signals of type Table
              ("Preprocessor", Preprocessor, "set_preprocessor")]
    outputs = [(Output.DATA, Table)]
    want_main_area = False

    # Settings
    num_topics = Setting(5)
    use_tfidf = Setting(False)

    def __init__(self):
        super().__init__()

        self.corpus = None
        self.preprocessor = Preprocessor()

        # Info.
        info_box = gui.widgetBox(self.controlArea, "Info")
        self.info_label = gui.label(info_box, self, '')

        # Settings.
        topic_box = gui.widgetBox(self.controlArea, "Settings")
        self.num_topics_input = gui.spin(
            topic_box, self, "num_topics", label="Number of topics: ",
            minv=1, maxv=2 ** 31 - 1,)
        self.use_tfidf_button = gui.checkBox(topic_box, self, "use_tfidf",
                     "Use TF-IDF weighting")

        # Commit button
        self.commit = gui.button(self.controlArea, self, "&Commit",
                                 callback=self.apply, default=True)

        self.refresh_gui()

    def set_preprocessor(self, data):
        if data is None:
            self.preprocessor = Preprocessor()
        else:
            self.preprocessor = data
        self.apply()

    def set_data(self, data=None):
        self.error(1)
        if data is None or isinstance(data, Corpus):
            self.corpus = data
        else:
            self.corpus = None
            self.error(1, 'Input should be of type Corpus')
        self.apply()

    def refresh_gui(self):
        got_corpus = self.corpus is not None
        self.num_topics_input.setEnabled(got_corpus)
        self.commit.setEnabled(got_corpus)
        self.use_tfidf_button.setEnabled(got_corpus)

        ndoc = len(self.corpus) if got_corpus else "(None)"
        self.info_label.setText("Input text entries: {}".format(ndoc))

    def apply(self):
        self.refresh_gui()
        if self.corpus:
            # Preprocess
            preprocessed = self.preprocessor(self.corpus.documents)
            dictionary = corpora.Dictionary(preprocessed)
            corpus = [dictionary.doc2bow(t) for t in preprocessed]

            # LDA
            if self.use_tfidf:
                tfidf = models.TfidfModel(corpus)
                corpus = tfidf[corpus]

            lda = models.LdaModel(corpus, id2word=dictionary,
                                  num_topics=self.num_topics)<commit_msg>LDA spinbox now resizes well.<commit_after>from gensim import corpora, models, matutils

from Orange.widgets.widget import OWWidget
from Orange.widgets.settings import Setting
from Orange.widgets import gui
from Orange.data import Table, ContinuousVariable, Domain
from Orange.widgets.data.contexthandlers import DomainContextHandler
from orangecontrib.text.corpus import Corpus
from orangecontrib.text.preprocess import Preprocessor


class Output:
    DATA = "Data"


class OWLDA(OWWidget):
    # Basic widget info
    name = "LDA"
    description = "Latent Dirichlet Allocation topic model."
    icon = "icons/mywidget.svg"

    settingsHandler = DomainContextHandler()

    # Input/output
    inputs = [("Corpus", Table, "set_data"),  # hack to accept input signals of type Table
              ("Preprocessor", Preprocessor, "set_preprocessor")]
    outputs = [(Output.DATA, Table)]
    want_main_area = False

    # Settings
    num_topics = Setting(5)
    use_tfidf = Setting(False)

    def __init__(self):
        super().__init__()

        self.corpus = None
        self.preprocessor = Preprocessor()

        # Info.
        info_box = gui.widgetBox(self.controlArea, "Info")
        self.info_label = gui.label(info_box, self, '')

        # Settings.
        topic_box = gui.widgetBox(self.controlArea, "Settings")
        hbox = gui.widgetBox(topic_box, orientation=0)
        self.topics_label = gui.label(hbox, self, 'Number of topics: ')
        self.topics_label.setMaximumSize(self.topics_label.sizeHint())
        self.topics_input = gui.spin(hbox, self, "num_topics",
                                     minv=1, maxv=2 ** 31 - 1,)
        self.tfidf_button = gui.checkBox(topic_box, self, "use_tfidf",
                                         "Use TF-IDF weighting")

        # Commit button
        self.commit = gui.button(self.controlArea, self, "&Commit",
                                 callback=self.apply, default=True)

        self.refresh_gui()

    def set_preprocessor(self, data):
        if data is None:
            self.preprocessor = Preprocessor()
        else:
            self.preprocessor = data
        self.apply()

    def set_data(self, data=None):
        self.error(1)
        if data is None or isinstance(data, Corpus):
            self.corpus = data
        else:
            self.corpus = None
            self.error(1, 'Input should be of type Corpus')
        self.apply()

    def refresh_gui(self):
        got_corpus = self.corpus is not None
        self.topics_label.setEnabled(got_corpus)
        self.topics_input.setEnabled(got_corpus)
        self.commit.setEnabled(got_corpus)
        self.tfidf_button.setEnabled(got_corpus)

        ndoc = len(self.corpus) if got_corpus else "(None)"
        self.info_label.setText("Input text entries: {}".format(ndoc))

    def apply(self):
        self.refresh_gui()
        if self.corpus:
            # Preprocess
            preprocessed = self.preprocessor(self.corpus.documents)
            dictionary = corpora.Dictionary(preprocessed)
            corpus = [dictionary.doc2bow(t) for t in preprocessed]

            # LDA
            if self.use_tfidf:
                tfidf = models.TfidfModel(corpus)
                corpus = tfidf[corpus]

            lda = models.LdaModel(corpus, id2word=dictionary,
                                  num_topics=self.num_topics)
{'fraction_non_alphanumeric': 0.09613022113022113, 'fraction_numerical': 0.0032248157248157246, 'mean_word_length': 2.291056088933805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022944', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1893, 'n_words': 436}