Skip to content

Commit

Permalink
Collocations
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdapretnar committed Feb 11, 2022
1 parent 287cf2c commit 4e19fd3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions orangecontrib/text/widgets/owcollocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(self) -> None:

# GUI
self.collModel = model = TableModel(parent=self) # type: TableModel
model.setHorizontalHeaderLabels(["Method", "Score"])
self.collView = view = TableView(self) # type: TableView
self.mainArea.layout().addWidget(view)
view.setModel(model)
Expand All @@ -96,6 +97,7 @@ def __init__(self) -> None:
@Inputs.corpus
def set_corpus(self, corpus):
self.collModel.clear()
self.collModel.resetSorting(True)
self.corpus = corpus
self.commit()

Expand All @@ -114,6 +116,8 @@ def _change_type(self):
self.commit()

def compute_scores(self):
self.collModel.clear()
self.collModel.resetSorting(True)
finder = self.type.from_documents(self.corpus.tokens)
finder.apply_freq_filter(self.freq_filter)

Expand Down
72 changes: 72 additions & 0 deletions orangecontrib/text/widgets/tests/test_owcollocations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest

from AnyQt.QtCore import Qt
from Orange.widgets.tests.base import WidgetTest

from orangecontrib.text import Corpus
from orangecontrib.text import preprocess
from orangecontrib.text.widgets.owcollocations import OWCollocations


class TestOWCollocations(WidgetTest):

def setUp(self) -> None:
self.widget: OWCollocations = self.create_widget(OWCollocations)

# create corpus
self.corpus = Corpus.from_file("book-excerpts")

def test_set_data(self):
self.send_signal(self.widget.Inputs.corpus, self.corpus)
output = self.get_output(self.widget.Outputs.corpus)
self.assertEqual(len(self.widget.collModel), 20)
self.assertEqual(len(output), 69309)

def test_preprocessed(self):
pp_list = [
preprocess.LowercaseTransformer(),
preprocess.PunktSentenceTokenizer(),
preprocess.SnowballStemmer(),
]
for p in pp_list:
self.pp_corpus = p(self.corpus)

self.send_signal(self.widget.Inputs.corpus, self.pp_corpus)
self.assertEqual(len(self.widget.collModel), 20)

def test_trigrams(self):
model = self.widget.collModel
self.send_signal(self.widget.Inputs.corpus, self.corpus)
bigram = len(self.widget.collModel[0][0].split(" "))

# trigrams
self.widget.controls.type_index.buttons[1].click()
trigram = len(self.widget.collModel[0][0].split(" "))

self.assertGreater(trigram, bigram)

def test_change_scorer(self):
model = self.widget.collModel
self.send_signal(self.widget.Inputs.corpus, self.corpus)
self.assertEqual(len(model[0]), 2)

for i, _ in enumerate(self.widget.controls.selected_method.buttons):
self.widget.controls.selected_method.buttons[i].click()
self.assertTrue(self.widget.Outputs.corpus)

def test_sort_table(self):
"""Test that sorting the table for one method doesn't crash the
widget when changing method"""
view = self.widget.collView
self.send_signal(self.widget.Inputs.corpus, self.corpus)
score = self.widget.collModel[0][1]

view.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)

# change method
self.widget.controls.selected_method.buttons[1].click()
self.assertNotEqual(self.widget.collModel[0][1], score)


if __name__ == "__main__":
unittest.main()

0 comments on commit 4e19fd3

Please sign in to comment.