speechbrain.tokenizers.SentencePiece 模块

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

  • 阿卜杜勒瓦哈布·赫巴 2020

  • 洛伦·卢戈斯奇 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)的词汇表大小。对于字符分词器,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: 令牌的最大字符数。(默认: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,则bos_id = unk_id = 0。否则,bos_id = int。(默认:-1):type eos_id: int :param pad_id: 如果为-1,则pad_id = unk_id = 0。否则,bos_id = int。(默认:-1):type pad_id: int :param unk_id: 对应于未知符号(不在令牌集中)的令牌。: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

获取令牌列表,可以通过令牌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)、 一元语言模型和字符/词分词器。 :param model_dir: 模型将保存(或已经存储)的目录。 :type model_dir: str :param vocab_size: 所选分词器类型(BPE, Unigram)的词汇表大小。

对于字符,vocab_size 是可选的,而对于 BPE 和 unigram 分词则是必需的。

Parameters:
  • 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) – 令牌的最大字符数。(默认值:10)

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

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

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

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

  • 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)

Example

>>> 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、正则化 BPE(带有 unigram)和字符(speechbrain/nnet/RNN.py)的分词器编码器和解码器(恢复单词字符串)。 :param batch: 如果 ( batch_lens = None 且 task = “decode_from_list”) 则为列表

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

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

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

  • 任务 (str) –

    (“encode”, “decode”, “decode_from_list) “encode”: 将批量张量转换为标记序列。

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

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

    单词序列列表。

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

获取令牌列表,可以通过令牌ID进行索引

生成的列表可用于将id映射到token。

Parameters:

model_path (str) – SentencePiece模型的路径

Returns:

按id排序的Tokens(可以通过id索引)

Return type:

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" 像这个函数所做的那样。

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

  • hyps (list of 输出标记假设) – 要解码的标记列表,长度可以是任意 >=0

  • context (SentencePieceDecoderStreamingContext) – 用于句子片段解码器的可变流式上下文,应在同一解码流的多次调用中重复使用。

Returns:

解码后的文本。除了在转录开始时,前导空格会被保留。

Return type:

str