textattack.constraints package
Constraints
Constraints determine whether a given transformation is valid. Since transformations do not perfectly preserve semantics semantics or grammaticality, constraints can increase the likelihood that the resulting transformation preserves these qualities. All constraints are subclasses of the Constraint
abstract class, and must implement at least one of __call__
or call_many
.
We split constraints into three main categories.
Semantics: Based on the meaning of the input and perturbation.
Grammaticality: Based on syntactic properties like part-of-speech and grammar.
Overlap: Based on character-based properties, like edit distance.
A fourth type of constraint restricts the search method from exploring certain parts of the search space:
pre_transformation: Based on the input and index of word replacement.
- textattack.constraints.grammaticality package
- Grammaticality:
- textattack.constraints.grammaticality.language_models package
- non-pre Language Models:
- textattack.constraints.grammaticality.language_models.google_language_model package
- textattack.constraints.grammaticality.language_models.learning_to_write package
- GPT2 Language Models:
GPT2
- Language Models Constraint
LanguageModelConstraint
- CoLA for Grammaticality
COLA
- LanguageTool Grammar Checker
LanguageTool
- Part of Speech Constraint
PartOfSpeech
- textattack.constraints.overlap package
- textattack.constraints.pre_transformation package
- textattack.constraints.semantics package
- Semantic Constraints
- textattack.constraints.semantics.sentence_encoders package
- Sentence Encoder Constraint
- textattack.constraints.semantics.sentence_encoders.bert package
- textattack.constraints.semantics.sentence_encoders.infer_sent package
- infer sent
- infer sent for sentence similarity
InferSent
- Infer sent model
InferSentModel
InferSentModel.build_vocab()
InferSentModel.build_vocab_k_words()
InferSentModel.encode()
InferSentModel.forward()
InferSentModel.get_batch()
InferSentModel.get_w2v()
InferSentModel.get_w2v_k()
InferSentModel.get_word_dict()
InferSentModel.is_cuda()
InferSentModel.prepare_samples()
InferSentModel.set_w2v_path()
InferSentModel.tokenize()
InferSentModel.update_vocab()
InferSentModel.training
- textattack.constraints.semantics.sentence_encoders.universal_sentence_encoder package
- Sentence Encoder Class
SentenceEncoder
get_angular_sim()
get_neg_euclidean_dist()
- Thought Vector Class
ThoughtVector
- BERT Score
BERTScore
- Word Embedding Distance
WordEmbeddingDistance
TextAttack Constraint Class
- class textattack.constraints.constraint.Constraint(compare_against_original)[source]
Bases:
ReprMixin
,ABC
An abstract class that represents constraints on adversial text examples. Constraints evaluate whether transformations from a
AttackedText
to anotherAttackedText
meet certain conditions.- Parameters:
compare_against_original (bool) – If True, the reference text should be the original text under attack. If False, the reference text is the most recent text from which the transformed text was generated. All constraints must have this attribute.
- call_many(transformed_texts, reference_text)[source]
Filters
transformed_texts
based on which transformations fulfill the constraint. First checks compatibility with latestTransformation
, then calls_check_constraint_many
- Parameters:
transformed_texts (list[AttackedText]) – The candidate transformed
AttackedText
’s.reference_text (AttackedText) – The
AttackedText
to compare against.
- check_compatibility(transformation)[source]
Checks if this constraint is compatible with the given transformation. For example, the
WordEmbeddingDistance
constraint compares the embedding of the word inserted with that of the word deleted. Therefore it can only be applied in the case of word swaps, and not for transformations which involve only one of insertion or deletion.- Parameters:
transformation – The
Transformation
to check compatibility with.
Pre-Transformation Constraint Class
- class textattack.constraints.pre_transformation_constraint.PreTransformationConstraint[source]
Bases:
ReprMixin
,ABC
An abstract class that represents constraints which are applied before the transformation.
These restrict which words are allowed to be modified during the transformation. For example, we might not allow stopwords to be modified.
- check_compatibility(transformation)[source]
Checks if this constraint is compatible with the given transformation. For example, the
WordEmbeddingDistance
constraint compares the embedding of the word inserted with that of the word deleted. Therefore it can only be applied in the case of word swaps, and not for transformations which involve only one of insertion or deletion.- Parameters:
transformation – The
Transformation
to check compatibility with.