def build(model): transformations = WordSwapWordNet() # # Don't modify the same word twice or the stopwords defined # in the TextFooler public implementation. # # fmt: off constraints = [RepeatModification()] #constraints = [StopwordModification()] # # During entailment, we should only edit the hypothesis - keep the premise # the same. # input_column_modification = InputColumnModification( ["premise", "hypothesis"], {"premise"} ) constraints.append(input_column_modification) #constraints.append(PartOfSpeech()) # # # # Goal is untargeted classification # goal_function = UntargetedClassification(model) # # Greedily swap words with "Word Importance Ranking". # search_method = PriotirizedSearch() return Attack(goal_function, constraints, transformations, search_method)
def __init__(self, **kwargs): from textattack.transformations import WordSwapWordNet transformation = WordSwapWordNet() super().__init__(transformation, constraints=DEFAULT_CONSTRAINTS, **kwargs)
def build(model): transformation = WordSwapWordNet() constraints = [RepeatModification(), StopwordModification()] goal_function = UntargetedClassification(model) # search over words based on a combination of their saliency score, and how efficient the WordSwap transform is search_method = GreedyWordSwapWIR("weighted-saliency") return Attack(goal_function, constraints, transformation, search_method)
def build(model, freq_dict, delta=None, type=None, gamma=None, candidates=8): transformations = [ WordSwapEmbedding(max_candidates=candidates), WordSwapWordNet() ] # # Don't modify the same word twice or the stopwords defined # in the TextFooler public implementation. # # fmt: off constraints = [RepeatModification()] # # During entailment, we should only edit the hypothesis - keep the premise # the same. # input_column_modification = InputColumnModification( ["premise", "hypothesis"], {"premise"}) constraints.append(input_column_modification) #constraints.append(PartOfSpeech()) # # Universal Sentence Encoder with a minimum angular similarity of ε = 0.5. # # # Goal is untargeted classification # goal_function = UntargetedClassification(model) # # Greedily swap words with "Word Importance Ranking". # search_method = VocabularySearch(freq_dict) return Attack(goal_function, constraints, transformations, search_method)
def PWWSRen2019(model): """An implementation of Probability Weighted Word Saliency from "Generating Natural Langauge Adversarial Examples through Probability Weighted Word Saliency", Ren et al., 2019. Words are prioritized for a synonym-swap transformation based on a combination of their saliency score and maximum word-swap effectiveness. Note that this implementation does not include the Named Entity adversarial swap from the original paper, because it requires access to the full dataset and ground truth labels in advance. https://www.aclweb.org/anthology/P19-1103/ """ transformation = WordSwapWordNet() constraints = [RepeatModification(), StopwordModification()] goal_function = UntargetedClassification(model) # search over words based on a combination of their saliency score, and how efficient the WordSwap transform is search_method = GreedyWordSwapWIR("pwws") return Attack(goal_function, constraints, transformation, search_method)