mindspore.dataset.text.RegexTokenizer

View Source On Gitee
class mindspore.dataset.text.RegexTokenizer(delim_pattern, keep_delim_pattern='', with_offsets=False)[source]

Tokenize a scalar tensor of UTF-8 string by regex expression pattern.

See https://unicode-org.github.io/icu/userguide/strings/regexp.html for supported regex pattern.

Note

RegexTokenizer is not supported on Windows platform yet.

Parameters
  • delim_pattern (str) – The pattern of regex delimiters. The original string will be split by matched elements.

  • keep_delim_pattern (str, optional) – The string matched by ‘delim_pattern’ can be kept as a token if it can be matched by ‘keep_delim_pattern’. The default value is an empty str which means that delimiters will not be kept as an output token. Default: ''.

  • with_offsets (bool, optional) – Whether to output the start and end offsets of each token in the original string. Default: False .

Raises
  • TypeError – If delim_pattern is not of type string.

  • TypeError – If keep_delim_pattern is not of type string.

  • TypeError – If with_offsets is not of type bool.

Supported Platforms:

CPU

Examples

>>> import mindspore.dataset as ds
>>> import mindspore.dataset.text as text
>>>
>>> text_file_list = ["/path/to/text_file_dataset_file"]
>>> text_file_dataset = ds.TextFileDataset(dataset_files=text_file_list)
>>>
>>> # 1) If with_offsets=False, default output is one column {["text", dtype=str]}
>>> delim_pattern = r"[ |,]"
>>> tokenizer_op = text.RegexTokenizer(delim_pattern, with_offsets=False)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op)
>>>
>>> # 2) If with_offsets=True, then output three columns {["token", dtype=str],
>>> #                                                     ["offsets_start", dtype=uint32],
>>> #                                                     ["offsets_limit", dtype=uint32]}
>>> tokenizer_op = text.RegexTokenizer(delim_pattern, with_offsets=True)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op, input_columns=["text"],
...                                           output_columns=["token", "offsets_start", "offsets_limit"])
Tutorial Examples: