|
|
""" |
|
|
|
|
|
TextBugger |
|
|
=============== |
|
|
|
|
|
(TextBugger: Generating Adversarial Text Against Real-world Applications) |
|
|
|
|
|
""" |
|
|
|
|
|
from textattack import Attack |
|
|
from textattack.constraints.pre_transformation import ( |
|
|
RepeatModification, |
|
|
StopwordModification, |
|
|
) |
|
|
from textattack.constraints.semantics.sentence_encoders import UniversalSentenceEncoder |
|
|
from textattack.goal_functions import UntargetedClassification |
|
|
from textattack.search_methods import GreedyWordSwapWIR |
|
|
from textattack.transformations import ( |
|
|
CompositeTransformation, |
|
|
WordSwapEmbedding, |
|
|
WordSwapHomoglyphSwap, |
|
|
WordSwapNeighboringCharacterSwap, |
|
|
WordSwapRandomCharacterDeletion, |
|
|
WordSwapRandomCharacterInsertion, |
|
|
) |
|
|
|
|
|
from .attack_recipe import AttackRecipe |
|
|
|
|
|
|
|
|
class TextBuggerLi2018(AttackRecipe): |
|
|
"""Li, J., Ji, S., Du, T., Li, B., and Wang, T. (2018). |
|
|
|
|
|
TextBugger: Generating Adversarial Text Against Real-world Applications. |
|
|
|
|
|
https://arxiv.org/abs/1812.05271 |
|
|
""" |
|
|
|
|
|
@staticmethod |
|
|
def build(model_wrapper): |
|
|
|
|
|
|
|
|
|
|
|
transformation = CompositeTransformation( |
|
|
[ |
|
|
|
|
|
|
|
|
|
|
|
WordSwapRandomCharacterInsertion( |
|
|
random_one=True, |
|
|
letters_to_insert=" ", |
|
|
skip_first_char=True, |
|
|
skip_last_char=True, |
|
|
), |
|
|
|
|
|
|
|
|
WordSwapRandomCharacterDeletion( |
|
|
random_one=True, skip_first_char=True, skip_last_char=True |
|
|
), |
|
|
|
|
|
|
|
|
|
|
|
WordSwapNeighboringCharacterSwap( |
|
|
random_one=True, skip_first_char=True, skip_last_char=True |
|
|
), |
|
|
|
|
|
|
|
|
|
|
|
WordSwapHomoglyphSwap(), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WordSwapEmbedding(max_candidates=5), |
|
|
] |
|
|
) |
|
|
|
|
|
constraints = [RepeatModification(), StopwordModification()] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constraints.append(UniversalSentenceEncoder(threshold=0.8)) |
|
|
|
|
|
|
|
|
|
|
|
goal_function = UntargetedClassification(model_wrapper) |
|
|
|
|
|
|
|
|
|
|
|
search_method = GreedyWordSwapWIR(wir_method="delete") |
|
|
|
|
|
return Attack(goal_function, constraints, transformation, search_method) |
|
|
|