Package: negate
Negate: A Python module to negate sentencesIntroductionNegate is a Python module that implements rule-based, syntactic sentence
negation in English1.1More languages might be supported in the future.InstallationNegate is available onPyPIand can be
installed using pip:pipinstall-UnegateUsage1. Initializing the NegatorFirst the Negator must be initialized:fromnegateimportNegator# Use default model (en_core_web_md):negator=Negator()By default, negate uses the spaCy modelen_core_web_mdfor 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 modelen_core_web_trf. To use this
model, first install the additional dependencies and then initialize the Negator
passinguse_transformers=True:pipinstall-U"negator[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 sentencesThen, 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 parameterprefer_contractionsis set toTrue(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."2Note that this does not affect other existent verbs in the
sentence that haven't been modified.Behavior upon unsupported sentencesCurrently, negate will not be able to negate certain types of sentences (seeCurrent LimitationsandIrremediable
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 withfail_on_unsupportedset toTrue, 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=[]forsentinsentences:try:negated_sentences.append(negator.negate_sentence(sent))exceptRuntimeError:pass# skip unsupported sentenceCurrent LimitationsNegate 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 justsome, 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."Inversionsare not supported.This mainly affects to questions, e.g., "Didyou go to
the concert?" vs. "Youdidgo 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'thateandlovewinter." 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'thatenorlovewinter."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 amnothungry because Iate."Irremediable CaveatsLanguage 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.AcknowledgementsNegate 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 PublicationsNegate has been used in the following academic papers:Anschütz, M., Lozano, D. M., & Groh, G. (2023, July 26).This is not correct!
Negation-aware Evaluation of Language Generation Systems.arXiv.org.https://arxiv.org/abs/2307.13989