fibber.paraphrase_strategies.openattack_strategy module

This module implements the paraphrase strategy using TextFooler.

class fibber.paraphrase_strategies.openattack_strategy.MyClassifier(clf_metric, field)[source]

Bases: object

get_counter()[source]
get_pred(input_)[source]
get_prob(input_)[source]
reset_counter()[source]
set_data_record(data_record)[source]
class fibber.paraphrase_strategies.openattack_strategy.OpenAttackStrategy(arg_dict, dataset_name, strategy_gpu_id, output_dir, metric_bundle, field)[source]

Bases: fibber.paraphrase_strategies.strategy_base.StrategyBase

This strategy is a wrapper for strategies implemented in OpenAttack Package.

The recipe is used to attack the classifier in metric_bundle.

If the attack succeeds, we use the adversarial sentence as a paraphrase. If the attack fails, we use the original sentence as a paraphrase.

This strategy always returns one paraphrase for one data record, regardless of n.

Initialize the paraphrase_strategies.

This function initialize the self._strategy_config, self._metric_bundle, self._device, self._output_dir, self._dataset_name.

You should not overwrite this function.

  • self._strategy_config (dict): a dictionary that stores the strategy name and all hyperparameter values. The dict is also saved to the results.

  • self._metric_bundle (MetricBundle): the metrics that will be used to evaluate paraphrases. Strategies can compute metrics during paraphrasing.

  • self._device (torch.Device): any computation that requires a GPU accelerator should use this device.

  • self._output_dir (str): the dir name where the strategy can save files.

  • self._dataset_name (str): the dataset name.

Parameters
  • arg_dict (dict) – all args load from command line.

  • dataset_name (str) – the name of the dataset.

  • strategy_gpu_id (int) – the gpu id to run the strategy.

  • output_dir (str) – a directory to save any models or temporary files.

  • metric_bundle (MetricBundle) – a MetricBundle object.

fit(trainset)[source]

Fit the paraphrase strategy on a training set.

Parameters

trainset (dict) – a fibber dataset.

paraphrase_example(data_record, n)[source]

Generate paraphrased sentences.