textflint.input_layer.component.sample.nli_sample

NLI Sample Class

class textflint.input_layer.component.sample.nli_sample.Field(field_value, field_type=<class 'str'>, **kwargs)[source]

Bases: object

A helper class that represents input string that to be modified.

__init__(field_value, field_type=<class 'str'>, **kwargs)[source]
Parameters
  • field_value (string|int|list) – The string that Field represents.

  • field_type (str) – field value type

class textflint.input_layer.component.sample.nli_sample.Sample(data, origin=None, sample_id=None)[source]

Bases: abc.ABC

Base Sample class to hold the necessary info and provide atomic operations

text_processor = <textflint.common.preprocess.en_processor.EnProcessor object>
__init__(data, origin=None, sample_id=None)[source]
Parameters
  • data (dict) – The dict obj that contains data info.

  • origin (sample) – original sample obj.

  • sample_id (int) – sampleindex

get_value(field)[source]

Get field value by field_str.

Parameters

field (str) – field name

Returns

field value

get_words(field)[source]

Get tokenized words of given textfield

Parameters

field (str) – field name

Returns

tokenized words

get_text(field)[source]

Get text string of given textfield

Parameters

field (str) – field name

Return string

text

get_mask(field)[source]

Get word masks of given textfield

Parameters

field (str) – field name

Returns

list of mask values

get_sentences(field)[source]

Get split sentences of given textfield

Parameters

field (str) – field name

Returns

list of sentences

get_pos(field)[source]

Get text field pos tags. :param str field: field name :return: pos tag list

get_ner(field)[source]

Get text field ner tags

Parameters

field (str) – field name

Returns

ner tag list

replace_fields(fields, field_values, field_masks=None)[source]

Fully replace multi fields at the same time and return new sample. Notice: Not suggest use this API as it will set mask values of TextField to MODIFIED_MASK.

Parameters
  • fields (list) – field str list

  • field_values (list) – field value list

  • field_masks (list) – indicate mask values, useful for printable text

Returns

Modified Sample

replace_field(field, field_value, field_mask=None)[source]

Fully replace single field and return new sample. Notice: Not suggest use this API as it will set mask values of TextField to MODIFIED_MASK.

Parameters
  • field (str) – field str

  • field_value – field_type

  • field_mask (list) – indicate mask value of field

Returns

Modified Sample

replace_field_at_indices(field, indices, items)[source]

Replace items of multi given scopes of field value at the same time. Stay away from the complex function !!!

Be careful of your input list shape.

Parameters
  • field (str) – field name

  • of int|list|slice indices (list) –

    each index can be int indicate replace single item or their list

    like [1, 2, 3],

    can be list like (0,3) indicate replace items from

    0 to 3(not included),

    can be slice which would be convert to list.

  • items

Returns

Modified Sample

replace_field_at_index(field, index, items)[source]

Replace items of given scope of field value.

Be careful of your input list shape.

Parameters
  • field (str) – field name

  • index (int|list|slice) –

    can be int indicate replace single item or list like [1, 2, 3], can be list like (0,3) indicate replace items

    from 0 to 3(not included),

    can be slice which would be convert to list.

  • items (str|list) – shape: indices_num, correspond to field_sub_items

Returns

Modified Sample

unequal_replace_field_at_indices(field, indices, rep_items)[source]

Replace scope items of field value with rep_items which may not equal with scope.

Parameters
  • field – field str

  • indices – list of int/tupe/list

  • rep_items – list

Returns

Modified Sample

delete_field_at_indices(field, indices)[source]

Delete items of given scopes of field value.

Parameters
  • field (str) – field name

  • of int|list|slice indices (list) –

    shape:indices_num each index can be int indicate delete single item or their list

    like [1, 2, 3],

    can be list like (0,3) indicate replace items

    from 0 to 3(not included),

    can be slice which would be convert to list.

Returns

Modified Sample

delete_field_at_index(field, index)[source]

Delete items of given scopes of field value.

Parameters
  • field (str) – field value

  • index (int|list|slice) –

    can be int indicate delete single item or their list like [1, 2, 3], can be list like (0,3) indicate replace items

    from 0 to 3(not included),

    can be slice which would be convert to list.

Returns

Modified Sample

insert_field_before_indices(field, indices, items)[source]

Insert items of multi given scopes before indices of field value at the same time.

Stay away from the complex function !!! Be careful of your input list shape.

Parameters
  • field (str) – field name

  • indices – list of int, shape:indices_num, list like [1, 2, 3]

  • items – list of str/list, shape: indices_num, correspond to indices

Returns

Modified Sample

insert_field_before_index(field, index, items)[source]

Insert items of multi given scope before index of field value.

Parameters
  • field (str) – field name

  • index (int) – indicate which index to insert items

  • items (str|list) – items to insert

Returns

Modified Sample

insert_field_after_indices(field, indices, items)[source]

Insert items of multi given scopes after indices of field value at the same time.

Stay away from the complex function !!! Be careful of your input list shape.

Parameters
  • field (str) – field name

  • indices – list of int, shape:indices_num, like [1, 2, 3]

  • items – list of str/list shape: indices_num, correspond to indices

Returns

Modified Sample

insert_field_after_index(field, index, items)[source]

Insert items of multi given scope after index of field value

Parameters
  • field (str) – field name

  • index (int) – indicate where to apply insert

  • items (str|list) – shape: indices_num, correspond to field_sub_items

Returns

Modified Sample

swap_field_at_index(field, first_index, second_index)[source]

Swap items between first_index and second_index of field value.

Parameters
  • field (str) – field name

  • first_index (int) –

  • second_index (int) –

Returns

Modified Sample

abstract check_data(data)[source]

Check rare data format

Parameters

data – rare data input

Returns

abstract load(data)[source]

Parse data into sample field value.

Parameters

data – rare data input

abstract dump()[source]

Convert sample info to input data json format.

Returns

dict object.

classmethod clone(original_sample)[source]

Deep copy self to a new sample

Parameters

original_sample – sample to be copied

Returns

Sample instance

property is_origin

Return whether the sample is original Sample.

class textflint.input_layer.component.sample.nli_sample.TextField(field_value, mask=None, is_one_sent=False, split_by_space=False, **kwargs)[source]

Bases: textflint.input_layer.component.field.field.Field

A helper class that represents input string that to be modified.

Text that Sample contains parsed in data set, TextField provides multiple methods for Sample to modify.

Support sentence level and word level modification, default using word level API.

text_processor = <textflint.common.preprocess.en_processor.EnProcessor object>
__init__(field_value, mask=None, is_one_sent=False, split_by_space=False, **kwargs)[source]
Parameters
  • field_value (str|list) – Sentence string or tokenized words.

  • mask (list) – list of mask values

  • is_one_sent (bool) – whether input is a sentence

  • split_by_space (boo) – whether tokenize sentence by split space

  • kwargs

pos_of_word_index(desired_word_idx)[source]

Get pos tag of given index.

Parameters

desired_word_idx (int) – desire index to get pos tag

Returns

pos tag of word of desired_word_idx.

replace_at_indices(indices, new_items)[source]

Replace words at indices and set their mask to MODIFIED_MASK.

Parameters
  • indices ([int|listslice]) –

    each index can be int indicate replace single item

    or their list like [1, 2, 3].

    each index can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    each index can be slice which would be convert to list.

  • new_items ([str|list|tuple]) – items corresponding indices.

Returns

Replaced TextField object.

replace_at_index(index, new_items)[source]

Replace words at indices and set their mask to MODIFIED_MASK.

Parameters
  • index (intlistslice) –

    can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    can be slice which would be convert to list.

  • new_items (str|listtuple) – items corresponding index.

Returns

Replaced TextField object.

delete_at_indices(indices)[source]

Delete words at indices and remove their mask value.

Parameters

indices ([int|list|slice]) –

each index can be int indicate replace single item

or their list like [1, 2, 3].

each index can be list like (0,3) indicate replace items

from 0 to 3(not included) or their list like [(0, 3), (5,6)]

each index can be slice which would be convert to list.

Returns

Modified TextField object.

delete_at_index(index)[source]

Delete words at index and remove their mask value.

Parameters

index (int|list|slice) –

can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

from 0 to 3(not included) or their list like [(0, 3), (5,6)]

can be slice which would be convert to list.

Returns

Modified TextField object.

insert_before_indices(indices, new_items)[source]

Insert words before indices.

Parameters
  • indices ([int]) –

    can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    can be slice which would be convert to list.

  • new_items ([str|list|tuple]) – items corresponding index.

Returns

new TextField object.

insert_before_index(index, new_items)[source]

Insert words before index and remove their mask value.

Parameters
  • index (int) –

    can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    can be slice which would be convert to list.

  • new_items (str|list|tuple) – items corresponding index.

Returns

new TextField object.

insert_after_indices(indices, new_items)[source]

Insert words after indices.

Parameters
  • indices ([int]) –

    can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    can be slice which would be convert to list.

  • new_items ([str|list|tuple]) – items corresponding index.

Returns

new TextField object.

insert_after_index(index, new_items)[source]

Insert words before index and remove their mask value.

Parameters
  • index (int) –

    can be int indicate replace single item or their list like [1, 2, 3] can be list like (0,3) indicate replace items

    from 0 to 3(not included) or their list like [(0, 3), (5,6)]

    can be slice which would be convert to list.

  • new_items (str|list|tuple) – items corresponding index.

Returns

new TextField object.

swap_at_index(first_index, second_index)[source]

Swap items between first_index and second_index of origin_list

Parameters
  • first_index (int) – index of first item

  • second_index (int) – index of second item

Returns

Modified TextField object.

property pos_tagging

Get POS tags.

Example:

given sentence 'All things in their being are good for something.'

>> [('All', 'DT'),
    ('things', 'NNS'),
    ('in', 'IN'),
    ('their', 'PRP$'),
    ('being', 'VBG'),
    ('are', 'VBP'),
    ('good', 'JJ'),
    ('for', 'IN'),
    ('something', 'NN'),
    ('.', '.')]
Returns

Tokenized tokens with their POS tags.

property ner

Get NER tags.

Example:

given sentence 'Lionel Messi is a football player from Argentina.'

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

A list of tuples, (entity, start, end, label)

property dependency_parsing

Dependency parsing.

Example:

given sentence: 'The quick brown fox jumps over the lazy dog.'

>>
    The     DT      4       det
    quick   JJ      4       amod
    brown   JJ      4       amod
    fox     NN      5       nsubj
    jumps   VBZ     0       root
    over    IN      9       case
    the     DT      9       det
    lazy    JJ      9       amod
    dog     NN      5       obl
Returns

A list of tuples, (token, pos, target, type)