textflint.generation_layer.transformation.UT.prejudice

Reverse gender or place names in sentences

class textflint.generation_layer.transformation.UT.prejudice.Prejudice(change_type='Loc', prejudice_tendency=None, **kwargs)[source]

Bases: textflint.generation_layer.transformation.transformation.Transformation

Transforms an input by Reverse gender or place names in sentences.

__init__(change_type='Loc', prejudice_tendency=None, **kwargs)[source]
Parameters
  • change_type (str) – change type, only support [‘Name’, ‘Loc’]

  • prejudice_tendency (str) – prejudice tendency, choose different tendency according to change_type

class textflint.generation_layer.transformation.UT.prejudice.Transformation(**kwargs)[source]

Bases: abc.ABC

An abstract class for transforming a sequence of text to produce a list of potential adversarial example.

processor = <textflint.common.preprocess.en_processor.EnProcessor object>
transform(sample, n=1, field='x', **kwargs)[source]

Transform data sample to a list of Sample.

Parameters
  • sample (Sample) – Data sample for augmentation.

  • n (int) – Max number of unique augmented output, default is 5.

  • field (str|list) – Indicate which fields to apply transformations.

  • **kwargs (dict) –

    other auxiliary params.

Returns

list of Sample

classmethod sample_num(x, num)[source]

Get ‘num’ samples from x.

Parameters
  • x (list) – list to sample

  • num (int) – sample number

Returns

max ‘num’ unique samples.

textflint.generation_layer.transformation.UT.prejudice.descartes(calculation_items, n)[source]
Parameters
  • calculation_items (list) –

  • n (int) – quantity to select

Return list

list items which we random choice from Cartesian product.

textflint.generation_layer.transformation.UT.prejudice.download_if_needed(folder_name)[source]

Folder name will be saved as .cache/textflint/[folder_name]. If it doesn’t exist on disk, the zip file will be downloaded and extracted.

Parameters

folder_name (str) – path to folder or file in cache

Returns

path to the downloaded folder or file on disk

textflint.generation_layer.transformation.UT.prejudice.plain_lines_loader(path)[source]

read data

textflint.generation_layer.transformation.UT.prejudice.read_json(path, encoding='utf-8', fields=None, dropna=True)[source]

Construct a generator to read json items.

Parameters
  • path – file path

  • encoding – file’s encoding, default: utf-8

  • fields – json object’s fields that needed, if None, all fields are needed. default: None

  • dropna – whether to ignore and drop invalid data, if False, raise ValueError when reading invalid data. default: True

Returns

generator, every time yield (line number, json item)