textflint.generation_layer.transformation.COREF.random_replace

Coref - Rnd Replace: some irrelevance sentences will replace the

original sentences, the corefs including in which will be ignored.


class textflint.generation_layer.transformation.COREF.random_replace.RndReplace(trans_p=0.2, **kwargs)[source]

Bases: textflint.generation_layer.transformation.transformation.Transformation

RndReplace: trans_p * num_sentences of sentences are replaced by

irrelevant sentences from samples_other, and the attached corefs will be ignored.

Attributes:

trans_p: proportion of inserted sentences; default 0.2 processor: textflint.common.preprocess.TextProcessor.

Example:

ori: {
    'sentences': [
        ['I', 'came'], ['I', 'saw'], ['I', 'conquered'], 
        ['Anna', 'bel', 'wanna', 'sleep'],
        ['Anna', 'bel', 'is', 'happy']],
    'clusters': [
        [[1, 1], [3, 3], [5, 5]], 
        [[7, 8], [11, 12]]]}
trans: {
    'sentences': [
        ['I', 'came'], ['It', 'was', 'a', 'good', 'trip'], 
        ['I', 'conquered'], ['Anna', 'bel', 'wanna', 'sleep'], 
        ['Anna', 'bel', 'is', 'happy']],
    'clusters': [
        [[1, 1], [8, 8]], 
        [[10, 11], [16, 17]]]}
class textflint.generation_layer.transformation.COREF.random_replace.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.COREF.random_replace.ceil(x, /)

Return the ceiling of x as an Integral.

This is the smallest integer >= x.

textflint.generation_layer.transformation.COREF.random_replace.copy(x)[source]

Shallow copy operation on arbitrary Python objects.

See the module’s __doc__ string for more info.