ORI-Muchim commited on
Commit
c1d9b48
1 Parent(s): 3332ea6

Upload cleaners.py

Browse files
Files changed (1) hide show
  1. text/cleaners.py +181 -0
text/cleaners.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 text.shanghainese import shanghainese_to_ipa
9
+ #from text.cantonese import cantonese_to_ipa
10
+ #from text.ngu_dialect import ngu_dialect_to_ipa
11
+
12
+
13
+ def japanese_cleaners(text):
14
+ text = japanese_to_romaji_with_accent(text)
15
+ if re.match('[A-Za-z]', text[-1]):
16
+ text += '.'
17
+ return text
18
+
19
+
20
+ def japanese_cleaners2(text):
21
+ return japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
22
+
23
+
24
+ def korean_cleaners(text):
25
+ '''Pipeline for Korean text'''
26
+ text = latin_to_hangul(text)
27
+ text = number_to_hangul(text)
28
+ text = divide_hangul(text)
29
+ if re.match('[\u3131-\u3163]', text[-1]):
30
+ text += '.'
31
+ return text
32
+
33
+
34
+ def korean_cleaners2(text):
35
+ text = korean_to_ipa(text)
36
+ return text
37
+
38
+
39
+ def chinese_cleaners(text):
40
+ '''Pipeline for Chinese text'''
41
+ text = number_to_chinese(text)
42
+ text = chinese_to_bopomofo(text)
43
+ text = latin_to_bopomofo(text)
44
+ if re.match('[ˉˊˇˋ˙]', text[-1]):
45
+ text += '。'
46
+ return text
47
+
48
+
49
+ def zh_ja_mixture_cleaners(text):
50
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
51
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
52
+ for chinese_text in chinese_texts:
53
+ cleaned_text = chinese_to_romaji(chinese_text[4:-4])
54
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
55
+ for japanese_text in japanese_texts:
56
+ cleaned_text = japanese_to_romaji_with_accent(
57
+ japanese_text[4:-4]).replace('ts', 'ʦ').replace('u', 'ɯ').replace('...', '…')
58
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
59
+ text = text[:-1]
60
+ if re.match('[A-Za-zɯɹəɥ→↓↑]', text[-1]):
61
+ text += '.'
62
+ return text
63
+
64
+
65
+ def sanskrit_cleaners(text):
66
+ text = text.replace('॥', '।').replace('ॐ', 'ओम्')
67
+ if text[-1] != '।':
68
+ text += ' ।'
69
+ return text
70
+
71
+
72
+ def cjks_cleaners(text):
73
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
74
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
75
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
76
+ sanskrit_texts = re.findall(r'\[SA\].*?\[SA\]', text)
77
+ english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
78
+ for chinese_text in chinese_texts:
79
+ cleaned_text = chinese_to_lazy_ipa(chinese_text[4:-4])
80
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
81
+ for japanese_text in japanese_texts:
82
+ cleaned_text = japanese_to_ipa(japanese_text[4:-4])
83
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
84
+ for korean_text in korean_texts:
85
+ cleaned_text = korean_to_lazy_ipa(korean_text[4:-4])
86
+ text = text.replace(korean_text, cleaned_text+' ', 1)
87
+ for sanskrit_text in sanskrit_texts:
88
+ cleaned_text = devanagari_to_ipa(sanskrit_text[4:-4])
89
+ text = text.replace(sanskrit_text, cleaned_text+' ', 1)
90
+ for english_text in english_texts:
91
+ cleaned_text = english_to_lazy_ipa(english_text[4:-4])
92
+ text = text.replace(english_text, cleaned_text+' ', 1)
93
+ text = text[:-1]
94
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
95
+ text += '.'
96
+ return text
97
+
98
+
99
+ def cjke_cleaners(text):
100
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
101
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
102
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
103
+ #english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
104
+ for chinese_text in chinese_texts:
105
+ cleaned_text = chinese_to_lazy_ipa(chinese_text[4:-4])
106
+ cleaned_text = cleaned_text.replace(
107
+ 'ʧ', 'tʃ').replace('ʦ', 'ts').replace('ɥan', 'ɥæn')
108
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
109
+ for japanese_text in japanese_texts:
110
+ cleaned_text = japanese_to_ipa(japanese_text[4:-4])
111
+ cleaned_text = cleaned_text.replace('ʧ', 'tʃ').replace(
112
+ 'ʦ', 'ts').replace('ɥan', 'ɥæn').replace('ʥ', 'dz')
113
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
114
+ for korean_text in korean_texts:
115
+ cleaned_text = korean_to_ipa(korean_text[4:-4])
116
+ text = text.replace(korean_text, cleaned_text+' ', 1)
117
+ #for english_text in english_texts:
118
+ # cleaned_text = english_to_ipa2(english_text[4:-4])
119
+ # cleaned_text = cleaned_text.replace('ɑ', 'a').replace(
120
+ # 'ɔ', 'o').replace('ɛ', 'e').replace('ɪ', 'i').replace('ʊ', 'u')
121
+ # text = text.replace(english_text, cleaned_text+' ', 1)
122
+ text = text[:-1]
123
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
124
+ text += '.'
125
+ return text
126
+
127
+
128
+ def cjke_cleaners2(text):
129
+ chinese_texts = re.findall(r'\[ZH\].*?\[ZH\]', text)
130
+ japanese_texts = re.findall(r'\[JA\].*?\[JA\]', text)
131
+ korean_texts = re.findall(r'\[KO\].*?\[KO\]', text)
132
+ english_texts = re.findall(r'\[EN\].*?\[EN\]', text)
133
+ for chinese_text in chinese_texts:
134
+ cleaned_text = chinese_to_ipa(chinese_text[4:-4])
135
+ text = text.replace(chinese_text, cleaned_text+' ', 1)
136
+ for japanese_text in japanese_texts:
137
+ cleaned_text = japanese_to_ipa2(japanese_text[4:-4])
138
+ text = text.replace(japanese_text, cleaned_text+' ', 1)
139
+ for korean_text in korean_texts:
140
+ cleaned_text = korean_to_ipa(korean_text[4:-4])
141
+ text = text.replace(korean_text, cleaned_text+' ', 1)
142
+ for english_text in english_texts:
143
+ cleaned_text = english_to_ipa2(english_text[4:-4])
144
+ text = text.replace(english_text, cleaned_text+' ', 1)
145
+ #text = text[:-1]
146
+ #if re.match(r'[^\.,!\?\-…~]', text[-1]):
147
+ text += '.'
148
+ return text
149
+
150
+
151
+ def thai_cleaners(text):
152
+ text = num_to_thai(text)
153
+ text = latin_to_thai(text)
154
+ return text
155
+
156
+
157
+ def shanghainese_cleaners(text):
158
+ text = shanghainese_to_ipa(text)
159
+ if re.match(r'[^\.,!\?\-…~]', text[-1]):
160
+ text += '.'
161
+ return text
162
+
163
+
164
+ def chinese_dialect_cleaners(text):
165
+ text = re.sub(r'\[MD\](.*?)\[MD\]',
166
+ lambda x: chinese_to_ipa2(x.group(1))+' ', text)
167
+ text = re.sub(r'\[TW\](.*?)\[TW\]',
168
+ lambda x: chinese_to_ipa2(x.group(1), True)+' ', text)
169
+ text = re.sub(r'\[JA\](.*?)\[JA\]',
170
+ lambda x: japanese_to_ipa3(x.group(1)).replace('Q', 'ʔ')+' ', text)
171
+ text = re.sub(r'\[SH\](.*?)\[SH\]', lambda x: shanghainese_to_ipa(x.group(1)).replace('1', '˥˧').replace('5',
172
+ '˧˧˦').replace('6', '˩˩˧').replace('7', '˥').replace('8', '˩˨').replace('ᴀ', 'ɐ').replace('ᴇ', 'e')+' ', text)
173
+ text = re.sub(r'\[GD\](.*?)\[GD\]',
174
+ lambda x: cantonese_to_ipa(x.group(1))+' ', text)
175
+ text = re.sub(r'\[EN\](.*?)\[EN\]',
176
+ lambda x: english_to_lazy_ipa2(x.group(1))+' ', text)
177
+ text = re.sub(r'\[([A-Z]{2})\](.*?)\[\1\]', lambda x: ngu_dialect_to_ipa(x.group(2), x.group(
178
+ 1)).replace('ʣ', 'dz').replace('ʥ', 'dʑ').replace('ʦ', 'ts').replace('ʨ', 'tɕ')+' ', text)
179
+ text = re.sub(r'\s+$', '', text)
180
+ text = re.sub(r'([^\.,!\?\-…~])$', r'\1.', text)
181
+ return text