Skip to content

Commit

Permalink
reformatting with python3.9 black
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyanjun committed Mar 10, 2024
1 parent c5af16c commit 9966100
Show file tree
Hide file tree
Showing 105 changed files with 112 additions and 79 deletions.
1 change: 1 addition & 0 deletions textattack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TextAttack provides components for common NLP tasks like sentence encoding, grammar-checking, and word replacement that can be used on their own.
"""

from .attack_args import AttackArgs, CommandLineAttackArgs
from .augment_args import AugmenterArgs
from .dataset_args import DatasetArgs
Expand Down
10 changes: 5 additions & 5 deletions textattack/attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def __init__(
constraints: List[Union[Constraint, PreTransformationConstraint]],
transformation: Transformation,
search_method: SearchMethod,
transformation_cache_size=2 ** 15,
constraint_cache_size=2 ** 15,
transformation_cache_size=2**15,
constraint_cache_size=2**15,
):
"""Initialize an attack object.
Expand Down Expand Up @@ -372,9 +372,9 @@ def filter_transformations(
uncached_texts.append(transformed_text)
else:
# promote transformed_text to the top of the LRU cache
self.constraints_cache[
(current_text, transformed_text)
] = self.constraints_cache[(current_text, transformed_text)]
self.constraints_cache[(current_text, transformed_text)] = (
self.constraints_cache[(current_text, transformed_text)]
)
if self.constraints_cache[(current_text, transformed_text)]:
filtered_texts.append(transformed_text)
filtered_texts += self._filter_transformations_uncached(
Expand Down
4 changes: 2 additions & 2 deletions textattack/attack_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ class _CommandLineAttackArgs:
interactive: bool = False
parallel: bool = False
model_batch_size: int = 32
model_cache_size: int = 2 ** 18
constraint_cache_size: int = 2 ** 18
model_cache_size: int = 2**18
constraint_cache_size: int = 2**18

@classmethod
def _add_parser_args(cls, parser):
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/bae_garg_2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
============================================
"""

from textattack.constraints.grammaticality import PartOfSpeech
from textattack.constraints.pre_transformation import (
RepeatModification,
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/bert_attack_li_2020.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Consider using smaller values for "max_candidates".
"""

from textattack import Attack
from textattack.constraints.overlap import MaxWordsPerturbed
from textattack.constraints.pre_transformation import (
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/checklist_ribeiro_2020.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(Beyond Accuracy: Behavioral Testing of NLP models with CheckList)
"""

from textattack import Attack
from textattack.constraints.pre_transformation import RepeatModification
from textattack.goal_functions import UntargetedClassification
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/hotflip_ebrahimi_2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(HotFlip: White-Box Adversarial Examples for Text Classification)
"""

from textattack import Attack
from textattack.constraints.grammaticality import PartOfSpeech
from textattack.constraints.overlap import MaxWordsPerturbed
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/iga_wang_2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(Natural Language Adversarial Attacks and Defenses in Word Level)
"""

from textattack import Attack
from textattack.constraints.overlap import MaxWordsPerturbed
from textattack.constraints.pre_transformation import StopwordModification
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/input_reduction_feng_2018.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(Pathologies of Neural Models Make Interpretations Difficult)
"""

from textattack import Attack
from textattack.constraints.pre_transformation import (
RepeatModification,
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/kuleshov_2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(Adversarial Examples for Natural Language Classification Problems)
"""

from textattack import Attack
from textattack.constraints.grammaticality.language_models import GPT2
from textattack.constraints.overlap import MaxWordsPerturbed
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/morpheus_tan_2020.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from textattack import Attack
from textattack.constraints.pre_transformation import (
RepeatModification,
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/pruthi_2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
=================================================================
"""

from textattack import Attack
from textattack.constraints.overlap import MaxWordsPerturbed
from textattack.constraints.pre_transformation import (
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/pso_zang_2020.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(Word-level Textual Adversarial Attacking as Combinatorial Optimization)
"""

from textattack import Attack
from textattack.constraints.pre_transformation import (
InputColumnModification,
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/pwws_ren_2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(Generating Natural Language Adversarial Examples through Probability Weighted Word Saliency)
"""

from textattack import Attack
from textattack.constraints.pre_transformation import (
RepeatModification,
Expand Down
1 change: 1 addition & 0 deletions textattack/attack_recipes/seq2sick_cheng_2018_blackbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
================================================
(Seq2Sick: Evaluating the Robustness of Sequence-to-Sequence Models with Adversarial Examples)
"""

from textattack import Attack
from textattack.constraints.overlap import LevenshteinEditDistance
from textattack.constraints.pre_transformation import (
Expand Down
1 change: 0 additions & 1 deletion textattack/attack_results/successful_attack_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


from .attack_result import AttackResult


Expand Down
1 change: 0 additions & 1 deletion textattack/augment_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
===================
"""


from dataclasses import dataclass

AUGMENTATION_RECIPE_NAMES = {
Expand Down
1 change: 0 additions & 1 deletion textattack/augmentation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Transformations and constraints can be used outside of an attack for simple NLP data augmentation with the ``Augmenter`` class that returns all possible transformations for a given string.
"""


from .augmenter import Augmenter
from .recipes import (
WordNetAugmenter,
Expand Down
1 change: 1 addition & 0 deletions textattack/augmentation/augmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Augmenter Class
===================
"""

import random

import tqdm
Expand Down
1 change: 1 addition & 0 deletions textattack/augmentation/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Transformations and constraints can be used for simple NLP data augmentations. Here is a list of recipes for NLP data augmentations
"""

import random

from textattack.constraints.pre_transformation import (
Expand Down
1 change: 0 additions & 1 deletion textattack/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""


from abc import ABC, abstractmethod
from .textattack_command import TextAttackCommand
from . import textattack_cli
1 change: 0 additions & 1 deletion textattack/commands/eval_model_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""


from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from dataclasses import dataclass

Expand Down
1 change: 0 additions & 1 deletion textattack/commands/textattack_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""


# !/usr/bin/env python
import argparse

Expand Down
1 change: 0 additions & 1 deletion textattack/commands/train_model_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""


from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser

from textattack import CommandLineTrainingArgs, Trainer
Expand Down
3 changes: 2 additions & 1 deletion textattack/constraints/grammaticality/cola.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--------------------------
"""

import lru
import nltk
from transformers import AutoModelForSequenceClassification, AutoTokenizer
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(

self.max_diff = max_diff
self.model_name = model_name
self._reference_score_cache = lru.LRU(2 ** 10)
self._reference_score_cache = lru.LRU(2**10)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = HuggingFaceModelWrapper(model, tokenizer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


from .language_model_constraint import LanguageModelConstraint

from .google_language_model import Google1BillionWordsLanguageModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


from .google_language_model import (
GoogleLanguageModel as Google1BillionWordsLanguageModel,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
All rights reserved.
"""


import os

import lru
Expand Down Expand Up @@ -49,7 +48,7 @@ def __init__(self):
self.sess, self.graph, self.PBTXT_PATH, self.CKPT_PATH
)

self.lm_cache = lru.LRU(2 ** 18)
self.lm_cache = lru.LRU(2**18)

def clear_cache(self):
self.lm_cache.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--------------------------------------
"""

from collections import defaultdict

import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Author: Moustafa Alzantot ([email protected])
All rights reserved.
"""

import sys

from textattack.shared.utils import LazyLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


import os

import torch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
--------------------------
"""

from .learning_to_write import LearningToWriteLanguageModel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""


import torch
from torch import nn
from torch.autograd import Variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


from torch import nn as nn
from torch.autograd import Variable

Expand Down
1 change: 1 addition & 0 deletions textattack/constraints/grammaticality/language_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
LanguageTool Grammar Checker
------------------------------
"""

import language_tool_python

from textattack.constraints import Constraint
Expand Down
3 changes: 2 additions & 1 deletion textattack/constraints/grammaticality/part_of_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Part of Speech Constraint
--------------------------
"""

import flair
from flair.data import Sentence
from flair.models import SequenceTagger
Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(
self.language_nltk = language_nltk
self.language_stanza = language_stanza

self._pos_tag_cache = lru.LRU(2 ** 14)
self._pos_tag_cache = lru.LRU(2**14)
if tagger_type == "flair":
if tagset == "universal":
self._flair_pos_tagger = SequenceTagger.load("upos-fast")
Expand Down
1 change: 0 additions & 1 deletion textattack/constraints/overlap/meteor_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""


import nltk

from textattack.constraints import Constraint
Expand Down
1 change: 1 addition & 0 deletions textattack/constraints/pre_transformation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Pre-transformation constraints determine if a transformation is valid based on only the original input and the position of the replacement. These constraints are applied before the transformation is even called. For example, these constraints can prevent search methods from swapping words at the same index twice, or from replacing stopwords.
"""

from .stopword_modification import StopwordModification
from .repeat_modification import RepeatModification
from .input_column_modification import InputColumnModification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-----------------------------
"""

import math

from textattack.constraints import PreTransformationConstraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
-----------------------------
"""

from textattack.constraints import PreTransformationConstraint


Expand Down
1 change: 1 addition & 0 deletions textattack/constraints/semantics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
---------------------
Semantic constraints determine if a transformation is valid based on similarity of the semantics of the orignal input and the transformed input.
"""

from . import sentence_encoders

from .word_embedding_distance import WordEmbeddingDistance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
----------------------------
"""


from .sentence_encoder import SentenceEncoder

from .sentence_bert import SBERT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
^^^^^^^^^^^^
"""


from .infer_sent import InferSent
Loading

0 comments on commit 9966100

Please sign in to comment.