textattack.constraints.pre_transformation package

Pre-Transformation:

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.

Input Column Modification

class textattack.constraints.pre_transformation.input_column_modification.InputColumnModification(matching_column_labels, columns_to_ignore)[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint disallowing the modification of words within a specific input column.

For example, can prevent modification of ‘premise’ during entailment.

extra_repr_keys()[source]

Set the extra representation of the constraint using these keys.

To print customized extra information, you should reimplement this method in your own constraint. Both single-line and multi- line strings are acceptable.

Max Modification Rate

class textattack.constraints.pre_transformation.max_modification_rate.MaxModificationRate(max_rate, min_threshold=1)[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint that prevents modifying words beyond certain percentage of total number of words.

Parameters
  • max_rate (float) – Percentage of words that can be modified. For example, given text of 20 words, max_rate=0.1 will allow at most 2 words to be modified.

  • min_threshold (int, optional, defaults to 1) – The minimum number of words that can be perturbed regardless of max_rate. For example, given text of 20 words and max_rate=0.1, setting`min_threshold=4` will still allow 4 words to be modified even though max_rate=0.1 only allows 2 words. This is useful since text length can vary a lot between samples, and a N% modification limit might not make sense for very short text.

extra_repr_keys()[source]

Set the extra representation of the constraint using these keys.

To print customized extra information, you should reimplement this method in your own constraint. Both single-line and multi- line strings are acceptable.

Max Word Index Modification

class textattack.constraints.pre_transformation.max_word_index_modification.MaxWordIndexModification(max_length)[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint disallowing the modification of words which are past some maximum sentence word-length limit.

extra_repr_keys()[source]

Set the extra representation of the constraint using these keys.

To print customized extra information, you should reimplement this method in your own constraint. Both single-line and multi- line strings are acceptable.

Min Word Lenth

class textattack.constraints.pre_transformation.min_word_length.MinWordLength(min_length)[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint that prevents modifications to words less than a certain word character-length.

Parameters

min_length – Minimum word character-length needed for changes to be made to a word.

Repeat Modification

class textattack.constraints.pre_transformation.repeat_modification.RepeatModification[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint disallowing the modification of words which have already been modified.

Stopword Modification

class textattack.constraints.pre_transformation.stopword_modification.StopwordModification(stopwords=None, language='english')[source]

Bases: textattack.constraints.pre_transformation_constraint.PreTransformationConstraint

A constraint disallowing the modification of stopwords.

check_compatibility(transformation)[source]

The stopword constraint only is concerned with word swaps since paraphrasing phrases containing stopwords is OK.

Parameters

transformation – The Transformation to check compatibility with.