Skip to content

Commit

Permalink
Word List: Handle words selection
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Mar 24, 2021
1 parent a4ec92c commit ea34e07
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
43 changes: 28 additions & 15 deletions orangecontrib/text/widgets/owwordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
PyListModel
from Orange.widgets.widget import Input, Output, OWWidget, Msg

from orangecontrib.text import Corpus


@contextmanager
def disconnected(signal, slot, connection_type=Qt.AutoConnection):
Expand Down Expand Up @@ -156,7 +154,7 @@ class Inputs:

class Outputs:
selected_words = Output("Selected Words", Table)
words = Output("Words", Corpus)
words = Output("Words", Table)

class Warning(OWWidget.Warning):
no_string_vars = Msg("Input 'Words' needs at least one Text variable.")
Expand Down Expand Up @@ -277,6 +275,7 @@ def __on_library_selection_changed(self, selected: QItemSelection, *_):
current = index[0]
word_list: WordList = self.library_model[current]
self.word_list_index = current
self.selected_words = set()
self.words_model.wrap(list(word_list.cached_words))
self._apply_update_rule()

Expand Down Expand Up @@ -492,17 +491,26 @@ def _apply_update_rule(self):
self.library_model[lib_index].update_rule_flag = modified
self._set_word_list_modified(mod_type=self.NONE)
self.library_view.repaint()
self.commit()

# Apply selection. selection_changed invokes commit().
# If there is no selection, call commit explicitly.
if any(w in self.selected_words for w in self.words_model):
self.set_selected_words()
self.words_view.repaint()
else:
self.commit()

def commit(self):
selection = self._get_selected_words_indices()
self.selected_words = set(np.array(self.words_model)[selection])

words, selected_words = None, None
if self.words_model:
words_var = StringVariable("Words")
words_var.attributes = {"type": "words"}
domain = Domain([], metas=[words_var])
_words = Table.from_list(domain, [[w] for w in self.words_model])
_words.name = "Words"
selection = self._get_selected_words_indices()
if selection:
selected_words = _words[selection]
words = create_annotated_table(_words, selection)
Expand Down Expand Up @@ -546,29 +554,34 @@ def _get_selected_words_indices(self) -> List[int]:
rows = self.words_view.selectionModel().selectedRows()
return [row.row() for row in rows]

def set_selected_words(self):
if self.selected_words:
indices = [i for i, w in enumerate(self.words_model)
if w in self.selected_words]
self._set_selected_words(indices)

def _restore_state(self):
index = self.word_list_index
source = [WordList.from_dict(s) for s in self.word_list_library]
self.library_model.wrap(source)
self._set_selected_word_list(index)
# __on_library_selection_changed() (invoked by _set_selected_word_list)
# clears self.selected_words
selected_words = self.selected_words
self._set_selected_word_list(self.word_list_index)

if self.words is not None:
self.words_model.wrap(list(self.words))
self._set_word_list_modified(mod_type=self.CACHED)
if self.selected_words:
indices = [i for i, w in enumerate(self.words)
if w in self.selected_words]
self._set_selected_words(indices)
else:
if selected_words:
self.selected_words = selected_words
self.set_selected_words()
elif len(self.word_list_library) > self.word_list_index and \
self.word_list_library[self.word_list_index] != self.words:
self.commit()

def _save_state(self):
self.word_list_library = [s.as_dict() for s in self.library_model]
self.words = self.words_model[:]

selected_indices = self._get_selected_words_indices()
self.selected_words = set(np.array(self.words_model)[selected_indices])


if __name__ == "__main__":
from Orange.widgets.utils.widgetpreview import WidgetPreview
Expand Down
44 changes: 42 additions & 2 deletions orangecontrib/text/widgets/tests/test_owwordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,54 @@ def test_saved_settings(self):
["changed", "word 2", "word 3", "word 4"])

def test_saved_workflow(self):
settings = {"word_list_library": [{"name": "Hello world",
"words": self._word_list_1},
{"name": "Word list 1",
"words": self._word_list_2}]}
widget = self.create_widget(OWWordList, stored_settings=settings)
words = self.get_output(widget.Outputs.words, widget=widget)
self.assertListEqual(list(words.metas[:, 0]), ["foo", "bar", "baz"])
swords = self.get_output(widget.Outputs.selected_words, widget=widget)
self.assertIsNone(swords)

def test_saved_workflow_with_changed_words(self):
settings = {"word_list_library": [{"name": "Hello world",
"words": self._word_list_1},
{"name": "Word list 1",
"words": self._word_list_2}],
"words": ["bar", "foo"]}
widget = self.create_widget(OWWordList, stored_settings=settings)
words = self.get_output(widget.Outputs.words, widget=widget)
self.assertListEqual(list(words.metas[:, 0]), ["bar", "foo"])
swords = self.get_output(widget.Outputs.selected_words, widget=widget)
self.assertIsNone(swords)

def test_saved_workflow_with_selection(self):
settings = {"word_list_library": [{"name": "Hello world",
"words": self._word_list_1},
{"name": "Word list 1",
"words": self._word_list_2}],
"words": ["bar", "foo"],
"selected_words": {"foo"}}
widget = self.create_widget(OWWordList, stored_settings=settings)
words = self.get_output(widget.Outputs.words, widget=widget)
self.assertListEqual(list(words.metas[:, 0]), ["bar", "foo"])
swords = self.get_output(widget.Outputs.selected_words, widget=widget)
self.assertListEqual(list(swords.metas[:, 0]), ["foo"])

def test_saved_workflow_with_input(self):
settings = {"word_list_library": [{"name": "Hello world",
"words": self._word_list_1},
{"name": "Word list 1",
"words": self._word_list_2}],
"words": {"foo"}}
"words": ["bar", "foo"],
"selected_words": {"foo"}}
widget = self.create_widget(OWWordList, stored_settings=settings)
self.send_signal(widget.Inputs.words, self._input_table, widget=widget)
words = self.get_output(widget.Outputs.words, widget=widget)
self.assertListEqual(list(words.metas[:, 0]), ["foo"])
self.assertListEqual(list(words.metas[:, 0]), ["bar", "foo"])
swords = self.get_output(widget.Outputs.selected_words, widget=widget)
self.assertListEqual(list(swords.metas[:, 0]), ["foo"])


if __name__ == "__main__":
Expand Down

0 comments on commit ea34e07

Please sign in to comment.