Skip to main content

A Python module that negates sentences.

Project description

Negate Logo

Negate: A Python module to negate sentences

Active Issues



Introduction

Negate is a Python module that implements rule-based, syntactic sentence negation in English1.

1 More languages might be supported in the future.


Installation

Negate is available on PyPI and can be installed using pip:

pip install -U negate

Usage

1. Initializing the Negator

First the Negator must be initialized:

from negate import Negator

# Use default model (en_core_web_md):
negator = Negator()

By default, negate uses the spaCy model en_core_web_md for POS tagging and dependency parsing. This model works well for most cases. However, if maximum accuracy is needed, negate also allows to use a Transformer model, namely the spaCy model en_core_web_trf. To use this model, first install the additional dependencies and then initialize the Negator passing use_transformers=True:

pip install -U "negate[transformers]"
# Use a Transformer model (en_core_web_trf):
negator = Negator(use_transformers=True)

# GPU can also be used (if available):
negator = Negator(use_transformers=True, use_gpu=True)

2. Negating sentences

Then, to negate a sentence:

sentence = "An apple a day, keeps the doctor away."

negated_sentence = negator.negate_sentence(sentence)

print(negated_sentence)  # "An apple a day, doesn't keep the doctor away."

When the parameter prefer_contractions is set to True (default), modifications to auxiliary verbs will use their contracted form2. For example:

sentence = "Speaking of doctors, I went to the doctor the other day."

negated_sentence = negator.negate_sentence(sentence, prefer_contractions=True)
print(negated_sentence)  # "Speaking of doctors, I didn't go to the doctor the other day."

negated_sentence = negator.negate_sentence(sentence, prefer_contractions=False)
print(negated_sentence)  # "Speaking of doctors, I did not go to the doctor the other day."

2 Note that this does not affect other existent verbs in the sentence that haven't been modified.


Behavior upon unsupported sentences

Currently, negate will not be able to negate certain types of sentences (see Current Limitations and Irremediable Caveats).

In some cases, negate will detect that a sentence is not supported. By default, a warning will be issued:

Negator - WARNING: Sentence not supported. Output might be arbitrary.

If you want the negator to fail instead of printing a warning, simply initialize it with fail_on_unsupported set to True, i.e.:

negator = Negator(fail_on_unsupported=True)

This can be useful to skip unsupported sentences when running negate on a batch of sentences, e.g.:

negator = Negator(fail_on_unsupported=True)
sentences = [...]
negated_sentences = []

for sent in sentences:
    try:
        negated_sentences.append(negator.negate_sentence(sent))
    except RuntimeError:
        pass  # skip unsupported sentence

Current Limitations

Negate should work fine for most cases. However, it is currently in beta phase. Some features have not yet been implemented. Pull Requests are always welcome!

  • "Some", "any", "yet" are not properly supported. E.g.: Negating the sentence "There are some features to be implemented." will currently output "There aren't some features to be implemented." Although this could still make sense depending on the context (e.g., "There aren't some features to be implemented. No, not just some, there are a lot!"), I assume most users would expect "some" being replaced with "any" and vice versa. When it comes to "yet", when negating a negation, it makes sense to remove it or replace it with "already", e.g., "I haven't been to Paris yet." → "I have been to Paris."

  • Inversions are not supported. This mainly affects to questions, e.g., "Did you go to the concert?" vs. "You did go to the concert." Notice how in the first example (interrogative) we have AUX + PRON + VERB and in the second (affirmative) PRON + AUX + VERB. Update: Inversions are now supported!

  • Non-verbal negations are not supported. This type of negations, such as "A bottle with no cap." will produce the warning: Negator - WARNING: Sentence not supported. Output might be arbitrary.

  • The auxiliary "ought" is not supported. "Ought" is the only auxiliary followed by a "to." This complicates things slightly. But yeah, it ought to be implemented at some point.

  • Certain verb conjunctions are not supported. E.g.: "She hates and loves winter." → "She doesn't hate and love winter." Currently, only the first verb will be correctly conjugated. In this cases, it would also make sense to attend to boolean algebra (De Morgan's law) and replace the "and" with "or"/ "neither"/"nor", i.e., "She doesn't hate nor love winter."

  • Multiple verb negation is not supported. In many sentences with subordinate clauses, it would make sense to negate several verbs. E.g.: "I am hungry because I didn't eat." → "I am not hungry because I ate."


Irremediable Caveats

Language took very seriously Bruce Lee's famous words "be water, my friend." It is extremely flexible, and therefore, no number of rules, however large this number may be, will cover the whole realm of possibilities. Just when you think your rules cover most of the cases, a new one comes in that breaks things. Early NLP researchers and developers know very well about this.

Negate has no notion of meaning neither will ever do. Its scope its limited to syntax. Because of this, in some cases, the produced negated sentences might sound rather off.

Negate depends 100 % on POS tagging and dependency parsing. If any of them fails, negate will also fail. The spaCy models we use are not infallible, which adds another layer of "things that could go wrong" to negate.

This module was in fact developed to generate negation data in order to fine-tune NLP Deep Learning models (yes, Transformers – can't believe I made it this long without mentioning the word). They are, of course, the way to go for a fully-fledged negation that also attends to semantics.


Acknowledgements

Negate has two core direct dependencies. Without them, negate wouldn't be able to exist:

  • spaCy 💫: As already mentioned, we rely on POS tagging and dependency parsing to negate sentences. spaCy makes this process very easy for us.

  • LemmInflect 🍋: Negations go far beyond adding or removing a negation particle. In some cases, verbs have to be properly conjugated (e.g., when negating verbs in third-person or in past simple). LemmInflect provides us with this functionality.


Academic Publications

Negate has been used in the following academic papers:

  • Anschütz, M., Miguel Lozano, D., & Groh, G. (2023). This is not correct! Negation-aware Evaluation of Language Generation Systems. Proceedings of the 16th International Natural Language Generation Conference, 163–175. https://doi.org/10.18653/v1/2023.inlg-main.12

  • Liu, X., Feng, Y., & Chang, K.-W. (2024). Casa: Causality-driven Argument Sufficiency Assessment. Proceedings of the 2024 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers), 5282–5302. https://doi.org/10.18653/v1/2024.naacl-long.296

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

negate-1.1.6.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

negate-1.1.6-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file negate-1.1.6.tar.gz.

File metadata

  • Download URL: negate-1.1.6.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for negate-1.1.6.tar.gz
Algorithm Hash digest
SHA256 c09d27cd406db4b4866abf30939de0108bf2ca7b4fa000262445689c709821b7
MD5 978d2d116d311b5459d253a217b0a894
BLAKE2b-256 4c3185f8138a5555b272874a4f5ee79a6e04477604e2140e8976edaff23e2c1f

See more details on using hashes here.

File details

Details for the file negate-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: negate-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for negate-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f0df8b6b1098f2c5bf863b75c24406108d8c5ba21cff82b4f7d98ed737e69308
MD5 c019519f179124d9e4a1c2e43c31168b
BLAKE2b-256 768b9f8e06f0b55b790a2ac047d446a29271a8808d617442bf80a67b236b0163

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page