From d2e2deae2cc1e9adbb69a3433e6b934c489270a7 Mon Sep 17 00:00:00 2001 From: mayukh Date: Tue, 31 Aug 2021 23:50:25 +0200 Subject: [PATCH 1/8] Added NegatedAntonym Transformation --- requirements.txt | 1 + transformations/NegatedAntonym/README.md | 16 +++++ transformations/NegatedAntonym/__init__.py | 1 + transformations/NegatedAntonym/test.json | 68 +++++++++++++++++++ .../NegatedAntonym/transformation.py | 40 +++++++++++ 5 files changed, 126 insertions(+) create mode 100644 transformations/NegatedAntonym/README.md create mode 100644 transformations/NegatedAntonym/__init__.py create mode 100644 transformations/NegatedAntonym/test.json create mode 100644 transformations/NegatedAntonym/transformation.py diff --git a/requirements.txt b/requirements.txt index 3415e3843..9c32b3c2b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ checklist==0.0.11 spacy==3.0.0 numpy +nltk 3.6.2 # for back_translation torch diff --git a/transformations/NegatedAntonym/README.md b/transformations/NegatedAntonym/README.md new file mode 100644 index 000000000..4f3fd8aeb --- /dev/null +++ b/transformations/NegatedAntonym/README.md @@ -0,0 +1,16 @@ +# Negated Antonym Perturbation +This perturbation rephrases the adjectives and adverbs to their negated antonym + +Authors: Mayukh Das (Technical University of Braunschweig / mayukh.das@tu-bs.de) + +## What type of a transformation is this? +This transformation detects all type of adjectives and adverb and converts them to their negated antonym. +Therfore the transformation retains the semantics of the original text as positives gets converted to negated negatives and negatives gets converted to negated positives. +Example: I think you are prepared for the test --> I think you are not unprepared for the test +## What tasks does it intend to benefit? +This can act as a valid perturbation that retains the semantics. +This will benefit for robustness of text classification like sentiment analysis, etc. + + +## What are the limitations of this transformation? +It is limited to only adjectives and adverbs (comparative and superlative) \ No newline at end of file diff --git a/transformations/NegatedAntonym/__init__.py b/transformations/NegatedAntonym/__init__.py new file mode 100644 index 000000000..930cdce0b --- /dev/null +++ b/transformations/NegatedAntonym/__init__.py @@ -0,0 +1 @@ +from .transformation import * diff --git a/transformations/NegatedAntonym/test.json b/transformations/NegatedAntonym/test.json new file mode 100644 index 000000000..4875c3136 --- /dev/null +++ b/transformations/NegatedAntonym/test.json @@ -0,0 +1,68 @@ +{ + "type": "negated_antonym_transformation", + "test_cases": [ + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are successful." + }, + "outputs": [{ + "sentence": "I think you are not unsuccessful." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are prepared for the test." + }, + "outputs": [{ + "sentence": "I think you are not unprepared for the test." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "He ran quickly." + }, + "outputs": [{ + "sentence": "He ran not slowly." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "He plays the flute beautifully." + }, + "outputs": [{ + "sentence": "He plays the flute not unattractively." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are joyful." + }, + "outputs": [{ + "sentence": "I think you are not sorrowful." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are smarter than me." + }, + "outputs": [{ + "sentence": "I think you are not stupid than me." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "The movie was bad." + }, + "outputs": [{ + "sentence": "The movie was not good." + }] + } + ] +} \ No newline at end of file diff --git a/transformations/NegatedAntonym/transformation.py b/transformations/NegatedAntonym/transformation.py new file mode 100644 index 000000000..b419bec81 --- /dev/null +++ b/transformations/NegatedAntonym/transformation.py @@ -0,0 +1,40 @@ +import nltk +from nltk.corpus import wordnet +import nltk.tokenize as nt + +from interfaces.SentenceOperation import SentenceOperation +from tasks.TaskTypes import TaskType + +''' Class that converts adjectives and adverbs to its negated antonym''' + + +class NegatedAntonym(SentenceOperation): + tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] + languages = ["en"] + + def __init__(self, seed=0, max_outputs=1): + super().__init__(seed, max_outputs=max_outputs) + + def generate(self, sen): + return [self.Neg_Antonym(sen)] + + def Neg_Antonym(self, sentence): + + tokenized_sent = nt.word_tokenize(sentence) + pos_sentences = nltk.pos_tag(tokenized_sent) + + for i in range(len(pos_sentences)): + antonyms = [] + if pos_sentences[i][1] == 'JJ' or pos_sentences[i][1] == 'JJR' or pos_sentences[i][1] == 'JJS' or \ + pos_sentences[i][1] == 'RB' or pos_sentences[i][1] == 'RBR' or pos_sentences[i][1] == 'RBS': + for syn in wordnet.synsets(tokenized_sent[i]): + + for lm in syn.lemmas(): + + if lm.antonyms(): + antonyms.append(lm.antonyms()[0].name()) + + if len(antonyms) != 0: + tokenized_sent[i] = 'not ' + antonyms[0] + + return ' '.join(tokenized_sent) From 19765fe242156abb263d65500590ecbdc22af7d4 Mon Sep 17 00:00:00 2001 From: mayukh Date: Tue, 31 Aug 2021 23:55:56 +0200 Subject: [PATCH 2/8] Added negated_antonym_perturbation transformation --- .../negated_antonym_perturbation/README.md | 16 +++++ .../negated_antonym_perturbation/__init__.py | 1 + .../negated_antonym_perturbation/test.json | 68 +++++++++++++++++++ .../transformation.py | 40 +++++++++++ 4 files changed, 125 insertions(+) create mode 100644 transformations/negated_antonym_perturbation/README.md create mode 100644 transformations/negated_antonym_perturbation/__init__.py create mode 100644 transformations/negated_antonym_perturbation/test.json create mode 100644 transformations/negated_antonym_perturbation/transformation.py diff --git a/transformations/negated_antonym_perturbation/README.md b/transformations/negated_antonym_perturbation/README.md new file mode 100644 index 000000000..4f3fd8aeb --- /dev/null +++ b/transformations/negated_antonym_perturbation/README.md @@ -0,0 +1,16 @@ +# Negated Antonym Perturbation +This perturbation rephrases the adjectives and adverbs to their negated antonym + +Authors: Mayukh Das (Technical University of Braunschweig / mayukh.das@tu-bs.de) + +## What type of a transformation is this? +This transformation detects all type of adjectives and adverb and converts them to their negated antonym. +Therfore the transformation retains the semantics of the original text as positives gets converted to negated negatives and negatives gets converted to negated positives. +Example: I think you are prepared for the test --> I think you are not unprepared for the test +## What tasks does it intend to benefit? +This can act as a valid perturbation that retains the semantics. +This will benefit for robustness of text classification like sentiment analysis, etc. + + +## What are the limitations of this transformation? +It is limited to only adjectives and adverbs (comparative and superlative) \ No newline at end of file diff --git a/transformations/negated_antonym_perturbation/__init__.py b/transformations/negated_antonym_perturbation/__init__.py new file mode 100644 index 000000000..930cdce0b --- /dev/null +++ b/transformations/negated_antonym_perturbation/__init__.py @@ -0,0 +1 @@ +from .transformation import * diff --git a/transformations/negated_antonym_perturbation/test.json b/transformations/negated_antonym_perturbation/test.json new file mode 100644 index 000000000..4875c3136 --- /dev/null +++ b/transformations/negated_antonym_perturbation/test.json @@ -0,0 +1,68 @@ +{ + "type": "negated_antonym_transformation", + "test_cases": [ + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are successful." + }, + "outputs": [{ + "sentence": "I think you are not unsuccessful." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are prepared for the test." + }, + "outputs": [{ + "sentence": "I think you are not unprepared for the test." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "He ran quickly." + }, + "outputs": [{ + "sentence": "He ran not slowly." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "He plays the flute beautifully." + }, + "outputs": [{ + "sentence": "He plays the flute not unattractively." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are joyful." + }, + "outputs": [{ + "sentence": "I think you are not sorrowful." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "I think you are smarter than me." + }, + "outputs": [{ + "sentence": "I think you are not stupid than me." + }] + }, + { + "class": "NegatedAntonym", + "inputs": { + "sentence": "The movie was bad." + }, + "outputs": [{ + "sentence": "The movie was not good." + }] + } + ] +} \ No newline at end of file diff --git a/transformations/negated_antonym_perturbation/transformation.py b/transformations/negated_antonym_perturbation/transformation.py new file mode 100644 index 000000000..b419bec81 --- /dev/null +++ b/transformations/negated_antonym_perturbation/transformation.py @@ -0,0 +1,40 @@ +import nltk +from nltk.corpus import wordnet +import nltk.tokenize as nt + +from interfaces.SentenceOperation import SentenceOperation +from tasks.TaskTypes import TaskType + +''' Class that converts adjectives and adverbs to its negated antonym''' + + +class NegatedAntonym(SentenceOperation): + tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] + languages = ["en"] + + def __init__(self, seed=0, max_outputs=1): + super().__init__(seed, max_outputs=max_outputs) + + def generate(self, sen): + return [self.Neg_Antonym(sen)] + + def Neg_Antonym(self, sentence): + + tokenized_sent = nt.word_tokenize(sentence) + pos_sentences = nltk.pos_tag(tokenized_sent) + + for i in range(len(pos_sentences)): + antonyms = [] + if pos_sentences[i][1] == 'JJ' or pos_sentences[i][1] == 'JJR' or pos_sentences[i][1] == 'JJS' or \ + pos_sentences[i][1] == 'RB' or pos_sentences[i][1] == 'RBR' or pos_sentences[i][1] == 'RBS': + for syn in wordnet.synsets(tokenized_sent[i]): + + for lm in syn.lemmas(): + + if lm.antonyms(): + antonyms.append(lm.antonyms()[0].name()) + + if len(antonyms) != 0: + tokenized_sent[i] = 'not ' + antonyms[0] + + return ' '.join(tokenized_sent) From a6de4b3d28e56a8c27681fae2f506257e799c00e Mon Sep 17 00:00:00 2001 From: mayukh Date: Thu, 2 Sep 2021 21:42:56 +0200 Subject: [PATCH 3/8] Fixed requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9c32b3c2b..b2eeadd16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ checklist==0.0.11 spacy==3.0.0 numpy -nltk 3.6.2 + # for back_translation torch From 55bb3bfacb0bd478d6c05bf947716d06c98c62c2 Mon Sep 17 00:00:00 2001 From: mayukh Date: Wed, 8 Sep 2021 17:15:11 +0200 Subject: [PATCH 4/8] Resolved maxoutput --- transformations/NegatedAntonym/README.md | 16 ----- transformations/NegatedAntonym/__init__.py | 1 - transformations/NegatedAntonym/test.json | 68 ------------------- .../NegatedAntonym/transformation.py | 40 ----------- .../transformation.py | 8 +-- 5 files changed, 4 insertions(+), 129 deletions(-) delete mode 100644 transformations/NegatedAntonym/README.md delete mode 100644 transformations/NegatedAntonym/__init__.py delete mode 100644 transformations/NegatedAntonym/test.json delete mode 100644 transformations/NegatedAntonym/transformation.py diff --git a/transformations/NegatedAntonym/README.md b/transformations/NegatedAntonym/README.md deleted file mode 100644 index 4f3fd8aeb..000000000 --- a/transformations/NegatedAntonym/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Negated Antonym Perturbation -This perturbation rephrases the adjectives and adverbs to their negated antonym - -Authors: Mayukh Das (Technical University of Braunschweig / mayukh.das@tu-bs.de) - -## What type of a transformation is this? -This transformation detects all type of adjectives and adverb and converts them to their negated antonym. -Therfore the transformation retains the semantics of the original text as positives gets converted to negated negatives and negatives gets converted to negated positives. -Example: I think you are prepared for the test --> I think you are not unprepared for the test -## What tasks does it intend to benefit? -This can act as a valid perturbation that retains the semantics. -This will benefit for robustness of text classification like sentiment analysis, etc. - - -## What are the limitations of this transformation? -It is limited to only adjectives and adverbs (comparative and superlative) \ No newline at end of file diff --git a/transformations/NegatedAntonym/__init__.py b/transformations/NegatedAntonym/__init__.py deleted file mode 100644 index 930cdce0b..000000000 --- a/transformations/NegatedAntonym/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .transformation import * diff --git a/transformations/NegatedAntonym/test.json b/transformations/NegatedAntonym/test.json deleted file mode 100644 index 4875c3136..000000000 --- a/transformations/NegatedAntonym/test.json +++ /dev/null @@ -1,68 +0,0 @@ -{ - "type": "negated_antonym_transformation", - "test_cases": [ - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "I think you are successful." - }, - "outputs": [{ - "sentence": "I think you are not unsuccessful." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "I think you are prepared for the test." - }, - "outputs": [{ - "sentence": "I think you are not unprepared for the test." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "He ran quickly." - }, - "outputs": [{ - "sentence": "He ran not slowly." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "He plays the flute beautifully." - }, - "outputs": [{ - "sentence": "He plays the flute not unattractively." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "I think you are joyful." - }, - "outputs": [{ - "sentence": "I think you are not sorrowful." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "I think you are smarter than me." - }, - "outputs": [{ - "sentence": "I think you are not stupid than me." - }] - }, - { - "class": "NegatedAntonym", - "inputs": { - "sentence": "The movie was bad." - }, - "outputs": [{ - "sentence": "The movie was not good." - }] - } - ] -} \ No newline at end of file diff --git a/transformations/NegatedAntonym/transformation.py b/transformations/NegatedAntonym/transformation.py deleted file mode 100644 index b419bec81..000000000 --- a/transformations/NegatedAntonym/transformation.py +++ /dev/null @@ -1,40 +0,0 @@ -import nltk -from nltk.corpus import wordnet -import nltk.tokenize as nt - -from interfaces.SentenceOperation import SentenceOperation -from tasks.TaskTypes import TaskType - -''' Class that converts adjectives and adverbs to its negated antonym''' - - -class NegatedAntonym(SentenceOperation): - tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] - languages = ["en"] - - def __init__(self, seed=0, max_outputs=1): - super().__init__(seed, max_outputs=max_outputs) - - def generate(self, sen): - return [self.Neg_Antonym(sen)] - - def Neg_Antonym(self, sentence): - - tokenized_sent = nt.word_tokenize(sentence) - pos_sentences = nltk.pos_tag(tokenized_sent) - - for i in range(len(pos_sentences)): - antonyms = [] - if pos_sentences[i][1] == 'JJ' or pos_sentences[i][1] == 'JJR' or pos_sentences[i][1] == 'JJS' or \ - pos_sentences[i][1] == 'RB' or pos_sentences[i][1] == 'RBR' or pos_sentences[i][1] == 'RBS': - for syn in wordnet.synsets(tokenized_sent[i]): - - for lm in syn.lemmas(): - - if lm.antonyms(): - antonyms.append(lm.antonyms()[0].name()) - - if len(antonyms) != 0: - tokenized_sent[i] = 'not ' + antonyms[0] - - return ' '.join(tokenized_sent) diff --git a/transformations/negated_antonym_perturbation/transformation.py b/transformations/negated_antonym_perturbation/transformation.py index b419bec81..a8222deab 100644 --- a/transformations/negated_antonym_perturbation/transformation.py +++ b/transformations/negated_antonym_perturbation/transformation.py @@ -12,11 +12,11 @@ class NegatedAntonym(SentenceOperation): tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] languages = ["en"] - def __init__(self, seed=0, max_outputs=1): - super().__init__(seed, max_outputs=max_outputs) + def __init__(self, seed=0): + super().__init__(seed) - def generate(self, sen): - return [self.Neg_Antonym(sen)] + def generate(self, sentence): + return [self.Neg_Antonym(sentence)] def Neg_Antonym(self, sentence): From e1e907b60463b8cfc665e87c4e0a2ee573350910 Mon Sep 17 00:00:00 2001 From: mayukh Date: Wed, 15 Sep 2021 03:31:10 +0200 Subject: [PATCH 5/8] Added Robustness and Keyword --- .../negated_antonym_perturbation/README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/transformations/negated_antonym_perturbation/README.md b/transformations/negated_antonym_perturbation/README.md index 4f3fd8aeb..dc8e6d426 100644 --- a/transformations/negated_antonym_perturbation/README.md +++ b/transformations/negated_antonym_perturbation/README.md @@ -13,4 +13,18 @@ This will benefit for robustness of text classification like sentiment analysis, ## What are the limitations of this transformation? -It is limited to only adjectives and adverbs (comparative and superlative) \ No newline at end of file +It is limited to only adjectives and adverbs (comparative and superlative) + +## Robustness Evaluation + +original accuracy: 96.0 +dataset_name: 'imdb' +model_name: 'aychang/roberta-base-imdb' +no_of_examples: 250 +accuracy after perturbation: 91.0 + +The accuracy drops by 5% when tested on imdb dataset for roberta + +## KeyWords +tokenizer-required +highly-meaning-preserving From a9afa1fd33fb8a8c139a4f758ebf2a7e7a56af1a Mon Sep 17 00:00:00 2001 From: mayukh Date: Wed, 15 Sep 2021 04:07:36 +0200 Subject: [PATCH 6/8] fixed test --- .../negated_antonym_perturbation/test.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/transformations/negated_antonym_perturbation/test.json b/transformations/negated_antonym_perturbation/test.json index 4875c3136..a44a9f839 100644 --- a/transformations/negated_antonym_perturbation/test.json +++ b/transformations/negated_antonym_perturbation/test.json @@ -7,7 +7,7 @@ "sentence": "I think you are successful." }, "outputs": [{ - "sentence": "I think you are not unsuccessful." + "sentence": "I think you are not unsuccessful ." }] }, { @@ -16,7 +16,7 @@ "sentence": "I think you are prepared for the test." }, "outputs": [{ - "sentence": "I think you are not unprepared for the test." + "sentence": "I think you are not unprepared for the test ." }] }, { @@ -25,7 +25,7 @@ "sentence": "He ran quickly." }, "outputs": [{ - "sentence": "He ran not slowly." + "sentence": "He ran not slowly ." }] }, { @@ -34,7 +34,7 @@ "sentence": "He plays the flute beautifully." }, "outputs": [{ - "sentence": "He plays the flute not unattractively." + "sentence": "He plays the flute not unattractively ." }] }, { @@ -43,7 +43,7 @@ "sentence": "I think you are joyful." }, "outputs": [{ - "sentence": "I think you are not sorrowful." + "sentence": "I think you are not sorrowful ." }] }, { @@ -52,7 +52,7 @@ "sentence": "I think you are smarter than me." }, "outputs": [{ - "sentence": "I think you are not stupid than me." + "sentence": "I think you are not stupid than me ." }] }, { @@ -61,7 +61,7 @@ "sentence": "The movie was bad." }, "outputs": [{ - "sentence": "The movie was not good." + "sentence": "The movie was not good ." }] } ] From f63287dedb3b862f04166a419409d8c6e523a02c Mon Sep 17 00:00:00 2001 From: mayukh Date: Fri, 8 Oct 2021 15:14:49 +0200 Subject: [PATCH 7/8] Keyword Corrected --- transformations/negated_antonym_perturbation/README.md | 3 --- transformations/negated_antonym_perturbation/transformation.py | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/transformations/negated_antonym_perturbation/README.md b/transformations/negated_antonym_perturbation/README.md index dc8e6d426..ba4e9d81d 100644 --- a/transformations/negated_antonym_perturbation/README.md +++ b/transformations/negated_antonym_perturbation/README.md @@ -25,6 +25,3 @@ accuracy after perturbation: 91.0 The accuracy drops by 5% when tested on imdb dataset for roberta -## KeyWords -tokenizer-required -highly-meaning-preserving diff --git a/transformations/negated_antonym_perturbation/transformation.py b/transformations/negated_antonym_perturbation/transformation.py index a8222deab..55418c98b 100644 --- a/transformations/negated_antonym_perturbation/transformation.py +++ b/transformations/negated_antonym_perturbation/transformation.py @@ -11,6 +11,8 @@ class NegatedAntonym(SentenceOperation): tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] languages = ["en"] + keywords = ["sentiment-analysis", "tokenizer-required", "highly-meaning-preserving"] + def __init__(self, seed=0): super().__init__(seed) From 05d311c150ca04a1e7c6a7711c866639243bba6a Mon Sep 17 00:00:00 2001 From: mayukh Date: Mon, 11 Oct 2021 17:37:34 +0200 Subject: [PATCH 8/8] error with period and commas corrected, test new example added --- .../negated_antonym_perturbation/README.md | 6 +++--- .../negated_antonym_perturbation/test.json | 16 ++++++++-------- .../transformation.py | 9 ++++++++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/transformations/negated_antonym_perturbation/README.md b/transformations/negated_antonym_perturbation/README.md index ba4e9d81d..bddcafde3 100644 --- a/transformations/negated_antonym_perturbation/README.md +++ b/transformations/negated_antonym_perturbation/README.md @@ -4,8 +4,8 @@ This perturbation rephrases the adjectives and adverbs to their negated antonym Authors: Mayukh Das (Technical University of Braunschweig / mayukh.das@tu-bs.de) ## What type of a transformation is this? -This transformation detects all type of adjectives and adverb and converts them to their negated antonym. -Therfore the transformation retains the semantics of the original text as positives gets converted to negated negatives and negatives gets converted to negated positives. +This transformation types of adjectives and adverbs and converts them to their negated antonym. +Therefore, the transformation retains the semantics of the original text as positives gets converted to negated negatives and negatives gets converted to negated positives. Example: I think you are prepared for the test --> I think you are not unprepared for the test ## What tasks does it intend to benefit? This can act as a valid perturbation that retains the semantics. @@ -13,7 +13,7 @@ This will benefit for robustness of text classification like sentiment analysis, ## What are the limitations of this transformation? -It is limited to only adjectives and adverbs (comparative and superlative) +It is limited to only adjectives and adverbs (all types e.g, comparative and superlative) ## Robustness Evaluation diff --git a/transformations/negated_antonym_perturbation/test.json b/transformations/negated_antonym_perturbation/test.json index a44a9f839..e3a73a372 100644 --- a/transformations/negated_antonym_perturbation/test.json +++ b/transformations/negated_antonym_perturbation/test.json @@ -7,7 +7,7 @@ "sentence": "I think you are successful." }, "outputs": [{ - "sentence": "I think you are not unsuccessful ." + "sentence": "I think you are not unsuccessful." }] }, { @@ -16,7 +16,7 @@ "sentence": "I think you are prepared for the test." }, "outputs": [{ - "sentence": "I think you are not unprepared for the test ." + "sentence": "I think you are not unprepared for the test." }] }, { @@ -34,7 +34,7 @@ "sentence": "He plays the flute beautifully." }, "outputs": [{ - "sentence": "He plays the flute not unattractively ." + "sentence": "He plays the flute not unattractively." }] }, { @@ -43,25 +43,25 @@ "sentence": "I think you are joyful." }, "outputs": [{ - "sentence": "I think you are not sorrowful ." + "sentence": "I think you are not sorrowful." }] }, { "class": "NegatedAntonym", "inputs": { - "sentence": "I think you are smarter than me." + "sentence": "During Harry's fourth year of school, Harry is unwillingly entered as a participant in the Triwizard Tournament, a dangerous yet exciting contest where three champions, one from each participating school, must compete with each other in three tasks in order to win the Triwizard Cup." }, "outputs": [{ - "sentence": "I think you are not stupid than me ." + "sentence": "During Harry's fourth year of school, Harry is not willingly entered as a participant in the Triwizard Tournament, a not safe yet exciting contest where three champions, one from each participating school, must compete with each not same in three tasks in order to win the Triwizard Cup." }] }, { "class": "NegatedAntonym", "inputs": { - "sentence": "The movie was bad." + "sentence": "Umbridge's incessant and persistent efforts to land him in trouble and the defensive lessons, Harry begins to lose sleep as he constantly receives disturbing dreams about a dark corridor in the Ministry of Magic, followed by a burning desire to learn more." }, "outputs": [{ - "sentence": "The movie was not good ." + "sentence": "Umbridge's incessant and not caducous efforts to land him in trouble and the not offensive lessons, Harry begins to lose sleep as he constantly receives disturbing dreams about a not light corridor in the Ministry of Magic, followed by a burning desire to learn not less." }] } ] diff --git a/transformations/negated_antonym_perturbation/transformation.py b/transformations/negated_antonym_perturbation/transformation.py index 55418c98b..4828bfe8a 100644 --- a/transformations/negated_antonym_perturbation/transformation.py +++ b/transformations/negated_antonym_perturbation/transformation.py @@ -9,7 +9,7 @@ class NegatedAntonym(SentenceOperation): - tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION] + tasks = [TaskType.TEXT_CLASSIFICATION, TaskType.TEXT_TO_TEXT_GENERATION, TaskType.SENTIMENT_ANALYSIS] languages = ["en"] keywords = ["sentiment-analysis", "tokenizer-required", "highly-meaning-preserving"] @@ -39,4 +39,11 @@ def Neg_Antonym(self, sentence): if len(antonyms) != 0: tokenized_sent[i] = 'not ' + antonyms[0] + for item in range(len(tokenized_sent)): + if tokenized_sent[item] == '.' or tokenized_sent[item] == ',' or tokenized_sent[item][0] == "'": + tokenized_sent[item - 1] = tokenized_sent[item - 1] + tokenized_sent[item] + tokenized_sent = [x for x in tokenized_sent if x != '.'] + tokenized_sent = [x for x in tokenized_sent if x != ','] + tokenized_sent = [x for x in tokenized_sent if x[0] != "'"] + return ' '.join(tokenized_sent)