Skip to content

Commit

Permalink
Corpus: show error when no string variables at all not when text_feat…
Browse files Browse the repository at this point in the history
…ures empty
  • Loading branch information
PrimozGodec committed Mar 11, 2021
1 parent adde63c commit a1e87a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 5 additions & 3 deletions orangecontrib/text/widgets/owcorpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self):

# Used Text Features
fbox = gui.widgetBox(self.controlArea, orientation=0)
ubox = gui.widgetBox(fbox, "Used text features", addSpace=False)
ubox = gui.widgetBox(fbox, "Used text features")
self.used_attrs_model = VariableListModel(enable_dnd=True)
self.used_attrs_view = VariablesListItemView()
self.used_attrs_view.setModel(self.used_attrs_model)
Expand All @@ -94,7 +94,7 @@ def __init__(self):
aa.rowsRemoved.connect(self.update_feature_selection)

# Ignored Text Features
ibox = gui.widgetBox(fbox, "Ignored text features", addSpace=False)
ibox = gui.widgetBox(fbox, "Ignored text features")
self.unused_attrs_model = VariableListModel(enable_dnd=True)
self.unused_attrs_view = VariablesListItemView()
self.unused_attrs_view.setModel(self.unused_attrs_model)
Expand Down Expand Up @@ -146,6 +146,7 @@ def _load_corpus(path: str, data: Table, state: TaskState) -> Corpus:
def open_file(self, path=None, data=None):
self.closeContext()
self.Error.clear()
self.cancel()
self.unused_attrs_model[:] = []
self.used_attrs_model[:] = []
self.start(self._load_corpus, path, data)
Expand All @@ -158,7 +159,8 @@ def on_done(self, corpus: Corpus) -> None:
self.update_output_info()
self._setup_title_dropdown()
self.used_attrs = list(self.corpus.text_features)
if not self.corpus.text_features:
all_str_features = [f for f in self.corpus.domain.metas if f.is_string]
if not all_str_features:
self.Error.corpus_without_text_features()
self.Outputs.corpus.send(None)
return
Expand Down
36 changes: 35 additions & 1 deletion orangecontrib/text/widgets/tests/test_owcorpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TestOWCorpus(WidgetTest):
def setUp(self):
self.widget = self.create_widget(OWCorpus)
self.widget: OWCorpus = self.create_widget(OWCorpus)

def check_output(self, sel_title):
"""
Expand Down Expand Up @@ -286,6 +286,40 @@ def test_keep_selected_variables(self):
self.wait_until_finished()
self.assertListEqual(list(prew_selected), self.widget.used_attrs)

def test_no_text_feature(self):
"""
Test with data which have empty text_features. Widget should not show
the error but, should have all features unused.
"""
# widget already loads book-excerpts from file and store context
# settings this call restore context settings to default otherwise
# Text variable is moved to used_attributes by the context
self.widget.settingsHandler.reset_to_original(self.widget)
data = Corpus.from_file("book-excerpts")
data.text_features = []
self.send_signal(self.widget.Inputs.data, data)
self.wait_until_finished()
self.assertFalse(
self.widget.Error.corpus_without_text_features.is_shown()
)
self.assertEqual(0, len(list(self.widget.used_attrs_model)))
self.assertListEqual(
[data.domain["Text"]],
list(self.widget.unused_attrs_model)
)

def test_corpus_without_text_features(self):
"""
Test if corpus_without_text_features is correctly raised for data
without text features
"""
data = Table("iris")
self.send_signal(self.widget.Inputs.data, data)
self.wait_until_finished()
self.assertTrue(
self.widget.Error.corpus_without_text_features.is_shown()
)


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

0 comments on commit a1e87a1

Please sign in to comment.