Skip to content

Commit

Permalink
Error handled with widget
Browse files Browse the repository at this point in the history
  • Loading branch information
SanchoSamba committed Jan 20, 2025
1 parent d17bf4f commit c24c4a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
32 changes: 16 additions & 16 deletions orangecontrib/text/widgets/owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
from orangecanvas.gui.utils import disconnected
from orangewidget.utils.listview import ListViewSearch

from PyQt5.QtWidgets import QMessageBox

from orangecontrib.text.corpus import Corpus

HTML = """
Expand Down Expand Up @@ -118,7 +116,7 @@
)


def _count_matches(content: List[str], search_string: str, state: TaskState) -> int:
def _count_matches(content: List[str], regex: re.Pattern, state: TaskState) -> int:
"""
Count number of appears of any terms in search_string in content texts.
Expand All @@ -135,18 +133,11 @@ def _count_matches(content: List[str], search_string: str, state: TaskState) ->
Number of all matches of search_string in all texts in content list
"""
matches = 0
if search_string:
try:
regex = re.compile(search_string.strip("|"), re.IGNORECASE)
except re.error:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Invalid regex")
msg.exec_()
return 0
for i, text in enumerate(content):
matches += len(regex.findall(text))
state.set_progress_value((i + 1) / len(content) * 100)
if regex:

This comment has been minimized.

Copy link
@markotoplak

markotoplak Jan 20, 2025

Member

Why these checks? Doesn't regex always exists and is valid?

This comment has been minimized.

Copy link
@markotoplak

markotoplak Jan 20, 2025

Member

Ah, I see that it is also called when non-valid. But is this filter really optional?

if regex.pattern:
for i, text in enumerate(content):
matches += len(regex.findall(text))
state.set_progress_value((i + 1) / len(content) * 100)
return matches


Expand Down Expand Up @@ -327,6 +318,9 @@ class Outputs:
class Warning(OWWidget.Warning):
no_feats_search = Msg("No features included in search.")
no_feats_display = Msg("No features selected for display.")

class Error(OWWidget.Error):
invalid_regex = Msg("Invalid regular expression.")

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -601,17 +595,23 @@ def regenerate_docs(self) -> List[str]:
return self.corpus.documents_from_features(self.search_features)

def refresh_search(self):
self.Error.invalid_regex.clear()
if self.corpus is not None:
self.doc_list.model().set_filter_string(self.regexp_filter)
if not self.selected_documents:
# when currently selected items are filtered selection is empty
# select first element in the view in that case
self.doc_list.setCurrentIndex(self.doc_list.model().index(0, 0))
self.update_info()
try:
self.compiled_regex = re.compile(self.regexp_filter.strip("|"), re.IGNORECASE)
except re.error:
self.Error.invalid_regex()
self.compiled_regex = None
self.start(
_count_matches,
self.doc_list_model.get_filter_content(),
self.regexp_filter,
self.compiled_regex,
)
self.show_docs()
self.commit.deferred()
Expand Down
14 changes: 14 additions & 0 deletions orangecontrib/text/widgets/tests/test_owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ def test_search(self):
self.wait_until_finished()
self.assertEqual(self.widget.n_matches, 0)

def test_invalid_regex(self):
# Error is shown when invalid regex is entered
self.send_signal(self.widget.Inputs.corpus, self.corpus)
self.widget.regexp_filter = "*"
self.widget.refresh_search()
self.process_events()
self.assertEqual(self.widget.n_matches, 0)
self.assertTrue(self.widget.Error.invalid_regex.is_shown())
# Error is hidden when valid regex is entered
self.widget.regexp_filter = "graph"
self.widget.refresh_search()
self.process_events()
self.assertFalse(self.widget.Error.invalid_regex.is_shown())

def test_highlighting(self):
self.send_signal(self.widget.Inputs.corpus, self.corpus)
# no intersection between filter and selection
Expand Down

0 comments on commit c24c4a5

Please sign in to comment.