-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d17bf4f
commit c24c4a5
Showing
2 changed files
with
30 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = """ | ||
|
@@ -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. | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
markotoplak
Member
|
||
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 | ||
|
||
|
||
|
@@ -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__() | ||
|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why these checks? Doesn't regex always exists and is valid?