# MIT License
#
# Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
from dataclasses import dataclass, field
from omegaconf import DictConfig
from openspeech.dataclass.configurations import VocabularyConfigs
from openspeech.datasets.librispeech.preprocess.subword import SENTENCEPIECE_MODEL_NAME
from openspeech.utils import SENTENCEPIECE_IMPORT_ERROR
from openspeech.vocabs import register_vocab
from openspeech.vocabs.vocab import Vocabulary
[docs]@dataclass
class LibriSpeechSubwordVocabConfigs(VocabularyConfigs):
unit: str = field(
default="libri_subword", metadata={"help": "Unit of vocabulary."}
)
sos_token: str = field(
default="<s>", metadata={"help": "Start of sentence token"}
)
eos_token: str = field(
default="</s>", metadata={"help": "End of sentence token"}
)
vocab_size: int = field(
default=5000, metadata={"help": "Size of vocabulary."}
)
vocab_path: str = field(
default="../../../LibriSpeech/", metadata={"help": "Path of vocabulary file."}
)
[docs]@register_vocab("libri_subword", dataclass=LibriSpeechSubwordVocabConfigs)
class LibriSpeechSubwordVocabulary(Vocabulary):
"""
Converts label to string for librispeech dataset.
Args:
configs (DictConfig): configuration set.
"""
def __init__(self, configs: DictConfig):
super(LibriSpeechSubwordVocabulary, self).__init__()
try:
import sentencepiece as spm
except ImportError:
raise ImportError(SENTENCEPIECE_IMPORT_ERROR)
self.sp = spm.SentencePieceProcessor()
self.sp.Load(os.path.join(configs.vocab.vocab_path, f"{SENTENCEPIECE_MODEL_NAME}.model"))
self.pad_id = self.sp.PieceToId(configs.vocab.pad_token)
self.sos_id = self.sp.PieceToId(configs.vocab.sos_token)
self.eos_id = self.sp.PieceToId(configs.vocab.eos_token)
self.blank_id = self.sp.PieceToId(configs.vocab.blank_token)
self.vocab_size = configs.vocab.vocab_size
def __len__(self):
return self.vocab_size
def label_to_string(self, labels):
if len(labels.shape) == 1:
return self.sp.DecodeIds([l.item() for l in labels])
elif len(labels.shape) == 2:
sentences = list()
for label in labels:
sentence = self.sp.DecodeIds([l.item() for l in label])
sentences.append(sentence)
return sentences
else:
raise ValueError("Unsupported label's shape")