File size: 7,582 Bytes
f1f433f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from abc import abstractmethod
from enum import Enum
from pathlib import Path
from typing import List, Sequence

import numpy


class BasePhoneme(object):
    """
    音素の応用クラス群の抽象基底クラス

    Attributes
    ----------
    phoneme_list : Sequence[str]
        音素のリスト
    num_phoneme : int
        音素リストの要素数
    space_phoneme : str
        読点に値する音素
    """

    phoneme_list: Sequence[str]
    num_phoneme: int
    space_phoneme: str

    def __init__(
        self,
        phoneme: str,
        start: float,
        end: float,
    ):
        self.phoneme = phoneme
        self.start = numpy.round(start, decimals=2)
        self.end = numpy.round(end, decimals=2)

    def __repr__(self):
        return f"Phoneme(phoneme='{self.phoneme}', start={self.start}, end={self.end})"

    def __eq__(self, o: object):
        return isinstance(o, BasePhoneme) and (
            self.phoneme == o.phoneme and self.start == o.start and self.end == o.end
        )

    def verify(self):
        """
        音素クラスとして、データが正しいかassertする
        """
        assert self.phoneme in self.phoneme_list, f"{self.phoneme} is not defined."

    @property
    def phoneme_id(self):
        """
        phoneme_id (phoneme list内でのindex)を取得する
        Returns
        -------
        id : int
            phoneme_idを返す
        """
        return self.phoneme_list.index(self.phoneme)

    @property
    def duration(self):
        """
        音素継続期間を取得する
        Returns
        -------
        duration : int
            音素継続期間を返す
        """
        return self.end - self.start

    @property
    def onehot(self):
        """
        phoneme listの長さ分の0埋め配列のうち、phoneme id番目がTrue(1)の配列を返す
        Returns
        -------
        onehot : numpu.ndarray
            関数内で変更された配列を返す
        """
        array = numpy.zeros(self.num_phoneme, dtype=bool)
        array[self.phoneme_id] = True
        return array

    @classmethod
    def parse(cls, s: str):
        """
        文字列をパースして音素クラスを作る
        Parameters
        ----------
        s : str
            パースしたい文字列

        Returns
        -------
        phoneme : BasePhoneme
            パース結果を用いた音素クラスを返す

        Examples
        --------
        >>> BasePhoneme.parse('1.7425000 1.9125000 o:')
        Phoneme(phoneme='o:', start=1.74, end=1.91)
        """
        words = s.split()
        return cls(
            start=float(words[0]),
            end=float(words[1]),
            phoneme=words[2],
        )

    @classmethod
    @abstractmethod
    def convert(cls, phonemes: List["BasePhoneme"]) -> List["BasePhoneme"]:
        raise NotImplementedError

    @classmethod
    def load_lab_list(cls, path: Path):
        """
        labファイルを読み込む
        Parameters
        ----------
        path : Path
            読み込みたいlabファイルのパス

        Returns
        -------
        phonemes : List[BasePhoneme]
            パース結果を用いた音素クラスを返す
        """
        phonemes = [cls.parse(s) for s in path.read_text().split("\n") if len(s) > 0]
        phonemes = cls.convert(phonemes)

        for phoneme in phonemes:
            phoneme.verify()
        return phonemes

    @classmethod
    def save_lab_list(cls, phonemes: List["BasePhoneme"], path: Path):
        """
        音素クラスのリストをlabファイル形式で保存する
        Parameters
        ----------
        phonemes : List[BasePhoneme]
            保存したい音素クラスのリスト
        path : Path
            labファイルの保存先パス
        """
        text = "\n".join(
            [
                f"{numpy.round(p.start, decimals=2):.2f}\t"
                f"{numpy.round(p.end, decimals=2):.2f}\t"
                f"{p.phoneme}"
                for p in phonemes
            ]
        )
        path.write_text(text)


class JvsPhoneme(BasePhoneme):
    """
    JVS(Japanese versatile speech)コーパスに含まれる音素群クラス

    Attributes
    ----------
    phoneme_list : Sequence[str]
        音素のリスト
    num_phoneme : int
        音素リストの要素数
    space_phoneme : str
        読点に値する音素
    """

    phoneme_list = (
        "pau",
        "I",
        "N",
        "U",
        "a",
        "b",
        "by",
        "ch",
        "cl",
        "d",
        "dy",
        "e",
        "f",
        "g",
        "gy",
        "h",
        "hy",
        "i",
        "j",
        "k",
        "ky",
        "m",
        "my",
        "n",
        "ny",
        "o",
        "p",
        "py",
        "r",
        "ry",
        "s",
        "sh",
        "t",
        "ts",
        "u",
        "v",
        "w",
        "y",
        "z",
    )
    num_phoneme = len(phoneme_list)
    space_phoneme = "pau"

    @classmethod
    def convert(cls, phonemes: List["JvsPhoneme"]) -> List["JvsPhoneme"]:
        """
        最初と最後のsil(silent)をspace_phoneme(pau)に置き換え(変換)する
        Parameters
        ----------
        phonemes : List[JvsPhoneme]
            変換したいphonemeのリスト

        Returns
        -------
        phonemes : List[JvsPhoneme]
            変換されたphonemeのリスト
        """
        if "sil" in phonemes[0].phoneme:
            phonemes[0].phoneme = cls.space_phoneme
        if "sil" in phonemes[-1].phoneme:
            phonemes[-1].phoneme = cls.space_phoneme
        return phonemes


class OjtPhoneme(BasePhoneme):
    """
    OpenJTalkに含まれる音素群クラス

    Attributes
    ----------
    phoneme_list : Sequence[str]
        音素のリスト
    num_phoneme : int
        音素リストの要素数
    space_phoneme : str
        読点に値する音素
    """

    phoneme_list = (
        "pau",
        "A",
        "E",
        "I",
        "N",
        "O",
        "U",
        "a",
        "b",
        "by",
        "ch",
        "cl",
        "d",
        "dy",
        "e",
        "f",
        "g",
        "gw",
        "gy",
        "h",
        "hy",
        "i",
        "j",
        "k",
        "kw",
        "ky",
        "m",
        "my",
        "n",
        "ny",
        "o",
        "p",
        "py",
        "r",
        "ry",
        "s",
        "sh",
        "t",
        "ts",
        "ty",
        "u",
        "v",
        "w",
        "y",
        "z",
    )
    num_phoneme = len(phoneme_list)
    space_phoneme = "pau"

    @classmethod
    def convert(cls, phonemes: List["OjtPhoneme"]):
        """
        最初と最後のsil(silent)をspace_phoneme(pau)に置き換え(変換)する
        Parameters
        ----------
        phonemes : List[OjtPhoneme]
            変換したいphonemeのリスト

        Returns
        -------
        phonemes : List[OjtPhoneme]
            変換されたphonemeのリスト
        """
        if "sil" in phonemes[0].phoneme:
            phonemes[0].phoneme = cls.space_phoneme
        if "sil" in phonemes[-1].phoneme:
            phonemes[-1].phoneme = cls.space_phoneme
        return phonemes


class PhonemeType(str, Enum):
    jvs = "jvs"
    openjtalk = "openjtalk"


phoneme_type_to_class = {
    PhonemeType.jvs: JvsPhoneme,
    PhonemeType.openjtalk: OjtPhoneme,
}