Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Corpus viewer: warn on bad regex #1095

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions orangecontrib/text/widgets/owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
QSizePolicy,
QSplitter,
QTableView,
QMessageBox,
)
from Orange.data import Variable
from Orange.data.domain import Domain, filter_visible
Expand Down Expand Up @@ -115,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 @@ -132,8 +133,7 @@ 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:
regex = re.compile(search_string.strip("|"), re.IGNORECASE)
if regex.pattern:
for i, text in enumerate(content):
matches += len(regex.findall(text))
state.set_progress_value((i + 1) / len(content) * 100)
Expand Down Expand Up @@ -186,6 +186,10 @@ class DocumentsFilterProxyModel(QSortFilterProxyModel):
__regex = None

def set_filter_string(self, filter_string: str):
try:
re.compile(filter_string.strip("|"), re.IGNORECASE)
except re.error:
return
self.__regex = re.compile(filter_string.strip("|"), re.IGNORECASE)
self.invalidateFilter()

Expand Down Expand Up @@ -313,6 +317,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 @@ -587,18 +594,29 @@ 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()
self.start(
_count_matches,
self.doc_list_model.get_filter_content(),
self.regexp_filter,
)
try:
self.compiled_regex = re.compile(self.regexp_filter.strip("|"), re.IGNORECASE)
self.start(
_count_matches,
self.doc_list_model.get_filter_content(),
self.compiled_regex,
)
except re.error:
self.Error.invalid_regex()
self.compiled_regex = None
self.n_matching = "n/a"
self.n_matches = "n/a"
self.n_tokens = "n/a"
self.n_types = "n/a"

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, "n/a")
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