From 2e14f81d66514dbf5a31643df9a16498a94ea3f1 Mon Sep 17 00:00:00 2001 From: Diego Miguel Date: Wed, 25 Oct 2023 17:49:28 +0200 Subject: [PATCH 1/5] Add tests for failing sentence --- tests/data.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/data.py b/tests/data.py index 33a555a..e0f0472 100644 --- a/tests/data.py +++ b/tests/data.py @@ -233,7 +233,9 @@ ("I really liked the food.", "I really didn't like the food.", True), ("I really liked the food.", "I really did not like the food.", False), ("A small Python module negating sentences.", "A small Python module not negating sentences.", False), - ("A small Python module negating sentences.", "A small Python module not negating sentences.", True) + ("A small Python module negating sentences.", "A small Python module not negating sentences.", True), + ("A small Python module to negate sentences.", "A small Python module to not negate sentences.", False), + ("A small Python module to negate sentences.", "A small Python module to not negate sentences.", True) ] # General verbs - Negative @@ -247,7 +249,9 @@ ("She does not think it's true.", "She thinks it's true.", True), ("She does not think it's true.", "She thinks it's true.", False), ("A small Python module not negating sentences.", "A small Python module negating sentences.", False), - ("A small Python module not negating sentences.", "A small Python module negating sentences.", True) + ("A small Python module not negating sentences.", "A small Python module negating sentences.", True), + ("A small Python module to not negate sentences.", "A small Python module to negate sentences.", False), + ("A small Python module to not negate sentences.", "A small Python module to negate sentences.", True) ] # Inversions - Affirmative From 57f47e5e19594a1b9a1c74e2486de07e75f7d32b Mon Sep 17 00:00:00 2001 From: Diego Miguel Date: Wed, 25 Oct 2023 17:49:42 +0200 Subject: [PATCH 2/5] Fix #9 --- negate/negate.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/negate/negate.py b/negate/negate.py index c37bfa2..e80e69c 100644 --- a/negate/negate.py +++ b/negate/negate.py @@ -126,12 +126,12 @@ def negate_sentence( # complicates things. first_aux_or_verb = self._get_first_aux_or_verb(doc) while (negation and first_aux_or_verb - and first_aux_or_verb.tag_ != "VBG" + and first_aux_or_verb.tag_ not in("VB", "VBG") and negation.i < first_aux_or_verb.i): # Search for another negation, if any. negation = self._get_negated_child(root, min_index=negation.i+1) + aux_child = self._get_aux_child(root) if negation: - aux_child = self._get_aux_child(root) remove, add = self._handle_ca_wo(root, aux_child, negation=negation) # General verbs -> Remove negation and conjugate verb. # If there is an AUX child, we need to "unnegate" the AUX instead. @@ -188,7 +188,6 @@ def negate_sentence( or any(self._is_aux(child) for child in root.children)): # If the AUX has AUX children, negate them instead. E.g.: "I will # be there." or "They have been moody lately." - aux_child = self._get_aux_child(root) try: return self._negate_aux_in_doc( aux=root if not aux_child else aux_child, @@ -203,7 +202,10 @@ def negate_sentence( pass # General verb non-negated. - if root.tag_ == "VBG": # E.g.: "A Python module negating sentences." + if (any(child.tag_ == "TO" for child in root.children) + or root.tag_ == "VBG"): + # E.g.: "A Python module negating sentences." or "A Python module + # to negate sentences." add = f"not {root.text}" else: negated_aux = self.negate_aux(self.conjugate_verb('do', root.tag_), From c1a4524cdc59e26d4a61e4bd84da15884647dbb6 Mon Sep 17 00:00:00 2001 From: Diego Miguel Date: Wed, 25 Oct 2023 18:43:33 +0200 Subject: [PATCH 3/5] Update `requirements.txt` --- tests/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index 18957c9..23a1da3 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,2 +1,3 @@ pytest>=7.2.0,<7.5.0 -negate[transformers]>=0.7.9,<1.5.0 +negate>=0.7.9,<1.5.0 +spacy-transformers>=1.2.5,<1.4.0 From 780dae0664951dc6acaa167191ce7d41a0340990 Mon Sep 17 00:00:00 2001 From: Diego Miguel Date: Wed, 25 Oct 2023 19:16:58 +0200 Subject: [PATCH 4/5] Fix texts to run with GitHub Actions --- .github/workflows/run-tests.yml | 2 +- tests/conftest.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b2a667c..698936b 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -31,4 +31,4 @@ jobs: run: pytest tests - name: Run tests with Transformers - run: pytest tests --transformers + run: pytest tests --transformers --use-cpu diff --git a/tests/conftest.py b/tests/conftest.py index 0a12f1c..507b9d3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ """Set up tests.""" from negate import Negator +from contextlib import suppress # Reuse negator model for all tests. @@ -10,6 +11,8 @@ def pytest_addoption(parser): parser.addoption( "--transformers", action="store_true", help="use Transformers") + parser.addoption( + "--use-cpu", action="store_true", help="use CPU instead of GPU") def pytest_generate_tests(metafunc): @@ -18,12 +21,14 @@ def pytest_generate_tests(metafunc): global negator_model if negator_model is None: use_transformers = metafunc.config.getoption("transformers") - try: - # `use_gpu` ignored if `use_transformers` is False. - negator_model = Negator( - use_transformers=use_transformers, use_gpu=True) - negator_model.negate_sentence("I will now check GPU support!") - except (ValueError, NotImplementedError): # GPU not supported + if not metafunc.config.getoption("use_cpu"): + with suppress(ValueError, NotImplementedError): + # `use_gpu` ignored if `use_transformers` is False. + negator_model = Negator(use_transformers=use_transformers, + use_gpu=True) + # If GPU is unsupported, we fallback to CPU. + negator_model.negate_sentence("I will now check GPU support!") + else: negator_model = Negator( use_transformers=use_transformers, use_gpu=False) metafunc.parametrize("negator", [negator_model]) From df654c25e2b10664020b065c5f68e3ddfe419c29 Mon Sep 17 00:00:00 2001 From: Diego Miguel Date: Wed, 25 Oct 2023 22:58:57 +0200 Subject: [PATCH 5/5] Disable automatic tests with Transformers --- .github/workflows/run-tests.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 698936b..643f4c7 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -29,6 +29,3 @@ jobs: - name: Run tests without Transformers run: pytest tests - - - name: Run tests with Transformers - run: pytest tests --transformers --use-cpu