From 83cf9bcc323534236f2b7e36d728ac71e445c200 Mon Sep 17 00:00:00 2001 From: Ziv Yaniv Date: Thu, 5 Nov 2026 15:03:42 -0500 Subject: [PATCH] Switch spell checker. Switching from pyenchant a Python wrapper for the C enchant spellchecker to comment-spell-check (https://github.com/SimpleITK/CommentSpellCheck, https://pypi.org/project/comment-spell-check/) a pure Python package built on top of the pyspellchecker package. --- .circleci/config.yml | 10 ----- .github/workflows/main.yml | 16 +------ .github/workflows/scheduled_or_manual.yml | 16 +------ CONTRIBUTING.md | 4 +- requirements_dev.txt | 2 +- tests/additional_dictionary.txt | 30 ++++++++++++- tests/test_notebooks.py | 52 ++++++++++------------- 7 files changed, 55 insertions(+), 75 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7ff76c05..937bd075 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -65,16 +65,6 @@ jobs: choco install python --version=<< parameters.python-version >> --allow-downgrade -y python -m pip install --upgrade pip fi - - run: - name: Install enchant on non windows systems - command: | - if [ "$RUNNER_OS" == "linux" ]; then - sudo apt-get update - sudo apt-get install enchant-2 - elif [ "$RUNNER_OS" == "macos" ]; then - brew update - brew install enchant - fi - run: name: Install dependencies command: | diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fdfa322c..8e2515af 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -53,17 +53,6 @@ jobs: uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} - # on windows, the pyenchant package includes enchant - - name: Install enchant on non windows systems - shell: bash - run: | - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get update - sudo apt-get install enchant-2 - elif [ "$RUNNER_OS" == "macOS" ]; then - brew update-reset - brew install enchant - fi - name: Install dependencies run: | python -m pip install --upgrade pip @@ -81,10 +70,7 @@ jobs: if: steps.nbl.outputs.any_changed == 'true' env: SIMPLE_ITK_MEMORY_CONSTRAINED_ENVIRONMENT: 1 - run: | # pyenchant has a problem locating libenchant on macOS-ARM64. setting this environment variable resolves the issue - if [ "$RUNNER_OS" == "macOS" ] && [ "${{ matrix.os }}" != "macos-15-intel" ]; then - export PYENCHANT_LIBRARY_PATH=/opt/homebrew/lib/libenchant-2.dylib - fi + run: | test_files="${{ steps.nbl.outputs.all_changed_files }}" # on windows the paths use backslashes, so replace with forward slashes if [ "$RUNNER_OS" == "Windows" ]; then diff --git a/.github/workflows/scheduled_or_manual.yml b/.github/workflows/scheduled_or_manual.yml index ed00dfbb..81d2dae8 100644 --- a/.github/workflows/scheduled_or_manual.yml +++ b/.github/workflows/scheduled_or_manual.yml @@ -53,17 +53,6 @@ jobs: uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} - # on windows, the pyenchant package includes enchant - - name: Install enchant on non windows systems - shell: bash - run: | - if [ "$RUNNER_OS" == "Linux" ]; then - sudo apt-get update - sudo apt-get install enchant-2 - elif [ "$RUNNER_OS" == "macOS" ]; then - brew update-reset - brew install enchant - fi - name: Install dependencies run: | python -m pip install --upgrade pip @@ -78,9 +67,6 @@ jobs: shell: bash env: SIMPLE_ITK_MEMORY_CONSTRAINED_ENVIRONMENT: 1 - run: | # pyenchant has a problem locating libenchant on macOS-ARM64. setting this environment variable resolves the issue - if [ "$RUNNER_OS" == "macOS" ] && [ "${{ matrix.os }}" != "macos-15-intel" ]; then - export PYENCHANT_LIBRARY_PATH=/opt/homebrew/lib/libenchant-2.dylib - fi + run: | pytest -v --tb=short -k "${{matrix.inputs}}" tests/test_notebooks.py::Test_notebooks::test_python_notebook pytest -v --tb=short tests/test_scripts.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9edab2ff..2249cb28 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,9 +16,7 @@ verify it before we merge it into the main repository. We use the [pytest](https://docs.pytest.org) Python testing framework as our test driver for both the Python and R notebooks. Testing relies on several Python packages listed in the [requirements_dev.txt](requirements_dev.txt) file. Please -install these packages. Note that we use the pyenchant -spell checker which is only available by using the pip package manager -(no conda). This spell checker relies on the [enchant](https://rrthomas.github.io/enchant/) library (`brew install enchant`, `sudo apt-get install enchant`). +install these packages. When you add a new notebook, you will need to add it to the relevant list of notebooks found in the [test_notebooks.py](tests/test_notebooks.py). These diff --git a/requirements_dev.txt b/requirements_dev.txt index ed1d4330..38b12879 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -2,6 +2,6 @@ black[jupyter] pytest markdown lxml -pyenchant +comment-spell-check ipython<=8.12 -rPython/requirements.txt diff --git a/tests/additional_dictionary.txt b/tests/additional_dictionary.txt index 8e7c5616..ca578041 100644 --- a/tests/additional_dictionary.txt +++ b/tests/additional_dictionary.txt @@ -1,3 +1,31 @@ + +rm +Assoc +phys +Trans +rel +hist +tuples +cc +resize +Med +Phys +Vol +pp +km +resized +workflows +int +compositing +mp +tuple +workaround +mm +res +div +etc +Multi +multi LabelMapContourOverlay MergeLabelMapFilter's ipympl @@ -21,7 +49,7 @@ BSplineTransform BSplineTransformInitializer BSplineTransformInitializerFilter Baum -Bérard +Berard Biancardi Bimodal BinaryMorphologicalClosing diff --git a/tests/test_notebooks.py b/tests/test_notebooks.py index 06420169..e231ab57 100755 --- a/tests/test_notebooks.py +++ b/tests/test_notebooks.py @@ -8,9 +8,9 @@ import re import urllib.request -from enchant.checker import SpellChecker -from enchant.tokenize import Filter, EmailFilter, URLFilter -from enchant import DictWithPWL +from comment_parser.parsers.common import Comment +from comment_spell_check.comment_spell_check import spell_check_comment +from comment_spell_check.utils.create_checker import create_checker from lxml.html import document_fromstring, etree from urllib.request import urlopen, URLError, Request @@ -250,44 +250,36 @@ def static_analysis(self, path): print("no broken links") ####################### - # Spell check all markdown cells and comments in code cells using the pyenchant spell checker. + # Spell check all markdown cells and comments in code cells using the + # CommentSpellCheck package. ####################### no_spelling_mistakes = True - simpleitk_notebooks_dictionary = DictWithPWL( - "en_US", - os.path.join( - os.path.dirname(os.path.abspath(__file__)), "additional_dictionary.txt" - ), - ) - spell_checker = SpellChecker( - simpleitk_notebooks_dictionary, filters=[EmailFilter, URLFilter] + spell_checker = create_checker( + [ + os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "additional_dictionary.txt", + ) + ] ) + prefixes = ["sitk", "itk", "vtk"] cells_and_spelling_mistakes = [] for c in nb.cells: - spelling_mistakes = [] if c.cell_type == "markdown": # Get the text as a string from the html without the markup which is replaced by space. - spell_checker.set_text( - " ".join( - etree.XPath("//text()")( - document_fromstring(markdown.markdown(c.source)) - ) + text = " ".join( + etree.XPath("//text()")( + document_fromstring(markdown.markdown(c.source)) ) ) elif c.cell_type == "code": # Get all the comments and concatenate them into a single string separated by newlines. - comment_lines = re.findall("#+.*", c.source) - spell_checker.set_text("\n".join(comment_lines)) - for error in spell_checker: - error_message = ( - "error: " - + "'" - + error.word - + "', " - + "suggestions: " - + str(spell_checker.suggest()) - ) - spelling_mistakes.append(error_message) + text = "\n".join(re.findall("#+.*", c.source)) + else: + continue + # Line number is irrelevant, the whole cell's text is checked as one unit. + mistakes = spell_check_comment(spell_checker, Comment(text, 1), prefixes=prefixes) + spelling_mistakes = ["error: " + mistake for mistake in mistakes] if spelling_mistakes: cells_and_spelling_mistakes.append((spelling_mistakes, c.source)) if cells_and_spelling_mistakes: