mindspore.dataset.text.BertTokenizer

查看源文件
class mindspore.dataset.text.BertTokenizer(vocab, suffix_indicator='##', max_bytes_per_token=100, unknown_token='[UNK]', lower_case=False, keep_whitespace=False, normalization_form=NormalizeForm.NONE, preserve_unused_token=True, with_offsets=False)[源代码]

使用Bert分词器对字符串进行分词。

说明

Windows平台尚不支持 BertTokenizer

参数:
  • vocab (Vocab) - 用于查词的词汇表。

  • suffix_indicator (str,可选) - 用于指示子词后缀的前缀标志。默认值: '##'

  • max_bytes_per_token (int,可选) - 分词最大长度,超过此长度的词汇将不会被拆分。默认值: 100

  • unknown_token (str,可选) - 对未知词汇的分词输出。当设置为空字符串时,直接返回对应未知词汇作为分词输出;否则,返回该字符串作为分词输出。默认值: '[UNK]'

  • lower_case (bool,可选) - 是否对字符串进行小写转换处理。若为 True ,会将字符串转换为小写并删除重音字符;若为 False ,将只对字符串进行规范化处理,其模式由 normalization_form 指定。默认值: False

  • keep_whitespace (bool,可选) - 是否在分词输出中保留空格。默认值: False

  • normalization_form (NormalizeForm, 可选) - 想要使用的规范化模式。可选值详见 NormalizeForm 。 默认值: NormalizeForm.NFKC

  • preserve_unused_token (bool,可选) - 是否保留特殊词汇。若为 True ,将不会对特殊词汇进行分词,如 ‘[CLS]’, ‘[SEP]’, ‘[UNK]’, ‘[PAD]’, ‘[MASK]’ 等。默认值: True

  • with_offsets (bool,可选) - 是否输出各Token在原字符串中的起始和结束偏移量。默认值: False

异常:
  • TypeError - 当 vocab 的类型不为 mindspore.dataset.text.Vocab

  • TypeError - 当 suffix_indicator 的类型不为str。

  • TypeError - 当 max_bytes_per_token 的类型不为int。

  • ValueError - 当 max_bytes_per_token 为负数。

  • TypeError - 当 unknown_token 的类型不为str。

  • TypeError - 当 lower_case 的类型不为bool。

  • TypeError - 当 keep_whitespace 的类型不为bool。

  • TypeError - 当 normalization_form 的类型不为 NormalizeForm

  • TypeError - 当 preserve_unused_token 的类型不为bool。

  • TypeError - 当 with_offsets 的类型不为bool。

支持平台:

CPU

样例:

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.text as text
>>> from mindspore.dataset.text import NormalizeForm
>>>
>>> # Use the transform in dataset pipeline mode
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data=["床前明月光"], column_names=["text"])
>>>
>>> # 1) If with_offsets=False, default output one column {["text", dtype=str]}
>>> vocab_list = ["床", "前", "明", "月", "光", "疑", "是", "地", "上", "霜", "举", "头", "望", "低",
...               "思", "故", "乡", "繁", "體", "字", "嘿", "哈", "大", "笑", "嘻", "i", "am", "mak",
...               "make", "small", "mistake", "##s", "during", "work", "##ing", "hour", "+", "/",
...               "-", "=", "12", "28", "40", "16", " ", "I", "[CLS]", "[SEP]", "[UNK]", "[PAD]", "[MASK]"]
>>> vocab = text.Vocab.from_list(vocab_list)
>>> tokenizer_op = text.BertTokenizer(vocab=vocab, suffix_indicator='##', max_bytes_per_token=100,
...                                   unknown_token='[UNK]', lower_case=False, keep_whitespace=False,
...                                   normalization_form=NormalizeForm.NONE, preserve_unused_token=True,
...                                   with_offsets=False)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=tokenizer_op)
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["text"])
['床' '前' '明' '月' '光']
>>>
>>> # 2) If with_offsets=True, then output three columns {["token", dtype=str],
>>> #                                                     ["offsets_start", dtype=uint32],
>>> #                                                     ["offsets_limit", dtype=uint32]}
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data=["床前明月光"], column_names=["text"])
>>> tokenizer_op = text.BertTokenizer(vocab=vocab, suffix_indicator='##', max_bytes_per_token=100,
...                                   unknown_token='[UNK]', lower_case=False, keep_whitespace=False,
...                                   normalization_form=NormalizeForm.NONE, preserve_unused_token=True,
...                                   with_offsets=True)
>>> numpy_slices_dataset = numpy_slices_dataset.map(
...     operations=tokenizer_op,
...     input_columns=["text"],
...     output_columns=["token", "offsets_start", "offsets_limit"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["token"], item["offsets_start"], item["offsets_limit"])
['床' '前' '明' '月' '光'] [ 0  3  6  9 12] [ 3  6  9 12 15]
>>>
>>> # Use the transform in eager mode
>>> data = "床前明月光"
>>> vocab = text.Vocab.from_list(vocab_list)
>>> tokenizer_op = text.BertTokenizer(vocab=vocab)
>>> output = tokenizer_op(data)
>>> print(output)
['床' '前' '明' '月' '光']
教程样例: