textflint.generation_layer.transformation.ABSA.absa_transformation

class textflint.generation_layer.transformation.ABSA.absa_transformation.ABSATransformation[source]

Bases: textflint.generation_layer.transformation.transformation.Transformation, abc.ABC

An class that supply methods for ABSA task data transformation.

reverse(words_list, opinion_position)[source]

Reverse the polarity of opinions.

Parameters
  • words_list (list) – tokenized words of original sentence

  • opinion_position (list) – opinion position

Returns

transformed sentence and transformed opinion words

exaggerate(words_list, opinions)[source]

Exaggerate the opinion words.

Parameters
  • words_list (list) – tokenized words of original sentence

  • opinions (list) – opinion words and their positions

Returns

transformed sentence and opinion words

get_postag(sentence, start, end)[source]

Get the postag.

Parameters
  • sentence (list|str) – sentence

  • start (int) – start index

  • end (int) – end index

Return list

postag

refine_candidate(trans_words, opi_from, opi_to, candidate_list)[source]

Refine the candidate opinion words.

Parameters
  • trans_words (list) – tokenized words of transformed sentence

  • opi_from (int) – start position of opinion words

  • opi_to (int) – end position of opinion words

  • candidate_list (set) – candidate antonyms word list

Return list

refined candidate word list

static get_word2id(text, lower=True)[source]

Get the index of words in sentence.

Parameters
  • text (str) – input text

  • lower (bool) – whether text is lowercase or not

Return dict

index of words

static add_degree_words(word_list, from_idx, to_idx)[source]

Add the degree words to sentence.

Parameters
  • word_list (list) – tokenized words of original sentence

  • from_idx (int) – index of start

  • to_idx (int) – index of end

Returns

transformed sentence and opinion words

static get_conjunction_idx(trans_words, aspect_term, conjunction_list)[source]

Get the index of conjunction words in conjunction_list.

Parameters
  • trans_words (list) – tokenized words of transformed sentence

  • aspect_term (dict) – aspect term

  • conjunction_list (list) – conjunction list

Return list

index of transformed conjunction word

get_sentence(trans_words, sentence)[source]

Untokenize and uppercase to get an output sentence.

Parameters
  • trans_words (list) – transformed sentence

  • sentence (list) – original sentence

Return list

transformed sentence

get_term_span(trans_sentence, term)[source]

Get the span of term in trans_sentence.

Parameters
  • trans_sentence (list) – transformed sentence

  • term (list) – target term

Returns

start and end index of target term

get_candidate(candidate_list, words_list, postag_list, opi_from, opi_to)[source]

Get the candidate opinion words from words_list.

Parameters
  • candidate_list (set) – candidate words

  • words_list (list) – tokenized words of original sentence

  • postag_list (list) – postag

  • opi_from (int) – start index of opinion

  • opi_to (int) – end index of opinion

Return list

refined candidate words

check_negation(trans_words, opinion_from, opinion_to)[source]

Check the negation words in trans_words and delete them.

Parameters
  • trans_words (list) – tokenized words of transformed sentence

  • opinion_from (int) – start index of opinion

  • opinion_to (int) – end index of opinion

Returns

transformed words, opinion words, position of opinion, and

whether exist negation in transformed sentence

reverse_opinion(trans_words, trans_opinion_words, opinion_from, opinion_to, has_neg)[source]

Reverse the polarity of original opinion and return the new transformed opinion words.

Parameters
  • trans_words (list) – tokenized words of transformed sentence

  • trans_opinion_words (list) – transformed opinion words

  • opinion_from (int) – start index of opinion

  • opinion_to (int) – end index of opinion

  • has_neg (bool) – whether exist negation in transformed sentence

update_sentence_terms(trans_words, trans_terms, trans_opinion_words, opinion_position)[source]

Update the terms and sentence.

Parameters
  • trans_words (list) – tokenized words of transformed sentence

  • trans_terms (dict) – transformed terms

  • trans_opinion_words (list) – transformed opinion words

  • opinion_position (list) – opinion position

Returns

transformed sentence and transformed terms

class textflint.generation_layer.transformation.ABSA.absa_transformation.ABC[source]

Bases: object

Helper class that provides a standard way to create an ABC using inheritance.

class textflint.generation_layer.transformation.ABSA.absa_transformation.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.ABSA.absa_transformation.deepcopy(x, memo=None, _nil=[])[source]

Deep copy operation on arbitrary Python objects.

See the module’s __doc__ string for more info.