c16095bc55
* Drop support for discrete F0 embedding (reserved in ONNX exporter) * Drop support for `interp_uv` configuration key * Drop support for `train_set_name` and `valid_set_name` configuration keys * Drop support for linear domain of random time stretching augmentation * Drop support for `num_pad_tokens` configuration key * Drop support for code backup before training * Drop support for `ffn_padding` configuration key * Drop support for random seeding * Add placeholder to load old checkpoint * Remove duplicate txt_embed layer (resuming may raise errors) * Remove migration script and error message for transcriptions.txt * Remove seed from batch shuffling * Use direct access on some hparam keys * Fix duplicate keys in YAML * Rename `pndm_speedup` to `diff_speedup`
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import numpy as np
|
|
|
|
PAD = '<PAD>'
|
|
PAD_INDEX = 0
|
|
|
|
|
|
class TokenTextEncoder:
|
|
"""Encoder based on a user-supplied vocabulary (file or list)."""
|
|
|
|
def __init__(self, vocab_list):
|
|
"""Initialize from a file or list, one token per line.
|
|
|
|
Handling of reserved tokens works as follows:
|
|
- When initializing from a list, we add reserved tokens to the vocab.
|
|
|
|
Args:
|
|
vocab_list: If not None, a list of elements of the vocabulary.
|
|
"""
|
|
self.vocab_list = sorted(vocab_list)
|
|
|
|
def encode(self, sentence):
|
|
"""Converts a space-separated string of phones to a list of ids."""
|
|
phones = sentence.strip().split() if isinstance(sentence, str) else sentence
|
|
return [self.vocab_list.index(ph) + 1 if ph != PAD else PAD_INDEX for ph in phones]
|
|
|
|
def decode(self, ids, strip_padding=False):
|
|
if strip_padding:
|
|
ids = np.trim_zeros(ids)
|
|
ids = list(ids)
|
|
return ' '.join([
|
|
self.vocab_list[_id - 1] if _id >= 1 else PAD
|
|
for _id in ids
|
|
])
|
|
|
|
@property
|
|
def vocab_size(self):
|
|
return len(self.vocab_list) + 1
|
|
|
|
def __len__(self):
|
|
return self.vocab_size
|
|
|
|
def store_to_file(self, filename):
|
|
"""Write vocab file to disk.
|
|
|
|
Vocab files have one token per line. The file ends in a newline. Reserved
|
|
tokens are written to the vocab file as well.
|
|
|
|
Args:
|
|
filename: Full path of the file to store the vocab to.
|
|
"""
|
|
with open(filename, 'w', encoding='utf8') as f:
|
|
print(PAD, file=f)
|
|
[print(tok, file=f) for tok in self.vocab_list]
|