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 #9 #10

Merged
merged 5 commits into from
Oct 25, 2023
Merged
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
3 changes: 0 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ jobs:

- name: Run tests without Transformers
run: pytest tests

- name: Run tests with Transformers
run: pytest tests --transformers
10 changes: 6 additions & 4 deletions negate/negate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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_),
Expand Down
17 changes: 11 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Set up tests."""

from negate import Negator
from contextlib import suppress


# Reuse negator model for all tests.
Expand All @@ -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):
Expand All @@ -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])
8 changes: 6 additions & 2 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -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