AriaMei commited on
Commit
01b13dd
1 Parent(s): 991e874
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,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ from https://github.com/keithito/tacotron """
2
+ from text import cleaners
3
+ from text.symbols import symbols
4
+
5
+
6
+ # Mappings from symbol to numeric ID and vice versa:
7
+ _symbol_to_id = {s: i for i, s in enumerate(symbols)}
8
+ _id_to_symbol = {i: s for i, s in enumerate(symbols)}
9
+
10
+
11
+ def text_to_sequence(text, cleaner_names):
12
+ '''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
13
+ Args:
14
+ text: string to convert to a sequence
15
+ cleaner_names: names of the cleaner functions to run the text through
16
+ Returns:
17
+ List of integers corresponding to the symbols in the text
18
+ '''
19
+ sequence = []
20
+
21
+ clean_text = _clean_text(text, cleaner_names)
22
+ for symbol in clean_text:
23
+ if symbol not in _symbol_to_id.keys():
24
+ continue
25
+ symbol_id = _symbol_to_id[symbol]
26
+ sequence += [symbol_id]
27
+ return sequence
28
+
29
+
30
+ def cleaned_text_to_sequence(cleaned_text):
31
+ '''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
32
+ Args:
33
+ text: string to convert to a sequence
34
+ Returns:
35
+ List of integers corresponding to the symbols in the text
36
+ '''
37
+ sequence = [_symbol_to_id[symbol] for symbol in cleaned_text if symbol in _symbol_to_id.keys()]
38
+ return sequence
39
+
40
+
41
+ def sequence_to_text(sequence):
42
+ '''Converts a sequence of IDs back to a string'''
43
+ result = ''
44
+ for symbol_id in sequence:
45
+ s = _id_to_symbol[symbol_id]
46
+ result += s
47
+ return result
48
+
49
+
50
+ def _clean_text(text, cleaner_names):
51
+ for name in cleaner_names:
52
+ cleaner = getattr(cleaners, name)
53
+ if not cleaner:
54
+ raise Exception('Unknown cleaner: %s' % name)
55
+ text = cleaner(text)
56
+ return text
text/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (2.12 kB). View file
 
text/__pycache__/cleaners.cpython-39.pyc ADDED
Binary file (6.29 kB). View file
 
text/__pycache__/japanese.cpython-39.pyc ADDED
Binary file (4.42 kB). View file
 
text/__pycache__/mandarin.cpython-39.pyc ADDED
Binary file (6.4 kB). View file
 
text/__pycache__/symbols.cpython-39.pyc ADDED
Binary file (394 Bytes). View file
 
text/cleaners.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ from text.japanese import japanese_to_romaji_with_accent, japanese_to_ipa, japanese_to_ipa2, japanese_to_ipa3
4
+ from text.mandarin import number_to_chinese, chinese_to_bopomofo, latin_to_bopomofo, chinese_to_romaji, chinese_to_lazy_ipa, chinese_to_ipa, chinese_to_ipa2
5
+
6
+ # from text.sanskrit import devanagari_to_ipa
7
+ # from text.english import english_to_lazy_ipa, english_to_ipa2, english_to_lazy_ipa2
8
+ # from text.thai import num_to_thai, latin_to_thai
9
+ # from text.shanghainese import shanghainese_to_ipa
10
+ # from text.cantonese import cantonese_to_ipa
11
+ # from text.ngu_dialect import ngu_dialect_to_ipa
12
+
13
+
14
+ def japanese_cleaners(text):
15
+ text = japanese_to_romaji_with_accent(text)
16
+ if re.match('[A-Za-z]', text[-1]):
17
+ text += '.'
18
+ return text
19
+
20
+
21
+ def japanese_cleaners2(text):
22
+ return japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
23
+
24
+
25
+ def korean_cleaners(text):
26
+ '''Pipeline for Korean text'''
27
+ text = latin_to_hangul(text)
28
+ text = number_to_hangul(text)
29
+ text = divide_hangul(text)
30
+ if re.match('[\u3131-\u3163]', text[-1]):
31
+ text += '.'
32
+ return text
33
+
34
+
35
+ def chinese_cleaners(text):
36
+ '''Pipeline for Chinese text'''
37
+ text = number_to_chinese(text)
38
+ text = chinese_to_bopomofo(text)
39
+ text = latin_to_bopomofo(text)
40
+ if re.match('[ˉˊˇˋ˙]', text[-1]):
41
+ text += '。'
42
+ return text
43
+
44
+
45
+ def zh_ja_mixture_cleaners(text):
46
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
47
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
48
+ for chinese_text in chinese_texts:
49
+ cleaned_text = chinese_to_romaji(chinese_text[4:-4])
50
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
51
+ for japanese_text in japanese_texts:
52
+ cleaned_text = japanese_to_romaji_with_accent(
53
+ japanese_text[4:-4]).replace('ts', 'ʦ').replace('u', 'ɯ').replace('...', '…')
54
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
55
+ text = text[:-1]
56
+ if re.match('[A-Za-zɯɹəɥ→↓↑]', text[-1]):
57
+ text += '.'
58
+ return text
59
+
60
+
61
+ def sanskrit_cleaners(text):
62
+ text = text.replace('॥', '।').replace('ॐ', 'ओम्')
63
+ if text[-1] != '।':
64
+ text += ' ।'
65
+ return text
66
+
67
+
68
+ def cjks_cleaners(text):
69
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
70
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
71
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
72
+ sanskrit_texts = re.findall(r'\[SA\].*?\[SA\]', text)
73
+ english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
74
+ for chinese_text in chinese_texts:
75
+ cleaned_text = chinese_to_lazy_ipa(chinese_text[4:-4])
76
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
77
+ for japanese_text in japanese_texts:
78
+ cleaned_text = japanese_to_ipa(japanese_text[4:-4])
79
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
80
+ for korean_text in korean_texts:
81
+ cleaned_text = korean_to_lazy_ipa(korean_text[4:-4])
82
+ text = text.replace(korean_text, cleaned_text+' ', 1)
83
+ for sanskrit_text in sanskrit_texts:
84
+ cleaned_text = devanagari_to_ipa(sanskrit_text[4:-4])
85
+ text = text.replace(sanskrit_text, cleaned_text+' ', 1)
86
+ for english_text in english_texts:
87
+ cleaned_text = english_to_lazy_ipa(english_text[4:-4])
88
+ text = text.replace(english_text, cleaned_text+' ', 1)
89
+ text = text[:-1]
90
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
91
+ text += '.'
92
+ return text
93
+
94
+
95
+ def cjke_cleaners(text):
96
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
97
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
98
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
99
+ english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
100
+ for chinese_text in chinese_texts:
101
+ cleaned_text = chinese_to_lazy_ipa(chinese_text[4:-4])
102
+ cleaned_text = cleaned_text.replace(
103
+ 'ʧ', 'tʃ').replace('ʦ', 'ts').replace('ɥan', 'ɥæn')
104
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
105
+ for japanese_text in japanese_texts:
106
+ cleaned_text = japanese_to_ipa(japanese_text[4:-4])
107
+ cleaned_text = cleaned_text.replace('ʧ', 'tʃ').replace(
108
+ 'ʦ', 'ts').replace('ɥan', 'ɥæn').replace('ʥ', 'dz')
109
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
110
+ for korean_text in korean_texts:
111
+ cleaned_text = korean_to_ipa(korean_text[4:-4])
112
+ text = text.replace(korean_text, cleaned_text+' ', 1)
113
+ for english_text in english_texts:
114
+ cleaned_text = english_to_ipa2(english_text[4:-4])
115
+ cleaned_text = cleaned_text.replace('ɑ', 'a').replace(
116
+ 'ɔ', 'o').replace('ɛ', 'e').replace('ɪ', 'i').replace('ʊ', 'u')
117
+ text = text.replace(english_text, cleaned_text+' ', 1)
118
+ text = text[:-1]
119
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
120
+ text += '.'
121
+ return text
122
+
123
+
124
+ def cjke_cleaners2(text):
125
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
126
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
127
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
128
+ english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
129
+ for chinese_text in chinese_texts:
130
+ cleaned_text = chinese_to_ipa(chinese_text[4:-4])
131
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
132
+ for japanese_text in japanese_texts:
133
+ cleaned_text = japanese_to_ipa2(japanese_text[4:-4])
134
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
135
+ for korean_text in korean_texts:
136
+ cleaned_text = korean_to_ipa(korean_text[4:-4])
137
+ text = text.replace(korean_text, cleaned_text+' ', 1)
138
+ for english_text in english_texts:
139
+ cleaned_text = english_to_ipa2(english_text[4:-4])
140
+ text = text.replace(english_text, cleaned_text+' ', 1)
141
+ text = text[:-1]
142
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
143
+ text += '.'
144
+ return text
145
+
146
+
147
+ def thai_cleaners(text):
148
+ text = num_to_thai(text)
149
+ text = latin_to_thai(text)
150
+ return text
151
+
152
+
153
+ def shanghainese_cleaners(text):
154
+ text = shanghainese_to_ipa(text)
155
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
156
+ text += '.'
157
+ return text
158
+
159
+
160
+ def chinese_dialect_cleaners(text):
161
+ text = re.sub(r'\[MD\](.*?)\[MD\]',
162
+ lambda x: chinese_to_ipa2(x.group(1))+' ', text)
163
+ text = re.sub(r'\[TW\](.*?)\[TW\]',
164
+ lambda x: chinese_to_ipa2(x.group(1), True)+' ', text)
165
+ text = re.sub(r'\[JA\](.*?)\[JA\]',
166
+ lambda x: japanese_to_ipa3(x.group(1)).replace('Q', 'ʔ')+' ', text)
167
+ text = re.sub(r'\[SH\](.*?)\[SH\]', lambda x: shanghainese_to_ipa(x.group(1)).replace('1', '˥˧').replace('5',
168
+ '˧˧˦').replace('6', '˩˩˧').replace('7', '˥').replace('8', '˩˨').replace('ᴀ', 'ɐ').replace('ᴇ', 'e')+' ', text)
169
+ text = re.sub(r'\[GD\](.*?)\[GD\]',
170
+ lambda x: cantonese_to_ipa(x.group(1))+' ', text)
171
+ text = re.sub(r'\[EN\](.*?)\[EN\]',
172
+ lambda x: english_to_lazy_ipa2(x.group(1))+' ', text)
173
+ text = re.sub(r'\[([A-Z]{2})\](.*?)\[\1\]', lambda x: ngu_dialect_to_ipa(x.group(2), x.group(
174
+ 1)).replace('ʣ', 'dz').replace('ʥ', 'dʑ').replace('ʦ', 'ts').replace('ʨ', 'tɕ')+' ', text)
175
+ text = re.sub(r'\s+$', '', text)
176
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
177
+ return text
text/japanese.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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]), x[1]) for x in [
21
+ ('ts', 'ʦ'),
22
+ ('u', 'ɯ'),
23
+ ('j', 'ʥ'),
24
+ ('y', 'j'),
25
+ ('ni', 'n^i'),
26
+ ('nj', 'n^'),
27
+ ('hi', 'çi'),
28
+ ('hj', 'ç'),
29
+ ('f', 'ɸ'),
30
+ ('I', 'i*'),
31
+ ('U', 'ɯ*'),
32
+ ('r', 'ɾ')
33
+ ]]
34
+
35
+ # List of (romaji, ipa2) pairs for marks:
36
+ _romaji_to_ipa2 = [(re.compile('%s' % x[0]), x[1]) for x in [
37
+ ('u', 'ɯ'),
38
+ ('ʧ', 'tʃ'),
39
+ ('j', 'dʑ'),
40
+ ('y', 'j'),
41
+ ('ni', 'n^i'),
42
+ ('nj', 'n^'),
43
+ ('hi', 'çi'),
44
+ ('hj', 'ç'),
45
+ ('f', 'ɸ'),
46
+ ('I', 'i*'),
47
+ ('U', 'ɯ*'),
48
+ ('r', 'ɾ')
49
+ ]]
50
+
51
+ # List of (consonant, sokuon) pairs:
52
+ _real_sokuon = [(re.compile('%s' % x[0]), x[1]) for x in [
53
+ (r'Q([↑↓]*[kg])', r'k#\1'),
54
+ (r'Q([↑↓]*[tdjʧ])', r't#\1'),
55
+ (r'Q([↑↓]*[sʃ])', r's\1'),
56
+ (r'Q([↑↓]*[pb])', r'p#\1')
57
+ ]]
58
+
59
+ # List of (consonant, hatsuon) pairs:
60
+ _real_hatsuon = [(re.compile('%s' % x[0]), x[1]) for x in [
61
+ (r'N([↑↓]*[pbm])', r'm\1'),
62
+ (r'N([↑↓]*[ʧʥj])', r'n^\1'),
63
+ (r'N([↑↓]*[tdn])', r'n\1'),
64
+ (r'N([↑↓]*[kg])', r'ŋ\1')
65
+ ]]
66
+
67
+
68
+ def symbols_to_japanese(text):
69
+ for regex, replacement in _symbols_to_japanese:
70
+ text = re.sub(regex, replacement, text)
71
+ return text
72
+
73
+
74
+ def japanese_to_romaji_with_accent(text):
75
+ '''Reference https://r9y9.github.io/ttslearn/latest/notebooks/ch10_Recipe-Tacotron.html'''
76
+ text = symbols_to_japanese(text)
77
+ sentences = re.split(_japanese_marks, text)
78
+ marks = re.findall(_japanese_marks, text)
79
+ text = ''
80
+ for i, sentence in enumerate(sentences):
81
+ if re.match(_japanese_characters, sentence):
82
+ if text != '':
83
+ text += ' '
84
+ labels = pyopenjtalk.extract_fullcontext(sentence)
85
+ for n, label in enumerate(labels):
86
+ phoneme = re.search(r'\-([^\+]*)\+', label).group(1)
87
+ if phoneme not in ['sil', 'pau']:
88
+ text += phoneme.replace('ch', 'ʧ').replace('sh',
89
+ 'ʃ').replace('cl', 'Q')
90
+ else:
91
+ continue
92
+ # n_moras = int(re.search(r'/F:(\d+)_', label).group(1))
93
+ a1 = int(re.search(r"/A:(\-?[0-9]+)\+", label).group(1))
94
+ a2 = int(re.search(r"\+(\d+)\+", label).group(1))
95
+ a3 = int(re.search(r"\+(\d+)/", label).group(1))
96
+ if re.search(r'\-([^\+]*)\+', labels[n + 1]).group(1) in ['sil', 'pau']:
97
+ a2_next = -1
98
+ else:
99
+ a2_next = int(
100
+ re.search(r"\+(\d+)\+", labels[n + 1]).group(1))
101
+ # Accent phrase boundary
102
+ if a3 == 1 and a2_next == 1:
103
+ text += ' '
104
+ # Falling
105
+ elif a1 == 0 and a2_next == a2 + 1:
106
+ text += '↓'
107
+ # Rising
108
+ elif a2 == 1 and a2_next == 2:
109
+ text += '↑'
110
+ if i < len(marks):
111
+ text += unidecode(marks[i]).replace(' ', '')
112
+ return text
113
+
114
+
115
+ def get_real_sokuon(text):
116
+ for regex, replacement in _real_sokuon:
117
+ text = re.sub(regex, replacement, text)
118
+ return text
119
+
120
+
121
+ def get_real_hatsuon(text):
122
+ for regex, replacement in _real_hatsuon:
123
+ text = re.sub(regex, replacement, text)
124
+ return text
125
+
126
+
127
+ def japanese_to_ipa(text):
128
+ text = japanese_to_romaji_with_accent(text).replace('...', '…')
129
+ text = re.sub(
130
+ r'([aiueo])\1+', lambda x: x.group(0)[0]+'ː'*(len(x.group(0))-1), text)
131
+ text = get_real_sokuon(text)
132
+ text = get_real_hatsuon(text)
133
+ for regex, replacement in _romaji_to_ipa:
134
+ text = re.sub(regex, replacement, text)
135
+ return text
136
+
137
+
138
+ def japanese_to_ipa2(text):
139
+ text = japanese_to_romaji_with_accent(text).replace('...', '…')
140
+ text = get_real_sokuon(text)
141
+ text = get_real_hatsuon(text)
142
+ for regex, replacement in _romaji_to_ipa2:
143
+ text = re.sub(regex, replacement, text)
144
+ return text
145
+
146
+
147
+ def japanese_to_ipa3(text):
148
+ text = japanese_to_ipa2(text).replace('n^', 'ȵ').replace(
149
+ 'ʃ', 'ɕ').replace('*', '\u0325').replace('#', '\u031a')
150
+ text = re.sub(
151
+ r'([aiɯeo])\1+', lambda x: x.group(0)[0]+'ː'*(len(x.group(0))-1), text)
152
+ text = re.sub(r'((?:^|\s)(?:ts|tɕ|[kpt]))', r'\1ʰ', text)
153
+ return text
text/mandarin.py ADDED
@@ -0,0 +1,328 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import re
4
+ from pypinyin import lazy_pinyin, BOPOMOFO
5
+ import jieba
6
+ import cn2an
7
+
8
+
9
+ # List of (Latin alphabet, bopomofo) pairs:
10
+ _latin_to_bopomofo = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
11
+ ('a', 'ㄟˉ'),
12
+ ('b', 'ㄅㄧˋ'),
13
+ ('c', 'ㄙㄧˉ'),
14
+ ('d', 'ㄉㄧˋ'),
15
+ ('e', 'ㄧˋ'),
16
+ ('f', 'ㄝˊㄈㄨˋ'),
17
+ ('g', 'ㄐㄧˋ'),
18
+ ('h', 'ㄝˇㄑㄩˋ'),
19
+ ('i', 'ㄞˋ'),
20
+ ('j', 'ㄐㄟˋ'),
21
+ ('k', 'ㄎㄟˋ'),
22
+ ('l', 'ㄝˊㄛˋ'),
23
+ ('m', 'ㄝˊㄇㄨˋ'),
24
+ ('n', 'ㄣˉ'),
25
+ ('o', 'ㄡˉ'),
26
+ ('p', 'ㄆㄧˉ'),
27
+ ('q', 'ㄎㄧㄡˉ'),
28
+ ('r', 'ㄚˋ'),
29
+ ('s', 'ㄝˊㄙˋ'),
30
+ ('t', 'ㄊㄧˋ'),
31
+ ('u', 'ㄧㄡˉ'),
32
+ ('v', 'ㄨㄧˉ'),
33
+ ('w', 'ㄉㄚˋㄅㄨˋㄌㄧㄡˋ'),
34
+ ('x', 'ㄝˉㄎㄨˋㄙˋ'),
35
+ ('y', 'ㄨㄞˋ'),
36
+ ('z', 'ㄗㄟˋ')
37
+ ]]
38
+
39
+ # List of (bopomofo, romaji) pairs:
40
+ _bopomofo_to_romaji = [(re.compile('%s' % x[0]), x[1]) for x in [
41
+ ('ㄅㄛ', 'p⁼wo'),
42
+ ('ㄆㄛ', 'pʰwo'),
43
+ ('ㄇㄛ', 'mwo'),
44
+ ('ㄈㄛ', 'fwo'),
45
+ ('ㄅ', 'p⁼'),
46
+ ('ㄆ', 'pʰ'),
47
+ ('ㄇ', 'm'),
48
+ ('ㄈ', 'f'),
49
+ ('ㄉ', 't⁼'),
50
+ ('ㄊ', 'tʰ'),
51
+ ('ㄋ', 'n'),
52
+ ('ㄌ', 'l'),
53
+ ('ㄍ', 'k⁼'),
54
+ ('ㄎ', 'kʰ'),
55
+ ('ㄏ', 'h'),
56
+ ('ㄐ', 'ʧ⁼'),
57
+ ('ㄑ', 'ʧʰ'),
58
+ ('ㄒ', 'ʃ'),
59
+ ('ㄓ', 'ʦ`⁼'),
60
+ ('ㄔ', 'ʦ`ʰ'),
61
+ ('ㄕ', 's`'),
62
+ ('ㄖ', 'ɹ`'),
63
+ ('ㄗ', 'ʦ⁼'),
64
+ ('ㄘ', 'ʦʰ'),
65
+ ('ㄙ', 's'),
66
+ ('ㄚ', 'a'),
67
+ ('ㄛ', 'o'),
68
+ ('ㄜ', 'ə'),
69
+ ('ㄝ', 'e'),
70
+ ('ㄞ', 'ai'),
71
+ ('ㄟ', 'ei'),
72
+ ('ㄠ', 'au'),
73
+ ('ㄡ', 'ou'),
74
+ ('ㄧㄢ', 'yeNN'),
75
+ ('ㄢ', 'aNN'),
76
+ ('ㄧㄣ', 'iNN'),
77
+ ('ㄣ', 'əNN'),
78
+ ('ㄤ', 'aNg'),
79
+ ('ㄧㄥ', 'iNg'),
80
+ ('ㄨㄥ', 'uNg'),
81
+ ('ㄩㄥ', 'yuNg'),
82
+ ('ㄥ', 'əNg'),
83
+ ('ㄦ', 'əɻ'),
84
+ ('ㄧ', 'i'),
85
+ ('ㄨ', 'u'),
86
+ ('ㄩ', 'ɥ'),
87
+ ('ˉ', '→'),
88
+ ('ˊ', '↑'),
89
+ ('ˇ', '↓↑'),
90
+ ('ˋ', '↓'),
91
+ ('˙', ''),
92
+ (',', ','),
93
+ ('。', '.'),
94
+ ('!', '!'),
95
+ ('?', '?'),
96
+ ('—', '-')
97
+ ]]
98
+
99
+ # List of (romaji, ipa) pairs:
100
+ _romaji_to_ipa = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
101
+ ('ʃy', 'ʃ'),
102
+ ('ʧʰy', 'ʧʰ'),
103
+ ('ʧ⁼y', 'ʧ⁼'),
104
+ ('NN', 'n'),
105
+ ('Ng', 'ŋ'),
106
+ ('y', 'j'),
107
+ ('h', 'x')
108
+ ]]
109
+
110
+ # List of (bopomofo, ipa) pairs:
111
+ _bopomofo_to_ipa = [(re.compile('%s' % x[0]), x[1]) for x in [
112
+ ('ㄅㄛ', 'p⁼wo'),
113
+ ('ㄆㄛ', 'pʰwo'),
114
+ ('ㄇㄛ', 'mwo'),
115
+ ('ㄈㄛ', 'fwo'),
116
+ ('ㄅ', 'p⁼'),
117
+ ('ㄆ', 'pʰ'),
118
+ ('ㄇ', 'm'),
119
+ ('ㄈ', 'f'),
120
+ ('ㄉ', 't⁼'),
121
+ ('ㄊ', 'tʰ'),
122
+ ('ㄋ', 'n'),
123
+ ('ㄌ', 'l'),
124
+ ('ㄍ', 'k⁼'),
125
+ ('ㄎ', 'kʰ'),
126
+ ('ㄏ', 'x'),
127
+ ('ㄐ', 'tʃ⁼'),
128
+ ('ㄑ', 'tʃʰ'),
129
+ ('ㄒ', 'ʃ'),
130
+ ('ㄓ', 'ts`⁼'),
131
+ ('ㄔ', 'ts`ʰ'),
132
+ ('ㄕ', 's`'),
133
+ ('ㄖ', 'ɹ`'),
134
+ ('ㄗ', 'ts⁼'),
135
+ ('ㄘ', 'tsʰ'),
136
+ ('ㄙ', 's'),
137
+ ('ㄚ', 'a'),
138
+ ('ㄛ', 'o'),
139
+ ('ㄜ', 'ə'),
140
+ ('ㄝ', 'ɛ'),
141
+ ('ㄞ', 'aɪ'),
142
+ ('ㄟ', 'eɪ'),
143
+ ('ㄠ', 'ɑʊ'),
144
+ ('ㄡ', 'oʊ'),
145
+ ('ㄧㄢ', 'jɛn'),
146
+ ('ㄩㄢ', 'ɥæn'),
147
+ ('ㄢ', 'an'),
148
+ ('ㄧㄣ', 'in'),
149
+ ('ㄩㄣ', 'ɥn'),
150
+ ('ㄣ', 'ən'),
151
+ ('ㄤ', 'ɑŋ'),
152
+ ('ㄧㄥ', 'iŋ'),
153
+ ('ㄨㄥ', 'ʊŋ'),
154
+ ('ㄩㄥ', 'jʊŋ'),
155
+ ('ㄥ', 'əŋ'),
156
+ ('ㄦ', 'əɻ'),
157
+ ('ㄧ', 'i'),
158
+ ('ㄨ', 'u'),
159
+ ('ㄩ', 'ɥ'),
160
+ ('ˉ', '→'),
161
+ ('ˊ', '↑'),
162
+ ('ˇ', '↓↑'),
163
+ ('ˋ', '↓'),
164
+ ('˙', ''),
165
+ (',', ','),
166
+ ('。', '.'),
167
+ ('!', '!'),
168
+ ('?', '?'),
169
+ ('—', '-')
170
+ ]]
171
+
172
+ # List of (bopomofo, ipa2) pairs:
173
+ _bopomofo_to_ipa2 = [(re.compile('%s' % x[0]), x[1]) for x in [
174
+ ('ㄅㄛ', 'pwo'),
175
+ ('ㄆㄛ', 'pʰwo'),
176
+ ('ㄇㄛ', 'mwo'),
177
+ ('ㄈㄛ', 'fwo'),
178
+ ('ㄅ', 'p'),
179
+ ('ㄆ', 'pʰ'),
180
+ ('ㄇ', 'm'),
181
+ ('ㄈ', 'f'),
182
+ ('ㄉ', 't'),
183
+ ('ㄊ', 'tʰ'),
184
+ ('ㄋ', 'n'),
185
+ ('ㄌ', 'l'),
186
+ ('ㄍ', 'k'),
187
+ ('ㄎ', 'kʰ'),
188
+ ('ㄏ', 'h'),
189
+ ('ㄐ', 'tɕ'),
190
+ ('ㄑ', 'tɕʰ'),
191
+ ('ㄒ', 'ɕ'),
192
+ ('ㄓ', 'tʂ'),
193
+ ('ㄔ', 'tʂʰ'),
194
+ ('ㄕ', 'ʂ'),
195
+ ('ㄖ', 'ɻ'),
196
+ ('ㄗ', 'ts'),
197
+ ('ㄘ', 'tsʰ'),
198
+ ('ㄙ', 's'),
199
+ ('ㄚ', 'a'),
200
+ ('ㄛ', 'o'),
201
+ ('ㄜ', 'ɤ'),
202
+ ('ㄝ', 'ɛ'),
203
+ ('ㄞ', 'aɪ'),
204
+ ('ㄟ', 'eɪ'),
205
+ ('ㄠ', 'ɑʊ'),
206
+ ('ㄡ', 'oʊ'),
207
+ ('ㄧㄢ', 'jɛn'),
208
+ ('ㄩㄢ', 'yæn'),
209
+ ('ㄢ', 'an'),
210
+ ('ㄧㄣ', 'in'),
211
+ ('ㄩㄣ', 'yn'),
212
+ ('ㄣ', 'ən'),
213
+ ('ㄤ', 'ɑŋ'),
214
+ ('ㄧㄥ', 'iŋ'),
215
+ ('ㄨㄥ', 'ʊŋ'),
216
+ ('ㄩㄥ', 'jʊŋ'),
217
+ ('ㄥ', 'ɤŋ'),
218
+ ('ㄦ', 'əɻ'),
219
+ ('ㄧ', 'i'),
220
+ ('ㄨ', 'u'),
221
+ ('ㄩ', 'y'),
222
+ ('ˉ', '˥'),
223
+ ('ˊ', '˧˥'),
224
+ ('ˇ', '˨˩˦'),
225
+ ('ˋ', '˥˩'),
226
+ ('˙', ''),
227
+ (',', ','),
228
+ ('。', '.'),
229
+ ('!', '!'),
230
+ ('?', '?'),
231
+ ('—', '-')
232
+ ]]
233
+
234
+
235
+ def number_to_chinese(text):
236
+ numbers = re.findall(r'\d+(?:\.?\d+)?', text)
237
+ for number in numbers:
238
+ text = text.replace(number, cn2an.an2cn(number), 1)
239
+ return text
240
+
241
+
242
+ def chinese_to_bopomofo(text, taiwanese=False):
243
+ text = text.replace('、', ',').replace(';', ',').replace(':', ',')
244
+ words = jieba.lcut(text, cut_all=False)
245
+ text = ''
246
+ for word in words:
247
+ bopomofos = lazy_pinyin(word, BOPOMOFO)
248
+ if not re.search('[\u4e00-\u9fff]', word):
249
+ text += word
250
+ continue
251
+ for i in range(len(bopomofos)):
252
+ bopomofos[i] = re.sub(r'([\u3105-\u3129])$', r'\1ˉ', bopomofos[i])
253
+ if text != '':
254
+ text += ' '
255
+ if taiwanese:
256
+ text += '#'+'#'.join(bopomofos)
257
+ else:
258
+ text += ''.join(bopomofos)
259
+ return text
260
+
261
+
262
+ def latin_to_bopomofo(text):
263
+ for regex, replacement in _latin_to_bopomofo:
264
+ text = re.sub(regex, replacement, text)
265
+ return text
266
+
267
+
268
+ def bopomofo_to_romaji(text):
269
+ for regex, replacement in _bopomofo_to_romaji:
270
+ text = re.sub(regex, replacement, text)
271
+ return text
272
+
273
+
274
+ def bopomofo_to_ipa(text):
275
+ for regex, replacement in _bopomofo_to_ipa:
276
+ text = re.sub(regex, replacement, text)
277
+ return text
278
+
279
+
280
+ def bopomofo_to_ipa2(text):
281
+ for regex, replacement in _bopomofo_to_ipa2:
282
+ text = re.sub(regex, replacement, text)
283
+ return text
284
+
285
+
286
+ def chinese_to_romaji(text):
287
+ text = number_to_chinese(text)
288
+ text = chinese_to_bopomofo(text)
289
+ text = latin_to_bopomofo(text)
290
+ text = bopomofo_to_romaji(text)
291
+ text = re.sub('i([aoe])', r'y\1', text)
292
+ text = re.sub('u([aoəe])', r'w\1', text)
293
+ text = re.sub('([ʦsɹ]`[⁼ʰ]?)([→↓↑ ]+|$)',
294
+ r'\1ɹ`\2', text).replace('ɻ', 'ɹ`')
295
+ text = re.sub('([ʦs][⁼ʰ]?)([→↓↑ ]+|$)', r'\1ɹ\2', text)
296
+ return text
297
+
298
+
299
+ def chinese_to_lazy_ipa(text):
300
+ text = chinese_to_romaji(text)
301
+ for regex, replacement in _romaji_to_ipa:
302
+ text = re.sub(regex, replacement, text)
303
+ return text
304
+
305
+
306
+ def chinese_to_ipa(text):
307
+ text = number_to_chinese(text)
308
+ text = chinese_to_bopomofo(text)
309
+ text = latin_to_bopomofo(text)
310
+ text = bopomofo_to_ipa(text)
311
+ text = re.sub('i([aoe])', r'j\1', text)
312
+ text = re.sub('u([aoəe])', r'w\1', text)
313
+ text = re.sub('([sɹ]`[⁼ʰ]?)([→↓↑ ]+|$)',
314
+ r'\1ɹ`\2', text).replace('ɻ', 'ɹ`')
315
+ text = re.sub('([s][⁼ʰ]?)([→↓↑ ]+|$)', r'\1ɹ\2', text)
316
+ return text
317
+
318
+
319
+ def chinese_to_ipa2(text, taiwanese=False):
320
+ text = number_to_chinese(text)
321
+ text = chinese_to_bopomofo(text, taiwanese)
322
+ text = latin_to_bopomofo(text)
323
+ text = bopomofo_to_ipa2(text)
324
+ text = re.sub(r'i([aoe])', r'j\1', text)
325
+ text = re.sub(r'u([aoəe])', r'w\1', text)
326
+ text = re.sub(r'([ʂɹ]ʰ?)([˩˨˧˦˥ ]+|$)', r'\1ʅ\2', text)
327
+ text = re.sub(r'(sʰ?)([˩˨˧˦˥ ]+|$)', r'\1ɿ\2', text)
328
+ return text
text/symbols.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ Defines the set of symbols used in text input to the model.
3
+ '''
4
+
5
+ _pad = '_'
6
+ _punctuation = ',.!?-'
7
+ _letters = 'AEINOQUabdefghijkmnoprstuvwyzʃʧ↓↑ '
8
+
9
+ '''
10
+ # japanese_cleaners2
11
+ _pad = '_'
12
+ _punctuation = ',.!?-~…'
13
+ _letters = 'AEINOQUabdefghijkmnoprstuvwyzʃʧʦ↓↑ '
14
+ '''
15
+
16
+ '''# korean_cleaners
17
+ _pad = '_'
18
+ _punctuation = ',.!?…~'
19
+ _letters = 'ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎㄲㄸㅃㅆㅉㅏㅓㅗㅜㅡㅣㅐㅔ '
20
+ '''
21
+
22
+ '''# chinese_cleaners
23
+ _pad = '_'
24
+ _punctuation = ',。!?—…'
25
+ _letters = 'ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦㄧㄨㄩˉˊˇˋ˙ '
26
+ '''
27
+
28
+
29
+ '''# sanskrit_cleaners
30
+ _pad = '_'
31
+ _punctuation = '।'
32
+ _letters = 'ँंःअआइईउऊऋएऐओऔकखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसहऽािीुूृॄेैोौ्ॠॢ '
33
+ '''
34
+
35
+ '''# cjks_cleaners
36
+ _pad = '_'
37
+ _punctuation = ',.!?-~…'
38
+ _letters = 'NQabdefghijklmnopstuvwxyzʃʧʥʦɯɹəɥçɸɾβŋɦː⁼ʰ`^#*=→↓↑ '
39
+ '''
40
+
41
+ '''# thai_cleaners
42
+ _pad = '_'
43
+ _punctuation = '.!? '
44
+ _letters = 'กขฃคฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลวศษสหฬอฮฯะัาำิีึืุูเแโใไๅๆ็่้๊๋์'
45
+ '''
46
+
47
+ '''# cjke_cleaners2
48
+ _pad = '_'
49
+ _punctuation = ',.!?-~…'
50
+ _letters = 'NQabdefghijklmnopstuvwxyzɑæʃʑçɯɪɔɛɹðəɫɥɸʊɾʒθβŋɦ⁼ʰ`^#*=ˈˌ→↓↑ '
51
+ '''
52
+
53
+ '''# shanghainese_cleaners
54
+ _pad = '_'
55
+ _punctuation = ',.!?…'
56
+ _letters = 'abdfghiklmnopstuvyzøŋȵɑɔɕəɤɦɪɿʑʔʰ̩̃ᴀᴇ15678 '
57
+ '''
58
+
59
+ '''# chinese_dialect_cleaners
60
+ _pad = '_'
61
+ _punctuation = ',.!?~…─'
62
+ _letters = '#Nabdefghijklmnoprstuvwxyzæçøŋœȵɐɑɒɓɔɕɗɘəɚɛɜɣɤɦɪɭɯɵɷɸɻɾɿʂʅʊʋʌʏʑʔʦʮʰʷˀː˥˦˧˨˩̥̩̃̚αᴀᴇ↑↓∅ⱼ '
63
+ '''
64
+
65
+ # Export all symbols:
66
+ symbols = [_pad] + list(_punctuation) + list(_letters)
67
+
68
+ # Special symbol ids
69
+ SPACE_ID = symbols.index(" ")