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
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion 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 All @@ -35,6 +36,8 @@
from orangecanvas.gui.utils import disconnected
from orangewidget.utils.listview import ListViewSearch

from PyQt5.QtWidgets import QMessageBox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this line (the correct import is already few lines up). We need to import from AnyQt so that the add-on is compatible both with Qt 5 and 6.


from orangecontrib.text.corpus import Corpus

HTML = """
Expand Down Expand Up @@ -133,7 +136,14 @@ def _count_matches(content: List[str], search_string: str, state: TaskState) ->
"""
matches = 0
if search_string:
regex = re.compile(search_string.strip("|"), re.IGNORECASE)
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)
Expand Down Expand Up @@ -186,6 +196,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
Loading