textattack.constraints.overlap package

Overlap Constraints

Overlap constraints determine if a transformation is valid based on character-level analysis.

BLEU Constraints

class textattack.constraints.overlap.bleu_score.BLEU(max_bleu_score, compare_against_original=True)[source]

Bases: Constraint

A constraint on BLEU score difference.

Parameters:
  • max_bleu_score (int) – Maximum BLEU score allowed.

  • compare_against_original (bool) – If True, compare new x_adv against the original x. Otherwise, compare it against the previous x_adv.

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.

chrF Constraints

class textattack.constraints.overlap.chrf_score.chrF(max_chrf, compare_against_original=True)[source]

Bases: Constraint

A constraint on chrF (n-gram F-score) difference.

Parameters:
  • max_chrf (int) – Max n-gram F-score allowed.

  • compare_against_original (bool) – If True, compare new x_adv against the original x. Otherwise, compare it against the previous x_adv.

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.

Edit Distance Constraints

class textattack.constraints.overlap.levenshtein_edit_distance.LevenshteinEditDistance(max_edit_distance, compare_against_original=True)[source]

Bases: Constraint

A constraint on edit distance (Levenshtein Distance).

Parameters:
  • max_edit_distance (int) – Maximum edit distance allowed.

  • compare_against_original (bool) – If True, compare new x_adv against the original x. Otherwise, compare it against the previous x_adv.

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 Perturb Words Constraints

class textattack.constraints.overlap.max_words_perturbed.MaxWordsPerturbed(max_num_words=None, max_percent=None, compare_against_original=True)[source]

Bases: Constraint

A constraint representing a maximum allowed perturbed words.

Parameters:
  • max_num_words (int, optional) – Maximum number of perturbed words allowed.

  • ( (max_percent) – obj: float, optional): Maximum percentage of words allowed to be perturbed.

  • compare_against_original (bool) – If True, compare new x_adv against the original x. Otherwise, compare it against the previous x_adv.

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.

METEOR Constraints

class textattack.constraints.overlap.meteor_score.METEOR(max_meteor, compare_against_original=True)[source]

Bases: Constraint

A constraint on METEOR score difference.

Parameters:
  • max_meteor (int) – Max METEOR score allowed.

  • compare_against_original (bool) – If True, compare new x_adv against the original x. Otherwise, compare it against the previous x_adv.

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.