Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TensorSpeech
GitHub Repository: TensorSpeech/TensorFlowTTS
Path: blob/master/test/test_base_processor.py
1558 views
1
import pytest
2
from tensorflow_tts.processor.base_processor import BaseProcessor, DataProcessorError
3
import string
4
from dataclasses import dataclass
5
from shutil import copyfile
6
7
8
@dataclass
9
class LJ(BaseProcessor):
10
def get_one_sample(self, item):
11
sample = {
12
"raw_text": None,
13
"text_ids": None,
14
"audio": None,
15
"utt_id": None,
16
"speaker_name": None,
17
"rate": None,
18
}
19
return sample
20
21
def text_to_sequence(self, text):
22
return ["0"]
23
24
def setup_eos_token(self):
25
return None
26
27
def save_pretrained(self, saved_path):
28
return super().save_pretrained(saved_path)
29
30
31
@pytest.fixture
32
def processor(tmpdir):
33
copyfile("test/files/train.txt", f"{tmpdir}/train.txt")
34
processor = LJ(data_dir=tmpdir, symbols=list(string.ascii_lowercase))
35
return processor
36
37
38
@pytest.fixture
39
def mapper_processor(tmpdir):
40
copyfile("test/files/train.txt", f"{tmpdir}/train.txt")
41
copyfile("test/files/mapper.json", f"{tmpdir}/mapper.json")
42
processor = LJ(data_dir=tmpdir, loaded_mapper_path=f"{tmpdir}/mapper.json")
43
return processor
44
45
46
def test_items_creation(processor):
47
# Check text
48
assert processor.items[0][0] == "in fact its just a test."
49
assert processor.items[1][0] == "in fact its just a speaker number one."
50
51
# Check path
52
assert processor.items[0][1].split("/")[-1] == "libri1.wav"
53
assert processor.items[1][1].split("/")[-1] == "libri2.wav"
54
55
# Check speaker name
56
assert processor.items[0][2] == "One"
57
assert processor.items[1][2] == "Two"
58
59
60
def test_mapper(processor):
61
# check symbol to id mapper
62
assert processor.symbol_to_id["a"] == 0
63
64
# check id to symbol mapper
65
assert processor.id_to_symbol[0] == "a"
66
67
# check speaker mapper
68
assert processor.speakers_map["One"] == 0
69
assert processor.speakers_map["Two"] == 1
70
71
72
def test_adding_symbols(processor):
73
# check symbol to id mapper
74
assert processor.symbol_to_id["a"] == 0
75
76
# check id to symbol mapper
77
assert processor.id_to_symbol[0] == "a"
78
79
old_processor_len = len(processor.symbols)
80
81
# Test adding new symbol
82
processor.add_symbol("O_O")
83
84
assert processor.symbol_to_id["a"] == 0
85
assert (
86
processor.symbol_to_id["O_O"] == len(processor.symbols) - 1
87
) # new symbol should have last id
88
89
assert processor.id_to_symbol[0] == "a"
90
assert processor.id_to_symbol[len(processor.symbols) - 1] == "O_O"
91
92
assert old_processor_len == len(processor.symbols) - 1
93
94
95
def test_loading_mapper(mapper_processor):
96
assert mapper_processor.symbol_to_id["a"] == 0
97
assert mapper_processor.symbol_to_id["@ph"] == 2
98
99
assert mapper_processor.speakers_map["test_one"] == 0
100
assert mapper_processor.speakers_map["test_two"] == 1
101
102
assert mapper_processor.id_to_symbol[0] == "a"
103
assert mapper_processor.id_to_symbol[2] == "@ph"
104
105
# Test failed creation
106
with pytest.raises(DataProcessorError):
107
failed = LJ(data_dir="test/files")
108
109