textflint.generation_layer.transformation.UT.swap_named_ent

SwapNamedEnt substitute class

class textflint.generation_layer.transformation.UT.swap_named_ent.SwapNamedEnt(entity_res=None, **kwargs)[source]

Bases: textflint.generation_layer.transformation.transformation.Transformation

Swap entities with other entities of the same category.

__init__(entity_res=None, **kwargs)[source]
Parameters

entity_res (dict) – dic of categories and their entities.

static decompose_entities_info(entities_info)[source]

Decompose given entities and normalize entity tag to [‘LOCATION’, ‘PERSON’, ‘ORGANIZATION’]

Example:

[('Lionel Messi', 0, 2, 'PERSON'),
('Argentina', 7, 8, 'LOCATION')]
>> [[0, 2], [7, 8]], [‘Lionel Messi’, ‘Argentina’],

[‘PERSON’, ‘LOCATION’]

Parameters

entities_info (dict) – parsed by default ner component.

Return list indices

indices

Return list entities

entity values

Return list categories

categories

class textflint.generation_layer.transformation.UT.swap_named_ent.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.swap_named_ent.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.swap_named_ent.trade_off_sub_words(sub_words, sub_indices, trans_num=None, n=1)[source]

Select proper candidate words to maximum number of transform result. Select words of top n substitutes words number.

Parameters
  • sub_words (list) – list of substitutes word of each legal word

  • sub_indices (list) – list of indices of each legal word

  • trans_num (int) – max number of words to apply substitution

  • n (int) –

Returns

sub_words after alignment + indices of sub_words