Yusin commited on
Commit
2ae68b8
1 Parent(s): 4f485d9

Upload 12 files

Browse files
configs/config.json ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "train": {
3
+ "segment_size": 8192
4
+ },
5
+ "data": {
6
+ "text_cleaners":["zh_ja_mixture_cleaners"],
7
+ "max_wav_value": 32768.0,
8
+ "sampling_rate": 22050,
9
+ "filter_length": 1024,
10
+ "hop_length": 256,
11
+ "win_length": 1024,
12
+ "add_blank": true,
13
+ "n_speakers": 5
14
+ },
15
+ "model": {
16
+ "inter_channels": 192,
17
+ "hidden_channels": 192,
18
+ "filter_channels": 768,
19
+ "n_heads": 2,
20
+ "n_layers": 6,
21
+ "kernel_size": 3,
22
+ "p_dropout": 0.1,
23
+ "resblock": "1",
24
+ "resblock_kernel_sizes": [3,7,11],
25
+ "resblock_dilation_sizes": [[1,3,5], [1,3,5], [1,3,5]],
26
+ "upsample_rates": [8,8,2,2],
27
+ "upsample_initial_channel": 512,
28
+ "upsample_kernel_sizes": [16,16,4,4],
29
+ "n_layers_q": 3,
30
+ "use_spectral_norm": false,
31
+ "gin_channels": 256
32
+ },
33
+ "speakers": ["\u7dbe\u5730\u5be7\u3005", "\u5728\u539f\u4e03\u6d77", "\u5c0f\u8338", "\u5510\u4e50\u541f"],
34
+ "symbols": ["_", ",", ".", "!", "?", "-", "~", "\u2026", "A", "E", "I", "N", "O", "Q", "U", "a", "b", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "v", "w", "y", "z", "\u0283", "\u02a7", "\u02a6", "\u026f", "\u0279", "\u0259", "\u0265", "\u207c", "\u02b0", "`", "\u2192", "\u2193", "\u2191", " "]
35
+ }
configs/vits_pre.json ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "data": {
3
+ "add_blank": true,
4
+ "cleaned_text": true,
5
+ "filter_length": 1024,
6
+ "hop_length": 256,
7
+ "max_wav_value": 32768.0,
8
+ "mel_fmax": null,
9
+ "mel_fmin": 0.0,
10
+ "n_mel_channels": 80,
11
+ "n_speakers": 8,
12
+ "sampling_rate": 22050,
13
+ "text_cleaners": [
14
+ "zh_ja_mixture_cleaners"
15
+ ],
16
+ "training_files": "/root/content/vits/filelists/vits_pre_train.txt.cleaned",
17
+ "validation_files": "/root/content/vits/filelists/vits_pre_val.txt.cleaned",
18
+ "win_length": 1024
19
+ },
20
+ "model": {
21
+ "filter_channels": 768,
22
+ "gin_channels": 256,
23
+ "hidden_channels": 192,
24
+ "inter_channels": 192,
25
+ "kernel_size": 3,
26
+ "n_heads": 2,
27
+ "n_layers": 6,
28
+ "n_layers_q": 3,
29
+ "p_dropout": 0.1,
30
+ "resblock": "1",
31
+ "resblock_dilation_sizes": [
32
+ [
33
+ 1,
34
+ 3,
35
+ 5
36
+ ],
37
+ [
38
+ 1,
39
+ 3,
40
+ 5
41
+ ],
42
+ [
43
+ 1,
44
+ 3,
45
+ 5
46
+ ]
47
+ ],
48
+ "resblock_kernel_sizes": [
49
+ 3,
50
+ 7,
51
+ 11
52
+ ],
53
+ "upsample_initial_channel": 512,
54
+ "upsample_kernel_sizes": [
55
+ 16,
56
+ 16,
57
+ 4,
58
+ 4
59
+ ],
60
+ "upsample_rates": [
61
+ 8,
62
+ 8,
63
+ 2,
64
+ 2
65
+ ],
66
+ "use_spectral_norm": false
67
+ },
68
+ "speakers": [
69
+ "yilanqiu",
70
+ "xing",
71
+ "speechocean",
72
+ "jsut"
73
+ ],
74
+ "symbols": [
75
+ "_",
76
+ ",",
77
+ ".",
78
+ "!",
79
+ "?",
80
+ "-",
81
+ "~",
82
+ "\u2026",
83
+ "A",
84
+ "E",
85
+ "I",
86
+ "N",
87
+ "O",
88
+ "Q",
89
+ "U",
90
+ "a",
91
+ "b",
92
+ "d",
93
+ "e",
94
+ "f",
95
+ "g",
96
+ "h",
97
+ "i",
98
+ "j",
99
+ "k",
100
+ "l",
101
+ "m",
102
+ "n",
103
+ "o",
104
+ "p",
105
+ "r",
106
+ "s",
107
+ "t",
108
+ "u",
109
+ "v",
110
+ "w",
111
+ "y",
112
+ "z",
113
+ "\u0283",
114
+ "\u02a7",
115
+ "\u02a6",
116
+ "\u026f",
117
+ "\u0279",
118
+ "\u0259",
119
+ "\u0265",
120
+ "\u207c",
121
+ "\u02b0",
122
+ "`",
123
+ "\u2192",
124
+ "\u2193",
125
+ "\u2191",
126
+ " "
127
+ ],
128
+ "train": {
129
+ "batch_size": 32,
130
+ "betas": [
131
+ 0.8,
132
+ 0.99
133
+ ],
134
+ "c_kl": 1.0,
135
+ "c_mel": 45,
136
+ "epochs": 5000,
137
+ "eps": 1e-09,
138
+ "eval_interval": 1000,
139
+ "fp16_run": true,
140
+ "init_lr_ratio": 1,
141
+ "learning_rate": 0.0002,
142
+ "log_interval": 200,
143
+ "lr_decay": 0.999875,
144
+ "seed": 1234,
145
+ "segment_size": 8192,
146
+ "warmup_epochs": 0
147
+ }
148
+ }
jibe/dict.txt ADDED
The diff for this file is too large to render. See raw diff
 
monotonic_align/__init__.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy import zeros, int32, float32
2
+ from torch import from_numpy
3
+
4
+ from .core import maximum_path_jit
5
+
6
+ def maximum_path(neg_cent, mask):
7
+ """ numba optimized version.
8
+ neg_cent: [b, t_t, t_s]
9
+ mask: [b, t_t, t_s]
10
+ """
11
+ device = neg_cent.device
12
+ dtype = neg_cent.dtype
13
+ neg_cent = neg_cent.data.cpu().numpy().astype(float32)
14
+ path = zeros(neg_cent.shape, dtype=int32)
15
+
16
+ t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
17
+ t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
18
+ maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
19
+ return from_numpy(path).to(device=device, dtype=dtype)
monotonic_align/core.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numba
2
+
3
+
4
+ @numba.jit(numba.void(numba.int32[:,:,::1], numba.float32[:,:,::1], numba.int32[::1], numba.int32[::1]), nopython=True, nogil=True)
5
+ def maximum_path_jit(paths, values, t_ys, t_xs):
6
+ b = paths.shape[0]
7
+ max_neg_val=-1e9
8
+ for i in range(int(b)):
9
+ path = paths[i]
10
+ value = values[i]
11
+ t_y = t_ys[i]
12
+ t_x = t_xs[i]
13
+
14
+ v_prev = v_cur = 0.0
15
+ index = t_x - 1
16
+
17
+ for y in range(t_y):
18
+ for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
19
+ if x == y:
20
+ v_cur = max_neg_val
21
+ else:
22
+ v_cur = value[y-1, x]
23
+ if x == 0:
24
+ if y == 0:
25
+ v_prev = 0.
26
+ else:
27
+ v_prev = max_neg_val
28
+ else:
29
+ v_prev = value[y-1, x-1]
30
+ value[y, x] += max(v_prev, v_cur)
31
+
32
+ for y in range(t_y - 1, -1, -1):
33
+ path[y, index] = 1
34
+ if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
35
+ index = index - 1
text/LICENSE ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Copyright (c) 2017 Keith Ito
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
text/__init__.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ from https://github.com/keithito/tacotron """
2
+ from text import cleaners
3
+
4
+
5
+ def text_to_sequence(text, symbols, cleaner_names):
6
+ '''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
7
+ Args:
8
+ text: string to convert to a sequence
9
+ cleaner_names: names of the cleaner functions to run the text through
10
+ Returns:
11
+ List of integers corresponding to the symbols in the text
12
+ '''
13
+ _symbol_to_id = {s: i for i, s in enumerate(symbols)}
14
+
15
+ sequence = []
16
+
17
+ clean_text = _clean_text(text, cleaner_names)
18
+ for symbol in clean_text:
19
+ if symbol not in _symbol_to_id.keys():
20
+ continue
21
+ symbol_id = _symbol_to_id[symbol]
22
+ sequence += [symbol_id]
23
+ return sequence
24
+
25
+
26
+ def _clean_text(text, cleaner_names):
27
+ for name in cleaner_names:
28
+ cleaner = getattr(cleaners, name)
29
+ if not cleaner:
30
+ raise Exception('Unknown cleaner: %s' % name)
31
+ text = cleaner(text)
32
+ return text
text/cleaners.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+
4
+ def japanese_cleaners(text):
5
+ from text.japanese import japanese_to_romaji_with_accent
6
+ text = japanese_to_romaji_with_accent(text)
7
+ if re.match('[A-Za-z]', text[-1]):
8
+ text += '.'
9
+ return text
10
+
11
+
12
+ def japanese_cleaners2(text):
13
+ return japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
14
+
15
+
16
+ def korean_cleaners(text):
17
+ '''Pipeline for Korean text'''
18
+ from text.korean import latin_to_hangul, number_to_hangul, divide_hangul
19
+ text = latin_to_hangul(text)
20
+ text = number_to_hangul(text)
21
+ text = divide_hangul(text)
22
+ if re.match('[\u3131-\u3163]', text[-1]):
23
+ text += '.'
24
+ return text
25
+
26
+
27
+ def chinese_cleaners(text):
28
+ '''Pipeline for Chinese text'''
29
+ from text.mandarin import number_to_chinese, chinese_to_bopomofo, latin_to_bopomofo
30
+ text = number_to_chinese(text)
31
+ text = chinese_to_bopomofo(text)
32
+ text = latin_to_bopomofo(text)
33
+ if re.match('[ˉˊˇˋ˙]', text[-1]):
34
+ text += '。'
35
+ return text
36
+
37
+
38
+ def zh_ja_mixture_cleaners(text):
39
+ from text.mandarin import chinese_to_romaji
40
+ from text.japanese import japanese_to_romaji_with_accent
41
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
42
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
43
+ for chinese_text in chinese_texts:
44
+ cleaned_text = chinese_to_romaji(chinese_text[4:-4])
45
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
46
+ for japanese_text in japanese_texts:
47
+ cleaned_text = japanese_to_romaji_with_accent(
48
+ japanese_text[4:-4]).replace('ts', 'ʦ').replace('u', 'ɯ').replace('...', '…')
49
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
50
+ text = text[:-1]
51
+ if re.match('[A-Za-zɯɹəɥ→↓↑]', text[-1]):
52
+ text += '.'
53
+ return text
54
+
55
+
56
+ def sanskrit_cleaners(text):
57
+ text = text.replace('॥', '।').replace('ॐ', 'ओम्')
58
+ if text[-1] != '।':
59
+ text += ' ।'
60
+ return text
61
+
62
+
63
+ def cjks_cleaners(text):
64
+ from text.mandarin import chinese_to_lazy_ipa
65
+ from text.japanese import japanese_to_ipa
66
+ from text.korean import korean_to_lazy_ipa
67
+ from text.sanskrit import devanagari_to_ipa
68
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
69
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
70
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
71
+ sanskrit_texts = re.findall(r'\[SA\].*?\[SA\]', text)
72
+ for chinese_text in chinese_texts:
73
+ cleaned_text = chinese_to_lazy_ipa(chinese_text[4:-4])
74
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
75
+ for japanese_text in japanese_texts:
76
+ cleaned_text = japanese_to_ipa(japanese_text[4:-4])
77
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
78
+ for korean_text in korean_texts:
79
+ cleaned_text = korean_to_lazy_ipa(korean_text[4:-4])
80
+ text = text.replace(korean_text, cleaned_text+' ', 1)
81
+ for sanskrit_text in sanskrit_texts:
82
+ cleaned_text = devanagari_to_ipa(sanskrit_text[4:-4])
83
+ text = text.replace(sanskrit_text, cleaned_text+' ', 1)
84
+ text = text[:-1]
85
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
86
+ text += '.'
87
+ return text
text/japanese.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from unidecode import unidecode
3
+ import pyopenjtalk
4
+
5
+
6
+ # Regular expression matching Japanese without punctuation marks:
7
+ _japanese_characters = re.compile(
8
+ r'[A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]')
9
+
10
+ # Regular expression matching non-Japanese characters or punctuation marks:
11
+ _japanese_marks = re.compile(
12
+ r'[^A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]')
13
+
14
+ # List of (symbol, Japanese) pairs for marks:
15
+ _symbols_to_japanese = [(re.compile('%s' % x[0]), x[1]) for x in [
16
+ ('%', 'パーセント')
17
+ ]]
18
+
19
+ # List of (romaji, ipa) pairs for marks:
20
+ _romaji_to_ipa = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
21
+ ('ts', 'ʦ'),
22
+ ('u', 'ɯ'),
23
+ ('...', '…'),
24
+ ('j', 'ʥ'),
25
+ ('y', 'j'),
26
+ ('ni', 'n^i'),
27
+ ('nj', 'n^'),
28
+ ('hi', 'çi'),
29
+ ('hj', 'ç'),
30
+ ('f', 'ɸ'),
31
+ ('I', 'i*'),
32
+ ('U', 'ɯ*'),
33
+ ('r', 'ɾ')
34
+ ]]
35
+
36
+ # Dictinary of (consonant, sokuon) pairs:
37
+ _real_sokuon = {
38
+ 'k': 'k#',
39
+ 'g': 'k#',
40
+ 't': 't#',
41
+ 'd': 't#',
42
+ 'ʦ': 't#',
43
+ 'ʧ': 't#',
44
+ 'ʥ': 't#',
45
+ 'j': 't#',
46
+ 's': 's',
47
+ 'ʃ': 's',
48
+ 'p': 'p#',
49
+ 'b': 'p#'
50
+ }
51
+
52
+ # Dictinary of (consonant, hatsuon) pairs:
53
+ _real_hatsuon = {
54
+ 'p': 'm',
55
+ 'b': 'm',
56
+ 'm': 'm',
57
+ 't': 'n',
58
+ 'd': 'n',
59
+ 'n': 'n',
60
+ 'ʧ': 'n^',
61
+ 'ʥ': 'n^',
62
+ 'k': 'ŋ',
63
+ 'g': 'ŋ'
64
+ }
65
+
66
+
67
+ def symbols_to_japanese(text):
68
+ for regex, replacement in _symbols_to_japanese:
69
+ text = re.sub(regex, replacement, text)
70
+ return text
71
+
72
+
73
+ def japanese_to_romaji_with_accent(text):
74
+ '''Reference https://r9y9.github.io/ttslearn/latest/notebooks/ch10_Recipe-Tacotron.html'''
75
+ text = symbols_to_japanese(text)
76
+ sentences = re.split(_japanese_marks, text)
77
+ marks = re.findall(_japanese_marks, text)
78
+ text = ''
79
+ for i, sentence in enumerate(sentences):
80
+ if re.match(_japanese_characters, sentence):
81
+ if text != '':
82
+ text += ' '
83
+ labels = pyopenjtalk.extract_fullcontext(sentence)
84
+ for n, label in enumerate(labels):
85
+ phoneme = re.search(r'\-([^\+]*)\+', label).group(1)
86
+ if phoneme not in ['sil', 'pau']:
87
+ text += phoneme.replace('ch', 'ʧ').replace('sh',
88
+ 'ʃ').replace('cl', 'Q')
89
+ else:
90
+ continue
91
+ # n_moras = int(re.search(r'/F:(\d+)_', label).group(1))
92
+ a1 = int(re.search(r"/A:(\-?[0-9]+)\+", label).group(1))
93
+ a2 = int(re.search(r"\+(\d+)\+", label).group(1))
94
+ a3 = int(re.search(r"\+(\d+)/", label).group(1))
95
+ if re.search(r'\-([^\+]*)\+', labels[n + 1]).group(1) in ['sil', 'pau']:
96
+ a2_next = -1
97
+ else:
98
+ a2_next = int(
99
+ re.search(r"\+(\d+)\+", labels[n + 1]).group(1))
100
+ # Accent phrase boundary
101
+ if a3 == 1 and a2_next == 1:
102
+ text += ' '
103
+ # Falling
104
+ elif a1 == 0 and a2_next == a2 + 1:
105
+ text += '↓'
106
+ # Rising
107
+ elif a2 == 1 and a2_next == 2:
108
+ text += '↑'
109
+ if i < len(marks):
110
+ text += unidecode(marks[i]).replace(' ', '')
111
+ return text
112
+
113
+
114
+ def get_real_sokuon(text):
115
+ text=re.sub('Q[↑↓]*(.)',lambda x:_real_sokuon[x.group(1)]+x.group(0)[1:] if x.group(1) in _real_sokuon.keys() else x.group(0),text)
116
+ return text
117
+
118
+
119
+ def get_real_hatsuon(text):
120
+ text=re.sub('N[↑↓]*(.)',lambda x:_real_hatsuon[x.group(1)]+x.group(0)[1:] if x.group(1) in _real_hatsuon.keys() else x.group(0),text)
121
+ return text
122
+
123
+
124
+ def japanese_to_ipa(text):
125
+ text=japanese_to_romaji_with_accent(text)
126
+ for regex, replacement in _romaji_to_ipa:
127
+ text = re.sub(regex, replacement, text)
128
+ text = re.sub(
129
+ r'([A-Za-zɯ])\1+', lambda x: x.group(0)[0]+'ː'*(len(x.group(0))-1), text)
130
+ text = get_real_sokuon(text)
131
+ text = get_real_hatsuon(text)
132
+ return text
text/korean.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from jamo import h2j, j2hcj
3
+ import ko_pron
4
+
5
+
6
+ # This is a list of Korean classifiers preceded by pure Korean numerals.
7
+ _korean_classifiers = '군데 권 개 그루 닢 대 두 마리 모 모금 뭇 발 발짝 방 번 벌 보루 살 수 술 시 쌈 움큼 정 짝 채 척 첩 축 켤레 톨 통'
8
+
9
+ # List of (hangul, hangul divided) pairs:
10
+ _hangul_divided = [(re.compile('%s' % x[0]), x[1]) for x in [
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
+ # List of (Latin alphabet, hangul) pairs:
38
+ _latin_to_hangul = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
39
+ ('a', '에이'),
40
+ ('b', '비'),
41
+ ('c', '시'),
42
+ ('d', '디'),
43
+ ('e', '이'),
44
+ ('f', '에프'),
45
+ ('g', '지'),
46
+ ('h', '에이치'),
47
+ ('i', '아이'),
48
+ ('j', '제이'),
49
+ ('k', '케이'),
50
+ ('l', '엘'),
51
+ ('m', '엠'),
52
+ ('n', '엔'),
53
+ ('o', '오'),
54
+ ('p', '피'),
55
+ ('q', '큐'),
56
+ ('r', '아르'),
57
+ ('s', '에스'),
58
+ ('t', '티'),
59
+ ('u', '유'),
60
+ ('v', '브이'),
61
+ ('w', '더블유'),
62
+ ('x', '엑스'),
63
+ ('y', '와이'),
64
+ ('z', '제트')
65
+ ]]
66
+
67
+ # List of (ipa, lazy ipa) pairs:
68
+ _ipa_to_lazy_ipa = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
69
+ ('t͡ɕ','ʧ'),
70
+ ('d͡ʑ','ʥ'),
71
+ ('ɲ','n^'),
72
+ ('ɕ','ʃ'),
73
+ ('ʷ','w'),
74
+ ('ɭ','l`'),
75
+ ('ʎ','ɾ'),
76
+ ('ɣ','ŋ'),
77
+ ('ɰ','ɯ'),
78
+ ('ʝ','j'),
79
+ ('ʌ','ə'),
80
+ ('ɡ','g'),
81
+ ('\u031a','#'),
82
+ ('\u0348','='),
83
+ ('\u031e',''),
84
+ ('\u0320',''),
85
+ ('\u0339','')
86
+ ]]
87
+
88
+
89
+ def latin_to_hangul(text):
90
+ for regex, replacement in _latin_to_hangul:
91
+ text = re.sub(regex, replacement, text)
92
+ return text
93
+
94
+
95
+ def divide_hangul(text):
96
+ text = j2hcj(h2j(text))
97
+ for regex, replacement in _hangul_divided:
98
+ text = re.sub(regex, replacement, text)
99
+ return text
100
+
101
+
102
+ def hangul_number(num, sino=True):
103
+ '''Reference https://github.com/Kyubyong/g2pK'''
104
+ num = re.sub(',', '', num)
105
+
106
+ if num == '0':
107
+ return '영'
108
+ if not sino and num == '20':
109
+ return '스무'
110
+
111
+ digits = '123456789'
112
+ names = '일이삼사오육칠팔구'
113
+ digit2name = {d: n for d, n in zip(digits, names)}
114
+
115
+ modifiers = '한 두 세 네 다섯 여섯 일곱 여덟 아홉'
116
+ decimals = '열 스물 서른 마흔 쉰 예순 일흔 여든 아흔'
117
+ digit2mod = {d: mod for d, mod in zip(digits, modifiers.split())}
118
+ digit2dec = {d: dec for d, dec in zip(digits, decimals.split())}
119
+
120
+ spelledout = []
121
+ for i, digit in enumerate(num):
122
+ i = len(num) - i - 1
123
+ if sino:
124
+ if i == 0:
125
+ name = digit2name.get(digit, '')
126
+ elif i == 1:
127
+ name = digit2name.get(digit, '') + '십'
128
+ name = name.replace('일십', '십')
129
+ else:
130
+ if i == 0:
131
+ name = digit2mod.get(digit, '')
132
+ elif i == 1:
133
+ name = digit2dec.get(digit, '')
134
+ if digit == '0':
135
+ if i % 4 == 0:
136
+ last_three = spelledout[-min(3, len(spelledout)):]
137
+ if ''.join(last_three) == '':
138
+ spelledout.append('')
139
+ continue
140
+ else:
141
+ spelledout.append('')
142
+ continue
143
+ if i == 2:
144
+ name = digit2name.get(digit, '') + '백'
145
+ name = name.replace('일백', '백')
146
+ elif i == 3:
147
+ name = digit2name.get(digit, '') + '천'
148
+ name = name.replace('일천', '천')
149
+ elif i == 4:
150
+ name = digit2name.get(digit, '') + '만'
151
+ name = name.replace('일만', '만')
152
+ elif i == 5:
153
+ name = digit2name.get(digit, '') + '십'
154
+ name = name.replace('일십', '십')
155
+ elif i == 6:
156
+ name = digit2name.get(digit, '') + '백'
157
+ name = name.replace('일백', '백')
158
+ elif i == 7:
159
+ name = digit2name.get(digit, '') + '천'
160
+ name = name.replace('일천', '천')
161
+ elif i == 8:
162
+ name = digit2name.get(digit, '') + '억'
163
+ elif i == 9:
164
+ name = digit2name.get(digit, '') + '십'
165
+ elif i == 10:
166
+ name = digit2name.get(digit, '') + '백'
167
+ elif i == 11:
168
+ name = digit2name.get(digit, '') + '천'
169
+ elif i == 12:
170
+ name = digit2name.get(digit, '') + '조'
171
+ elif i == 13:
172
+ name = digit2name.get(digit, '') + '십'
173
+ elif i == 14:
174
+ name = digit2name.get(digit, '') + '백'
175
+ elif i == 15:
176
+ name = digit2name.get(digit, '') + '천'
177
+ spelledout.append(name)
178
+ return ''.join(elem for elem in spelledout)
179
+
180
+
181
+ def number_to_hangul(text):
182
+ '''Reference https://github.com/Kyubyong/g2pK'''
183
+ tokens = set(re.findall(r'(\d[\d,]*)([\uac00-\ud71f]+)', text))
184
+ for token in tokens:
185
+ num, classifier = token
186
+ if classifier[:2] in _korean_classifiers or classifier[0] in _korean_classifiers:
187
+ spelledout = hangul_number(num, sino=False)
188
+ else:
189
+ spelledout = hangul_number(num, sino=True)
190
+ text = text.replace(f'{num}{classifier}', f'{spelledout}{classifier}')
191
+ # digit by digit for remaining digits
192
+ digits = '0123456789'
193
+ names = '영일이삼사오육칠팔구'
194
+ for d, n in zip(digits, names):
195
+ text = text.replace(d, n)
196
+ return text
197
+
198
+
199
+ def korean_to_lazy_ipa(text):
200
+ text = latin_to_hangul(text)
201
+ text = number_to_hangul(text)
202
+ text=re.sub('[\uac00-\ud7af]+',lambda x:ko_pron.romanise(x.group(0),'ipa'),text).split('] ~ [')[0]
203
+ for regex, replacement in _ipa_to_lazy_ipa:
204
+ text = re.sub(regex, replacement, text)
205
+ return text
text/mandarin.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import sys
4
+
5
+ import jieba
6
+ import cn2an
7
+ import logging
8
+ from pypinyin import lazy_pinyin, BOPOMOFO
9
+
10
+ # logging.getLogger('jieba').setLevel(logging.WARNING)
11
+ # jieba.set_dictionary(os.path.dirname(sys.argv[0]) + '/jieba/dict.txt')
12
+
13
+ # List of (Latin alphabet, bopomofo) pairs:
14
+ _latin_to_bopomofo = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
15
+ ('a', 'ㄟˉ'),
16
+ ('b', 'ㄅㄧˋ'),
17
+ ('c', 'ㄙㄧˉ'),
18
+ ('d', 'ㄉㄧˋ'),
19
+ ('e', 'ㄧˋ'),
20
+ ('f', 'ㄝˊㄈㄨˋ'),
21
+ ('g', 'ㄐㄧˋ'),
22
+ ('h', 'ㄝˇㄑㄩˋ'),
23
+ ('i', 'ㄞˋ'),
24
+ ('j', 'ㄐㄟˋ'),
25
+ ('k', 'ㄎㄟˋ'),
26
+ ('l', 'ㄝˊㄛˋ'),
27
+ ('m', 'ㄝˊㄇㄨˋ'),
28
+ ('n', 'ㄣˉ'),
29
+ ('o', 'ㄡˉ'),
30
+ ('p', 'ㄆㄧˉ'),
31
+ ('q', 'ㄎㄧㄡˉ'),
32
+ ('r', 'ㄚˋ'),
33
+ ('s', 'ㄝˊㄙˋ'),
34
+ ('t', 'ㄊㄧˋ'),
35
+ ('u', 'ㄧㄡˉ'),
36
+ ('v', 'ㄨㄧˉ'),
37
+ ('w', 'ㄉㄚˋㄅㄨˋㄌㄧㄡˋ'),
38
+ ('x', 'ㄝˉㄎㄨˋㄙˋ'),
39
+ ('y', 'ㄨㄞˋ'),
40
+ ('z', 'ㄗㄟˋ')
41
+ ]]
42
+
43
+ # List of (bopomofo, romaji) pairs:
44
+ _bopomofo_to_romaji = [(re.compile('%s' % x[0]), x[1]) for x in [
45
+ ('ㄅㄛ', 'p⁼wo'),
46
+ ('ㄆㄛ', 'pʰwo'),
47
+ ('ㄇㄛ', 'mwo'),
48
+ ('ㄈㄛ', 'fwo'),
49
+ ('ㄅ', 'p⁼'),
50
+ ('ㄆ', 'pʰ'),
51
+ ('ㄇ', 'm'),
52
+ ('ㄈ', 'f'),
53
+ ('ㄉ', 't⁼'),
54
+ ('ㄊ', 'tʰ'),
55
+ ('ㄋ', 'n'),
56
+ ('ㄌ', 'l'),
57
+ ('ㄍ', 'k⁼'),
58
+ ('ㄎ', 'kʰ'),
59
+ ('ㄏ', 'h'),
60
+ ('ㄐ', 'ʧ⁼'),
61
+ ('ㄑ', 'ʧʰ'),
62
+ ('ㄒ', 'ʃ'),
63
+ ('ㄓ', 'ʦ`⁼'),
64
+ ('ㄔ', 'ʦ`ʰ'),
65
+ ('ㄕ', 's`'),
66
+ ('ㄖ', 'ɹ`'),
67
+ ('ㄗ', 'ʦ⁼'),
68
+ ('ㄘ', 'ʦʰ'),
69
+ ('ㄙ', 's'),
70
+ ('ㄚ', 'a'),
71
+ ('ㄛ', 'o'),
72
+ ('ㄜ', 'ə'),
73
+ ('ㄝ', 'e'),
74
+ ('ㄞ', 'ai'),
75
+ ('ㄟ', 'ei'),
76
+ ('ㄠ', 'au'),
77
+ ('ㄡ', 'ou'),
78
+ ('ㄧㄢ', 'yeNN'),
79
+ ('ㄢ', 'aNN'),
80
+ ('ㄧㄣ', 'iNN'),
81
+ ('ㄣ', 'əNN'),
82
+ ('ㄤ', 'aNg'),
83
+ ('ㄧㄥ', 'iNg'),
84
+ ('ㄨㄥ', 'uNg'),
85
+ ('ㄩㄥ', 'yuNg'),
86
+ ('ㄥ', 'əNg'),
87
+ ('ㄦ', 'əɻ'),
88
+ ('ㄧ', 'i'),
89
+ ('ㄨ', 'u'),
90
+ ('ㄩ', 'ɥ'),
91
+ ('ˉ', '→'),
92
+ ('ˊ', '↑'),
93
+ ('ˇ', '↓↑'),
94
+ ('ˋ', '↓'),
95
+ ('˙', ''),
96
+ (',', ','),
97
+ ('。', '.'),
98
+ ('!', '!'),
99
+ ('?', '?'),
100
+ ('—', '-')
101
+ ]]
102
+
103
+ # List of (romaji, ipa) pairs:
104
+ _romaji_to_ipa = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
105
+ ('ʃy', 'ʃ'),
106
+ ('ʧʰy', 'ʧʰ'),
107
+ ('ʧ⁼y', 'ʧ⁼'),
108
+ ('NN', 'n'),
109
+ ('Ng', 'ŋ'),
110
+ ('y', 'j'),
111
+ ('h', 'x')
112
+ ]]
113
+
114
+
115
+ def number_to_chinese(text):
116
+ numbers = re.findall(r'\d+(?:\.?\d+)?', text)
117
+ for number in numbers:
118
+ text = text.replace(number, cn2an.an2cn(number), 1)
119
+ return text
120
+
121
+
122
+ def chinese_to_bopomofo(text):
123
+ text = text.replace('、', ',').replace(';', ',').replace(':', ',')
124
+ words = jieba.lcut(text, cut_all=False)
125
+ text = ''
126
+ for word in words:
127
+ bopomofos = lazy_pinyin(word, BOPOMOFO)
128
+ if not re.search('[\u4e00-\u9fff]', word):
129
+ text += word
130
+ continue
131
+ for i in range(len(bopomofos)):
132
+ if re.match('[\u3105-\u3129]', bopomofos[i][-1]):
133
+ bopomofos[i] += 'ˉ'
134
+ if text != '':
135
+ text += ' '
136
+ text += ''.join(bopomofos)
137
+ return text
138
+
139
+
140
+ def latin_to_bopomofo(text):
141
+ for regex, replacement in _latin_to_bopomofo:
142
+ text = re.sub(regex, replacement, text)
143
+ return text
144
+
145
+
146
+ def bopomofo_to_romaji(text):
147
+ for regex, replacement in _bopomofo_to_romaji:
148
+ text = re.sub(regex, replacement, text)
149
+ return text
150
+
151
+
152
+ def chinese_to_romaji(text):
153
+ text = number_to_chinese(text)
154
+ text = chinese_to_bopomofo(text)
155
+ text = latin_to_bopomofo(text)
156
+ text = bopomofo_to_romaji(text)
157
+ text = re.sub('i[aoe]', lambda x: 'y' + x.group(0)[1:], text)
158
+ text = re.sub('u[aoəe]', lambda x: 'w' + x.group(0)[1:], text)
159
+ text = re.sub('([ʦsɹ]`[⁼ʰ]?)([→↓↑ ]+|$)', lambda x: x.group(1) +
160
+ 'ɹ`' + x.group(2), text).replace('ɻ', 'ɹ`')
161
+ text = re.sub('([ʦs][⁼ʰ]?)([→↓↑ ]+|$)',
162
+ lambda x: x.group(1) + 'ɹ' + x.group(2), text)
163
+ return text
164
+
165
+
166
+ def chinese_to_lazy_ipa(text):
167
+ text = chinese_to_romaji(text)
168
+ for regex, replacement in _romaji_to_ipa:
169
+ text = re.sub(regex, replacement, text)
170
+ return text
text/sanskrit.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from indic_transliteration import sanscript
3
+
4
+
5
+ # List of (iast, ipa) pairs:
6
+ _iast_to_ipa = [(re.compile('%s' % x[0]), x[1]) for x in [
7
+ ('a', 'ə'),
8
+ ('ā', 'aː'),
9
+ ('ī', 'iː'),
10
+ ('ū', 'uː'),
11
+ ('ṛ', 'ɹ`'),
12
+ ('ṝ', 'ɹ`ː'),
13
+ ('ḷ', 'l`'),
14
+ ('ḹ', 'l`ː'),
15
+ ('e', 'eː'),
16
+ ('o', 'oː'),
17
+ ('k', 'k⁼'),
18
+ ('k⁼h', 'kʰ'),
19
+ ('g', 'g⁼'),
20
+ ('g⁼h', 'gʰ'),
21
+ ('ṅ', 'ŋ'),
22
+ ('c', 'ʧ⁼'),
23
+ ('ʧ⁼h', 'ʧʰ'),
24
+ ('j', 'ʥ⁼'),
25
+ ('ʥ⁼h', 'ʥʰ'),
26
+ ('ñ', 'n^'),
27
+ ('ṭ', 't`⁼'),
28
+ ('t`⁼h', 't`ʰ'),
29
+ ('ḍ', 'd`⁼'),
30
+ ('d`⁼h', 'd`ʰ'),
31
+ ('ṇ', 'n`'),
32
+ ('t', 't⁼'),
33
+ ('t⁼h', 'tʰ'),
34
+ ('d', 'd⁼'),
35
+ ('d⁼h', 'dʰ'),
36
+ ('p', 'p⁼'),
37
+ ('p⁼h', 'pʰ'),
38
+ ('b', 'b⁼'),
39
+ ('b⁼h', 'bʰ'),
40
+ ('y', 'j'),
41
+ ('ś', 'ʃ'),
42
+ ('ṣ', 's`'),
43
+ ('r', 'ɾ'),
44
+ ('l̤', 'l`'),
45
+ ('h', 'ɦ'),
46
+ ("'", ''),
47
+ ('~', '^'),
48
+ ('ṃ', '^')
49
+ ]]
50
+
51
+
52
+ def devanagari_to_ipa(text):
53
+ text = text.replace('ॐ', 'ओम्')
54
+ text = re.sub(r'\s*।\s*$', '.', text)
55
+ text = re.sub(r'\s*।\s*', ', ', text)
56
+ text = re.sub(r'\s*॥', '.', text)
57
+ text = sanscript.transliterate(text, sanscript.DEVANAGARI, sanscript.IAST)
58
+ for regex, replacement in _iast_to_ipa:
59
+ text = re.sub(regex, replacement, text)
60
+ text = re.sub('(.)[`ː]*ḥ', lambda x: x.group(0)
61
+ [:-1]+'h'+x.group(1)+'*', text)
62
+ return text