-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #567 from QData/add-language-options-to-transforma…
…tion add language options
- Loading branch information
Showing
12 changed files
with
9,479 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
def test_imports(): | ||
import flair | ||
import torch | ||
|
||
import textattack | ||
|
||
del textattack, torch, flair | ||
|
||
|
||
def test_word_swap_change_location(): | ||
from flair.data import Sentence | ||
from flair.models import SequenceTagger | ||
|
||
from textattack.augmentation import Augmenter | ||
from textattack.transformations.word_swaps import WordSwapChangeLocation | ||
|
||
augmenter = Augmenter(transformation=WordSwapChangeLocation()) | ||
s = "I am in Dallas." | ||
s_augmented = augmenter.augment(s) | ||
augmented_text = Sentence(s_augmented[0]) | ||
tagger = SequenceTagger.load("flair/ner-english") | ||
original_text = Sentence(s) | ||
tagger.predict(original_text) | ||
tagger.predict(augmented_text) | ||
|
||
entity_original = [] | ||
entity_augmented = [] | ||
|
||
for entity in original_text.get_spans("ner"): | ||
entity_original.append(entity.tag) | ||
for entity in augmented_text.get_spans("ner"): | ||
entity_augmented.append(entity.tag) | ||
assert entity_original == entity_augmented | ||
|
||
|
||
def test_word_swap_change_name(): | ||
from flair.data import Sentence | ||
from flair.models import SequenceTagger | ||
|
||
from textattack.augmentation import Augmenter | ||
from textattack.transformations.word_swaps import WordSwapChangeName | ||
|
||
augmenter = Augmenter(transformation=WordSwapChangeName()) | ||
s = "My name is Anthony Davis." | ||
s_augmented = augmenter.augment(s) | ||
augmented_text = Sentence(s_augmented[0]) | ||
tagger = SequenceTagger.load("flair/ner-english") | ||
original_text = Sentence(s) | ||
tagger.predict(original_text) | ||
tagger.predict(augmented_text) | ||
|
||
entity_original = [] | ||
entity_augmented = [] | ||
|
||
for entity in original_text.get_spans("ner"): | ||
entity_original.append(entity.tag) | ||
for entity in augmented_text.get_spans("ner"): | ||
entity_augmented.append(entity.tag) | ||
assert entity_original == entity_augmented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from textattack import Attack | ||
from textattack.constraints.pre_transformation import ( | ||
RepeatModification, | ||
StopwordModification, | ||
) | ||
from textattack.goal_functions import UntargetedClassification | ||
from textattack.search_methods import GreedyWordSwapWIR | ||
from textattack.transformations import ( | ||
CompositeTransformation, | ||
WordSwapChangeLocation, | ||
WordSwapChangeName, | ||
WordSwapWordNet, | ||
) | ||
|
||
from .attack_recipe import AttackRecipe | ||
|
||
|
||
class FrenchRecipe(AttackRecipe): | ||
@staticmethod | ||
def build(model_wrapper): | ||
transformation = CompositeTransformation( | ||
[ | ||
WordSwapWordNet(language="fra"), | ||
WordSwapChangeLocation(language="fra"), | ||
WordSwapChangeName(language="fra"), | ||
] | ||
) | ||
constraints = [RepeatModification(), StopwordModification("french")] | ||
goal_function = UntargetedClassification(model_wrapper) | ||
search_method = GreedyWordSwapWIR() | ||
return Attack(goal_function, constraints, transformation, search_method) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from textattack import Attack | ||
from textattack.constraints.pre_transformation import ( | ||
RepeatModification, | ||
StopwordModification, | ||
) | ||
from textattack.goal_functions import UntargetedClassification | ||
from textattack.search_methods import GreedyWordSwapWIR | ||
from textattack.transformations import ( | ||
CompositeTransformation, | ||
WordSwapChangeLocation, | ||
WordSwapChangeName, | ||
WordSwapWordNet, | ||
) | ||
|
||
from .attack_recipe import AttackRecipe | ||
|
||
|
||
class SpanishRecipe(AttackRecipe): | ||
@staticmethod | ||
def build(model_wrapper): | ||
transformation = CompositeTransformation( | ||
[ | ||
WordSwapWordNet(language="esp"), | ||
WordSwapChangeLocation(language="esp"), | ||
WordSwapChangeName(language="esp"), | ||
] | ||
) | ||
constraints = [RepeatModification(), StopwordModification("spanish")] | ||
goal_function = UntargetedClassification(model_wrapper) | ||
search_method = GreedyWordSwapWIR() | ||
return Attack(goal_function, constraints, transformation, search_method) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.