speechbrain.tokenizers.SentencePiece 模块

用于字节对编码 (BPE) 分词的库。作者

  • Abdelwahab Heba 2020

  • Loren Lugosch 2020

摘要

SentencePiece

BPE 类调用来自 Google 的 SentencePiece 无监督文本分词器。参考:https://github.com/google/sentencepiece SentencePiece 库是一个无监督的文本分词器和去分词器。它实现了子词单元,如字节对编码 (BPE)、Unigram 语言模型以及字符/词分词器。 :param model_dir: 模型将保存(或已存储)的目录。 :type model_dir: str :param vocab_size: 所选分词器类型 (BPE, Unigram) 的词汇表大小。对于 char 分词,vocab_size 是可选的;对于 BPE 和 unigram 分词,它是必需的。 :type vocab_size: int, None, optional :param annotation_train: 用于学习分词器的标注文件的路径。可以是 JSON 或 csv 格式。 :type annotation_train: str :param annotation_read: 标注文件中包含词序列的数据项。 :type annotation_read: str :param model_type: (bpe, char, unigram)。如果为“bpe”,训练无监督的词片段分词。参见:https://www.aclweb.org/anthology/P16-1162/ 如果为“word”,从输入文本中获取词汇表。如果为“unigram”,使用 unigram 语言模型进行词片段分词。参见:https://arxiv.org/abs/1804.10959 :type model_type: str :param char_format_input: 读取的条目是否包含字符格式输入。(默认值:False)(例如,a p p l e _ i s _ g o o d) :type char_format_input: bool :param character_coverage: 模型覆盖的字符数量,推荐默认值:对于字符集丰富的语言(如日语或中文)为 0.9995,对于字符集较小的其他语言为 1.0。(默认值:1.0) :type character_coverage: int :param user_defined_symbols: 包含用逗号分隔的符号列表的字符串。用户定义的符号在任何上下文中都被视为一个整体。(默认值:None) :type user_defined_symbols: string :param max_sentencepiece_length: token 的最大字符数。(默认值:10) :type max_sentencepiece_length: int :param bos_id: 如果为 -1,则 bos_id = unk_id = 0。否则,bos_id 为 int。(默认值:-1) :type bos_id: int :param eos_id: 如果为 -1,则 eos_id = unk_id = 0。否则,eos_id 为 int。(默认值:-1) :type eos_id: int :param pad_id: 如果为 -1,则 pad_id = unk_id = 0。否则,pad_id 为 int。(默认值:-1) :type pad_id: int :param unk_id: 对应于未知符号(不在 token 集合中)的 token。 :type unk_id: int :param split_by_whitespace: 如果为 False,则允许 sentencepiece 提取跨越多个词的片段。此功能对于中文/日文/韩文非常重要。(默认值:True) :type split_by_whitespace: bool :param num_sequences: 如果不是 None,则最多使用这么多序列来训练分词器(适用于大型数据集)。(默认值:None) :type num_sequences: int :param annotation_list_to_check: 用于检查分词器恢复词准确性的标注文件列表。 :type annotation_list_to_check: list, :param annotation_format: 标注文件的格式。支持 JSON 或 csv 格式。 :type annotation_format: str :param text_file: 文本文件的备用路径(当多个模型在同一数据文件上训练时需要) :type text_file: str :param add_dummy_prefix: 如果为 True,分词器会在文本开头添加一个虚拟空格。(默认值:True) :type add_dummy_prefix: bool。

SentencePieceDecoderStreamingContext

单个 SentencePiece 流式会话的可变流式上下文。

函数

get_spm_tokens

获取 token 列表,可以通过 token ID 索引

spm_decode_preserve_leading_space

假设分词器是 SentencePiece,解码输入假设,但在流式处理时避免错误地去除前导空格。

参考

class speechbrain.tokenizers.SentencePiece.SentencePiece(model_dir, vocab_size, annotation_train=None, annotation_read=None, model_type='unigram', char_format_input=False, character_coverage=1.0, user_defined_symbols=None, max_sentencepiece_length=10, bos_id=-1, eos_id=-1, pad_id=-1, unk_id=0, split_by_whitespace=True, num_sequences=None, annotation_list_to_check=None, annotation_format='csv', text_file=None, add_dummy_prefix=True)[source]

基类: object

BPE 类调用来自 Google 的 SentencePiece 无监督文本分词器。参考:https://github.com/google/sentencepiece SentencePiece 库是一个无监督的文本分词器和去分词器。它实现了子词单元,如字节对编码 (BPE)、Unigram 语言模型以及字符/词分词器。 :param model_dir: 模型将保存(或已存储)的目录。 :type model_dir: str :param vocab_size: 所选分词器类型 (BPE, Unigram) 的词汇表大小。

对于 char 分词,vocab_size 是可选的;对于 BPE 和 unigram 分词,它是必需的。

参数:
  • annotation_train (str) – 用于学习分词器的标注文件的路径。可以是 JSON 或 csv 格式。

  • annotation_read (str) – 标注文件中包含词序列的数据项。

  • model_type (str) – (bpe, char, unigram)。如果为“bpe”,训练无监督的词片段分词。参见:https://www.aclweb.org/anthology/P16-1162/ 如果为“word”,从输入文本中获取词汇表。如果为“unigram”,使用 unigram 语言模型进行词片段分词。参见:https://arxiv.org/abs/1804.10959

  • char_format_input (bool) – 读取的条目是否包含字符格式输入。(默认值:False)(例如,a p p l e _ i s _ g o o d)

  • character_coverage (int) – 模型覆盖的字符数量,推荐默认值:对于字符集丰富的语言(如日语或中文)为 0.9995,对于字符集较小的其他语言为 1.0。(默认值:1.0)

  • user_defined_symbols (string) – 包含用逗号分隔的符号列表的字符串。用户定义的符号在任何上下文中都被视为一个整体。(默认值:None)

  • max_sentencepiece_length (int) – token 的最大字符数。(默认值:10)

  • bos_id (int) – 如果为 -1,则 bos_id = unk_id = 0。否则,bos_id 为 int。(默认值:-1)

  • eos_id (int) – 如果为 -1,则 eos_id = unk_id = 0。否则,eos_id 为 int。(默认值:-1)

  • pad_id (int) – 如果为 -1,则 pad_id = unk_id = 0。否则,pad_id 为 int。(默认值:-1)

  • unk_id (int) – 对应于未知符号(不在 token 集合中)的 token。

  • split_by_whitespace (bool) – 如果为 False,则允许 sentencepiece 提取跨越多个词的片段。此功能对于中文/日文/韩文非常重要。(默认值:True)

  • num_sequences (int) – 如果不是 None,则最多使用这么多序列来训练分词器(适用于大型数据集)。(默认值:None)

  • annotation_list_to_check (list,) – 用于检查分词器恢复词准确性的标注文件列表。

  • annotation_format (str) – 标注文件的格式。支持 JSON 或 csv 格式。

  • text_file (str) – 文本文件的备用路径(当多个模型在同一数据文件上训练时需要)

  • add_dummy_prefix (bool) – 如果为 True,分词器会在文本开头添加一个虚拟空格。(默认值:True)

示例

>>> import torch
>>> dict_int2lab = {1: "HELLO", 2: "MORNING"}
>>> model_dir = getfixture('tmpdir') / "tokenizer_data"
>>> # Example with csv
>>> annotation_train = "tests/samples/annotation/dev-clean.csv"
>>> annotation_read = "wrd"
>>> model_type = "bpe"
>>> bpe = SentencePiece(str(model_dir), 100, annotation_train, annotation_read, model_type)
>>> batch_seq = torch.Tensor([[1, 2, 2, 1],[1, 2, 1, 0]])
>>> batch_lens = torch.Tensor([1.0, 0.75])
>>> encoded_seq_ids, encoded_seq_pieces = bpe(
...     batch_seq, batch_lens, dict_int2lab, task="encode"
... )
>>> # Example using JSON
>>> annotation_train = str(model_dir + "/dev-clean.json")
>>> annotation_read = "wrd"
>>> bpe = SentencePiece(model_dir, 100, annotation_train, annotation_read, model_type, annotation_format = 'json')
>>> encoded_seq_ids, encoded_seq_pieces = bpe(
...     batch_seq, batch_lens, dict_int2lab, task="encode"
... )
__call__(batch, batch_lens=None, ind2lab=None, task='encode')[source]

此 __call__ 函数实现了 BPE、Regularized BPE (含 unigram) 和 char (speechbrain/nnet/RNN.py) 的分词器编码器和解码器(恢复词的字符串)。 :param batch: 列表(如果 batch_lens = None 且 task = “decode_from_list”)

包含原始标签。形状:[batch_size, max_length]

参数:
  • batch_lens (tensor.LongTensor) – 包含每个标签序列的相对长度。必须是形状为 [batch_size] 的 1D 张量。(默认值:None)

  • ind2lab (dict) – 将标签序列(batch 张量)中的索引映射到字符串标签的字典。

  • task (str) –

    (“encode”, “decode”, “decode_from_list”) “encode”:将 batch 张量转换为 token 序列。

    输出包含一个 (tokens_seq, tokens_lens) 列表

    “decode”:将 token 张量转换为词序列列表。“decode_from_list”:将 token 序列列表转换为列表

    词序列的列表。

speechbrain.tokenizers.SentencePiece.get_spm_tokens(model_path)[source]

获取 token 列表,可以通过 token ID 索引

结果列表可用于将 ID 映射到 token。

参数:

model_path (str) – SentencePiece 模型的路径

返回值:

按 ID 顺序排列的 token(可通过 ID 索引)

返回类型:

list

class speechbrain.tokenizers.SentencePiece.SentencePieceDecoderStreamingContext(emitted_symbol_count: int = 0)[source]

基类: object

单个 SentencePiece 流式会话的可变流式上下文。

emitted_symbol_count: int = 0

此转录已发出的符号数量。

speechbrain.tokenizers.SentencePiece.spm_decode_preserve_leading_space(tokenizer: SentencePieceProcessor, hyps: List[int], context: SentencePieceDecoderStreamingContext) List[str][source]

假设分词器是 SentencePiece,解码输入假设,但在流式处理时避免错误地去除前导空格。操作于单个假设,而不是批量假设。

通常,分词器一次解码完整的句子,结果是解码中的第一个空格会被移除。然而,在流式处理时,我们可能正在解码话语中间部分,此时句子中间的空格不能被移除。此函数处理这种情况。

例如,如果在同一个流式上下文内,您先解码 [" how", " are"] 然后解码 [" you"],解码器通常会返回 "how areyou",而不是像此函数处理后返回的 "how are you"

参数:
  • tokenizer (sentencepiece.SentencePieceProcessor) – 用于解码的 SentencePiece 处理器。

  • hyps (list of output token hypotheses) – 要解码的 token 列表,长度 >=0

  • context (SentencePieceDecoderStreamingContext) – 用于 SentencePiece 解码器的可变流式上下文,对于同一解码流,应在多次调用之间重用。

返回值:

解码后的文本。前导空格被保留,但转录开始处除外。

返回类型:

str