NaNames commited on
Commit
ee66067
·
1 Parent(s): 89d3c68

Upload cleaners.py

Browse files
Files changed (1) hide show
  1. cleaners.py +203 -0
cleaners.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from text.japanese import japanese_to_romaji_with_accent, japanese_to_ipa, japanese_to_ipa2, japanese_to_ipa3
3
+ from text.korean import latin_to_hangul, number_to_hangul, divide_hangul, korean_to_lazy_ipa, korean_to_ipa
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
+ from text.sanskrit import devanagari_to_ipa
6
+ from text.english import english_to_lazy_ipa, english_to_ipa2, english_to_lazy_ipa2
7
+ from text.thai import num_to_thai, latin_to_thai
8
+ from unidecode import unidecode
9
+ from phonemizer import phonemize
10
+ from pypinyin import Style, pinyin
11
+ from pypinyin.style._utils import get_finals, get_initials
12
+ # Regular expression matching whitespace:
13
+ _whitespace_re = re.compile(r'\s+')
14
+
15
+ # List of (regular expression, replacement) pairs for abbreviations:
16
+ _abbreviations = [(re.compile('\\b%s\\.' % x[0], re.IGNORECASE), x[1]) for x in [
17
+ ('mrs', 'misess'),
18
+ ('mr', 'mister'),
19
+ ('dr', 'doctor'),
20
+ ('st', 'saint'),
21
+ ('co', 'company'),
22
+ ('jr', 'junior'),
23
+ ('maj', 'major'),
24
+ ('gen', 'general'),
25
+ ('drs', 'doctors'),
26
+ ('rev', 'reverend'),
27
+ ('lt', 'lieutenant'),
28
+ ('hon', 'honorable'),
29
+ ('sgt', 'sergeant'),
30
+ ('capt', 'captain'),
31
+ ('esq', 'esquire'),
32
+ ('ltd', 'limited'),
33
+ ('col', 'colonel'),
34
+ ('ft', 'fort'),
35
+ ]]
36
+ # from text.shanghainese import shanghainese_to_ipa
37
+ # from text.cantonese import cantonese_to_ipa
38
+ # from text.ngu_dialect import ngu_dialect_to_ipa
39
+
40
+
41
+ def japanese_cleaners(text):
42
+ text = japanese_to_romaji_with_accent(text)
43
+ text = re.sub(r'([A-Za-z])$', r'\1.', text)
44
+ return text
45
+
46
+
47
+ def japanese_cleaners2(text):
48
+ return japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
49
+
50
+
51
+ def korean_cleaners(text):
52
+ '''Pipeline for Korean text'''
53
+ text = latin_to_hangul(text)
54
+ text = number_to_hangul(text)
55
+ text = divide_hangul(text)
56
+ text = re.sub(r'([\u3131-\u3163])$', r'\1.', text)
57
+ return text
58
+
59
+
60
+ def chinese_cleaners(text):
61
+ '''Pipeline for Chinese text'''
62
+ text = text.replace("[ZH]", "")
63
+ text = number_to_chinese(text)
64
+ text = chinese_to_bopomofo(text)
65
+ text = latin_to_bopomofo(text)
66
+ text = re.sub(r'([ˉˊˇˋ˙])$', r'\1。', text)
67
+ return text
68
+
69
+
70
+ def zh_ja_mixture_cleaners(text):
71
+ text = re.sub(r'\[ZH\](.*?)\[ZH\]',
72
+ lambda x: chinese_to_romaji(x.group(1))+' ', text)
73
+ text = re.sub(r'\[JA\](.*?)\[JA\]', lambda x: japanese_to_romaji_with_accent(
74
+ x.group(1)).replace('ts', 'ʦ').replace('u', 'ɯ').replace('...', '…')+' ', text)
75
+ text = re.sub(r'\s+$', '', text)
76
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
77
+ return text
78
+
79
+
80
+ def sanskrit_cleaners(text):
81
+ text = text.replace('॥', '।').replace('ॐ', 'ओम्')
82
+ text = re.sub(r'([^।])$', r'\1।', text)
83
+ return text
84
+
85
+
86
+ def cjks_cleaners(text):
87
+ text = re.sub(r'\[ZH\](.*?)\[ZH\]',
88
+ lambda x: chinese_to_lazy_ipa(x.group(1))+' ', text)
89
+ text = re.sub(r'\[JA\](.*?)\[JA\]',
90
+ lambda x: japanese_to_ipa(x.group(1))+' ', text)
91
+ text = re.sub(r'\[KO\](.*?)\[KO\]',
92
+ lambda x: korean_to_lazy_ipa(x.group(1))+' ', text)
93
+ text = re.sub(r'\[SA\](.*?)\[SA\]',
94
+ lambda x: devanagari_to_ipa(x.group(1))+' ', text)
95
+ text = re.sub(r'\[EN\](.*?)\[EN\]',
96
+ lambda x: english_to_lazy_ipa(x.group(1))+' ', text)
97
+ text = re.sub(r'\s+$', '', text)
98
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
99
+ return text
100
+
101
+
102
+ def cjke_cleaners(text):
103
+ text = re.sub(r'\[ZH\](.*?)\[ZH\]', lambda x: chinese_to_lazy_ipa(x.group(1)).replace(
104
+ 'ʧ', 'tʃ').replace('ʦ', 'ts').replace('ɥan', 'ɥæn')+' ', text)
105
+ text = re.sub(r'\[JA\](.*?)\[JA\]', lambda x: japanese_to_ipa(x.group(1)).replace('ʧ', 'tʃ').replace(
106
+ 'ʦ', 'ts').replace('ɥan', 'ɥæn').replace('ʥ', 'dz')+' ', text)
107
+ text = re.sub(r'\[KO\](.*?)\[KO\]',
108
+ lambda x: korean_to_ipa(x.group(1))+' ', text)
109
+ text = re.sub(r'\[EN\](.*?)\[EN\]', lambda x: english_to_ipa2(x.group(1)).replace('ɑ', 'a').replace(
110
+ 'ɔ', 'o').replace('ɛ', 'e').replace('ɪ', 'i').replace('ʊ', 'u')+' ', text)
111
+ text = re.sub(r'\s+$', '', text)
112
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
113
+ return text
114
+
115
+
116
+ def cjke_cleaners2(text):
117
+ text = re.sub(r'\[ZH\](.*?)\[ZH\]',
118
+ lambda x: chinese_to_ipa(x.group(1))+' ', text)
119
+ text = re.sub(r'\[JA\](.*?)\[JA\]',
120
+ lambda x: japanese_to_ipa2(x.group(1))+' ', text)
121
+ text = re.sub(r'\[KO\](.*?)\[KO\]',
122
+ lambda x: korean_to_ipa(x.group(1))+' ', text)
123
+ text = re.sub(r'\[EN\](.*?)\[EN\]',
124
+ lambda x: english_to_ipa2(x.group(1))+' ', text)
125
+ text = re.sub(r'\s+$', '', text)
126
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
127
+ return text
128
+
129
+
130
+ def thai_cleaners(text):
131
+ text = num_to_thai(text)
132
+ text = latin_to_thai(text)
133
+ return text
134
+
135
+
136
+ # def shanghainese_cleaners(text):
137
+ # text = shanghainese_to_ipa(text)
138
+ # text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
139
+ # return text
140
+
141
+
142
+ # def chinese_dialect_cleaners(text):
143
+ # text = re.sub(r'\[ZH\](.*?)\[ZH\]',
144
+ # lambda x: chinese_to_ipa2(x.group(1))+' ', text)
145
+ # text = re.sub(r'\[JA\](.*?)\[JA\]',
146
+ # lambda x: japanese_to_ipa3(x.group(1)).replace('Q', 'ʔ')+' ', text)
147
+ # text = re.sub(r'\[SH\](.*?)\[SH\]', lambda x: shanghainese_to_ipa(x.group(1)).replace('1', '˥˧').replace('5',
148
+ # '˧˧˦').replace('6', '˩˩˧').replace('7', '˥').replace('8', '˩˨').replace('ᴀ', 'ɐ').replace('ᴇ', 'e')+' ', text)
149
+ # text = re.sub(r'\[GD\](.*?)\[GD\]',
150
+ # lambda x: cantonese_to_ipa(x.group(1))+' ', text)
151
+ # text = re.sub(r'\[EN\](.*?)\[EN\]',
152
+ # lambda x: english_to_lazy_ipa2(x.group(1))+' ', text)
153
+ # text = re.sub(r'\[([A-Z]{2})\](.*?)\[\1\]', lambda x: ngu_dialect_to_ipa(x.group(2), x.group(
154
+ # 1)).replace('ʣ', 'dz').replace('ʥ', 'dʑ').replace('ʦ', 'ts').replace('ʨ', 'tɕ')+' ', text)
155
+ # text = re.sub(r'\s+$', '', text)
156
+ # text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
157
+ # return text
158
+
159
+ def english_cleaners(text):
160
+ '''Pipeline for English text, including abbreviation expansion.'''
161
+ text = convert_to_ascii(text)
162
+ text = lowercase(text)
163
+ text = expand_abbreviations(text)
164
+ phonemes = phonemize(text, language='en-us', backend='espeak', strip=True)
165
+ phonemes = collapse_whitespace(phonemes)
166
+ return phonemes
167
+
168
+
169
+ def english_cleaners2(text):
170
+ '''Pipeline for English text, including abbreviation expansion. + punctuation + stress'''
171
+ text = convert_to_ascii(text)
172
+ text = lowercase(text)
173
+ text = expand_abbreviations(text)
174
+ phonemes = phonemize(text, language='en-us', backend='espeak', strip=True, preserve_punctuation=True,
175
+ with_stress=True)
176
+ phonemes = collapse_whitespace(phonemes)
177
+ return phonemes
178
+
179
+
180
+ def chinese_cleaners1(text):
181
+ from pypinyin import Style, pinyin
182
+
183
+ phones = [phone[0] for phone in pinyin(text, style=Style.TONE3)]
184
+ return ' '.join(phones)
185
+
186
+
187
+ def chinese_cleaners2(text):
188
+ phones = [
189
+ p
190
+ for phone in pinyin(text, style=Style.TONE3)
191
+ for p in [
192
+ get_initials(phone[0], strict=True),
193
+ get_finals(phone[0][:-1], strict=True) + phone[0][-1]
194
+ if phone[0][-1].isdigit()
195
+ else get_finals(phone[0], strict=True)
196
+ if phone[0][-1].isalnum()
197
+ else phone[0],
198
+ ]
199
+ # Remove the case of individual tones as a phoneme
200
+ if len(p) != 0 and not p.isdigit()
201
+ ]
202
+ return phones
203
+ # return phonemes