|
""" |
|
|
|
DeepWordBug |
|
======================================== |
|
(Black-box Generation of Adversarial Text Sequences to Evade Deep Learning Classifiers) |
|
|
|
""" |
|
|
|
from textattack import Attack |
|
from textattack.constraints.overlap import LevenshteinEditDistance |
|
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, |
|
WordSwapNeighboringCharacterSwap, |
|
WordSwapRandomCharacterDeletion, |
|
WordSwapRandomCharacterInsertion, |
|
WordSwapRandomCharacterSubstitution, |
|
) |
|
|
|
from .attack_recipe import AttackRecipe |
|
|
|
|
|
class DeepWordBugGao2018(AttackRecipe): |
|
"""Gao, Lanchantin, Soffa, Qi. |
|
|
|
Black-box Generation of Adversarial Text Sequences to Evade Deep Learning |
|
Classifiers. |
|
|
|
https://arxiv.org/abs/1801.04354 |
|
""" |
|
|
|
@staticmethod |
|
def build(model_wrapper, use_all_transformations=True): |
|
|
|
|
|
|
|
if use_all_transformations: |
|
|
|
transformation = CompositeTransformation( |
|
[ |
|
|
|
WordSwapNeighboringCharacterSwap(), |
|
|
|
WordSwapRandomCharacterSubstitution(), |
|
|
|
WordSwapRandomCharacterDeletion(), |
|
|
|
WordSwapRandomCharacterInsertion(), |
|
] |
|
) |
|
else: |
|
|
|
|
|
|
|
transformation = WordSwapRandomCharacterSubstitution() |
|
|
|
|
|
|
|
constraints = [RepeatModification(), StopwordModification()] |
|
|
|
|
|
|
|
|
|
constraints.append(LevenshteinEditDistance(30)) |
|
|
|
|
|
|
|
goal_function = UntargetedClassification(model_wrapper) |
|
|
|
|
|
|
|
search_method = GreedyWordSwapWIR() |
|
|
|
return Attack(goal_function, constraints, transformation, search_method) |
|
|