Spaces:
Running
on
Zero
Running
on
Zero
Update GPT_SoVITS/text/japanese.py
Browse files- GPT_SoVITS/text/japanese.py +223 -227
GPT_SoVITS/text/japanese.py
CHANGED
@@ -1,227 +1,223 @@
|
|
1 |
-
# modified from https://github.com/CjangCjengh/vits/blob/main/text/japanese.py
|
2 |
-
import re
|
3 |
-
|
4 |
-
import pyopenjtalk
|
5 |
-
import os
|
6 |
-
import hashlib
|
7 |
-
from text.symbols2 import symbols
|
8 |
-
current_file_path = os.path.dirname(__file__)
|
9 |
-
def get_hash(fp: str) -> str:
|
10 |
-
hash_md5 = hashlib.md5()
|
11 |
-
with open(fp, "rb") as f:
|
12 |
-
for chunk in iter(lambda: f.read(4096), b""):
|
13 |
-
hash_md5.update(chunk)
|
14 |
-
return hash_md5.hexdigest()
|
15 |
-
|
16 |
-
USERDIC_CSV_PATH = os.path.join(current_file_path, "ja_userdic", "userdict.csv")
|
17 |
-
USERDIC_BIN_PATH = os.path.join(current_file_path, "ja_userdic", "user.dict")
|
18 |
-
USERDIC_HASH_PATH = os.path.join(current_file_path, "ja_userdic", "userdict.md5")
|
19 |
-
# 如果没有用户词典,就生成一个;如果有,就检查md5,如果不一样,就重新生成
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# List of (
|
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 |
-
if ph in
|
84 |
-
ph =
|
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 |
-
# deal
|
167 |
-
if
|
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 |
-
if __name__ == "__main__":
|
226 |
-
phones = g2p("こんにちは, hello, AKITOです,よろしくお願いしますね!")
|
227 |
-
print(phones)
|
|
|
1 |
+
# modified from https://github.com/CjangCjengh/vits/blob/main/text/japanese.py
|
2 |
+
import re
|
3 |
+
|
4 |
+
import pyopenjtalk
|
5 |
+
import os
|
6 |
+
import hashlib
|
7 |
+
from text.symbols2 import symbols
|
8 |
+
current_file_path = os.path.dirname(__file__)
|
9 |
+
def get_hash(fp: str) -> str:
|
10 |
+
hash_md5 = hashlib.md5()
|
11 |
+
with open(fp, "rb") as f:
|
12 |
+
for chunk in iter(lambda: f.read(4096), b""):
|
13 |
+
hash_md5.update(chunk)
|
14 |
+
return hash_md5.hexdigest()
|
15 |
+
|
16 |
+
USERDIC_CSV_PATH = os.path.join(current_file_path, "ja_userdic", "userdict.csv")
|
17 |
+
USERDIC_BIN_PATH = os.path.join(current_file_path, "ja_userdic", "user.dict")
|
18 |
+
USERDIC_HASH_PATH = os.path.join(current_file_path, "ja_userdic", "userdict.md5")
|
19 |
+
# 如果没有用户词典,就生成一个;如果有,就检查md5,如果不一样,就重新生成
|
20 |
+
try:
|
21 |
+
if os.path.exists(USERDIC_BIN_PATH):
|
22 |
+
pyopenjtalk.update_global_jtalk_with_user_dict(USERDIC_BIN_PATH)
|
23 |
+
except:
|
24 |
+
print("FAIL TO USE USERDICT")
|
25 |
+
|
26 |
+
|
27 |
+
from text.symbols import punctuation
|
28 |
+
# Regular expression matching Japanese without punctuation marks:
|
29 |
+
_japanese_characters = re.compile(
|
30 |
+
r"[A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]"
|
31 |
+
)
|
32 |
+
|
33 |
+
# Regular expression matching non-Japanese characters or punctuation marks:
|
34 |
+
_japanese_marks = re.compile(
|
35 |
+
r"[^A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]"
|
36 |
+
)
|
37 |
+
|
38 |
+
# List of (symbol, Japanese) pairs for marks:
|
39 |
+
_symbols_to_japanese = [(re.compile("%s" % x[0]), x[1]) for x in [("%", "パーセント")]]
|
40 |
+
|
41 |
+
|
42 |
+
# List of (consonant, sokuon) pairs:
|
43 |
+
_real_sokuon = [
|
44 |
+
(re.compile("%s" % x[0]), x[1])
|
45 |
+
for x in [
|
46 |
+
(r"Q([↑↓]*[kg])", r"k#\1"),
|
47 |
+
(r"Q([↑↓]*[tdjʧ])", r"t#\1"),
|
48 |
+
(r"Q([↑↓]*[sʃ])", r"s\1"),
|
49 |
+
(r"Q([↑↓]*[pb])", r"p#\1"),
|
50 |
+
]
|
51 |
+
]
|
52 |
+
|
53 |
+
# List of (consonant, hatsuon) pairs:
|
54 |
+
_real_hatsuon = [
|
55 |
+
(re.compile("%s" % x[0]), x[1])
|
56 |
+
for x in [
|
57 |
+
(r"N([↑↓]*[pbm])", r"m\1"),
|
58 |
+
(r"N([↑↓]*[ʧʥj])", r"n^\1"),
|
59 |
+
(r"N([↑↓]*[tdn])", r"n\1"),
|
60 |
+
(r"N([↑↓]*[kg])", r"ŋ\1"),
|
61 |
+
]
|
62 |
+
]
|
63 |
+
|
64 |
+
|
65 |
+
def post_replace_ph(ph):
|
66 |
+
rep_map = {
|
67 |
+
":": ",",
|
68 |
+
";": ",",
|
69 |
+
",": ",",
|
70 |
+
"。": ".",
|
71 |
+
"!": "!",
|
72 |
+
"?": "?",
|
73 |
+
"\n": ".",
|
74 |
+
"·": ",",
|
75 |
+
"、": ",",
|
76 |
+
"...": "…",
|
77 |
+
}
|
78 |
+
|
79 |
+
if ph in rep_map.keys():
|
80 |
+
ph = rep_map[ph]
|
81 |
+
if ph in symbols:
|
82 |
+
return ph
|
83 |
+
if ph not in symbols:
|
84 |
+
ph = "UNK"
|
85 |
+
return ph
|
86 |
+
|
87 |
+
|
88 |
+
def replace_consecutive_punctuation(text):
|
89 |
+
punctuations = ''.join(re.escape(p) for p in punctuation)
|
90 |
+
pattern = f'([{punctuations}])([{punctuations}])+'
|
91 |
+
result = re.sub(pattern, r'\1', text)
|
92 |
+
return result
|
93 |
+
|
94 |
+
|
95 |
+
def symbols_to_japanese(text):
|
96 |
+
for regex, replacement in _symbols_to_japanese:
|
97 |
+
text = re.sub(regex, replacement, text)
|
98 |
+
return text
|
99 |
+
|
100 |
+
|
101 |
+
def preprocess_jap(text, with_prosody=False):
|
102 |
+
"""Reference https://r9y9.github.io/ttslearn/latest/notebooks/ch10_Recipe-Tacotron.html"""
|
103 |
+
text = symbols_to_japanese(text)
|
104 |
+
sentences = re.split(_japanese_marks, text)
|
105 |
+
marks = re.findall(_japanese_marks, text)
|
106 |
+
text = []
|
107 |
+
for i, sentence in enumerate(sentences):
|
108 |
+
if re.match(_japanese_characters, sentence):
|
109 |
+
if with_prosody:
|
110 |
+
text += pyopenjtalk_g2p_prosody(sentence)[1:-1]
|
111 |
+
else:
|
112 |
+
p = pyopenjtalk.g2p(sentence)
|
113 |
+
text += p.split(" ")
|
114 |
+
|
115 |
+
if i < len(marks):
|
116 |
+
if marks[i] == " ":# 防止意外的UNK
|
117 |
+
continue
|
118 |
+
text += [marks[i].replace(" ", "")]
|
119 |
+
return text
|
120 |
+
|
121 |
+
|
122 |
+
def text_normalize(text):
|
123 |
+
# todo: jap text normalize
|
124 |
+
|
125 |
+
# 避免重复标点引起的参考泄露
|
126 |
+
text = replace_consecutive_punctuation(text)
|
127 |
+
text = "".join([i.lower() for i in text])
|
128 |
+
return text
|
129 |
+
|
130 |
+
# Copied from espnet https://github.com/espnet/espnet/blob/master/espnet2/text/phoneme_tokenizer.py
|
131 |
+
def pyopenjtalk_g2p_prosody(text, drop_unvoiced_vowels=True):
|
132 |
+
"""Extract phoneme + prosoody symbol sequence from input full-context labels.
|
133 |
+
|
134 |
+
The algorithm is based on `Prosodic features control by symbols as input of
|
135 |
+
sequence-to-sequence acoustic modeling for neural TTS`_ with some r9y9's tweaks.
|
136 |
+
|
137 |
+
Args:
|
138 |
+
text (str): Input text.
|
139 |
+
drop_unvoiced_vowels (bool): whether to drop unvoiced vowels.
|
140 |
+
|
141 |
+
Returns:
|
142 |
+
List[str]: List of phoneme + prosody symbols.
|
143 |
+
|
144 |
+
Examples:
|
145 |
+
>>> from espnet2.text.phoneme_tokenizer import pyopenjtalk_g2p_prosody
|
146 |
+
>>> pyopenjtalk_g2p_prosody("こんにちは。")
|
147 |
+
['^', 'k', 'o', '[', 'N', 'n', 'i', 'ch', 'i', 'w', 'a', '$']
|
148 |
+
|
149 |
+
.. _`Prosodic features control by symbols as input of sequence-to-sequence acoustic
|
150 |
+
modeling for neural TTS`: https://doi.org/10.1587/transinf.2020EDP7104
|
151 |
+
|
152 |
+
"""
|
153 |
+
labels = pyopenjtalk.make_label(pyopenjtalk.run_frontend(text))
|
154 |
+
N = len(labels)
|
155 |
+
|
156 |
+
phones = []
|
157 |
+
for n in range(N):
|
158 |
+
lab_curr = labels[n]
|
159 |
+
|
160 |
+
# current phoneme
|
161 |
+
p3 = re.search(r"\-(.*?)\+", lab_curr).group(1)
|
162 |
+
# deal unvoiced vowels as normal vowels
|
163 |
+
if drop_unvoiced_vowels and p3 in "AEIOU":
|
164 |
+
p3 = p3.lower()
|
165 |
+
|
166 |
+
# deal with sil at the beginning and the end of text
|
167 |
+
if p3 == "sil":
|
168 |
+
assert n == 0 or n == N - 1
|
169 |
+
if n == 0:
|
170 |
+
phones.append("^")
|
171 |
+
elif n == N - 1:
|
172 |
+
# check question form or not
|
173 |
+
e3 = _numeric_feature_by_regex(r"!(\d+)_", lab_curr)
|
174 |
+
if e3 == 0:
|
175 |
+
phones.append("$")
|
176 |
+
elif e3 == 1:
|
177 |
+
phones.append("?")
|
178 |
+
continue
|
179 |
+
elif p3 == "pau":
|
180 |
+
phones.append("_")
|
181 |
+
continue
|
182 |
+
else:
|
183 |
+
phones.append(p3)
|
184 |
+
|
185 |
+
# accent type and position info (forward or backward)
|
186 |
+
a1 = _numeric_feature_by_regex(r"/A:([0-9\-]+)\+", lab_curr)
|
187 |
+
a2 = _numeric_feature_by_regex(r"\+(\d+)\+", lab_curr)
|
188 |
+
a3 = _numeric_feature_by_regex(r"\+(\d+)/", lab_curr)
|
189 |
+
|
190 |
+
# number of mora in accent phrase
|
191 |
+
f1 = _numeric_feature_by_regex(r"/F:(\d+)_", lab_curr)
|
192 |
+
|
193 |
+
a2_next = _numeric_feature_by_regex(r"\+(\d+)\+", labels[n + 1])
|
194 |
+
# accent phrase border
|
195 |
+
if a3 == 1 and a2_next == 1 and p3 in "aeiouAEIOUNcl":
|
196 |
+
phones.append("#")
|
197 |
+
# pitch falling
|
198 |
+
elif a1 == 0 and a2_next == a2 + 1 and a2 != f1:
|
199 |
+
phones.append("]")
|
200 |
+
# pitch rising
|
201 |
+
elif a2 == 1 and a2_next == 2:
|
202 |
+
phones.append("[")
|
203 |
+
|
204 |
+
return phones
|
205 |
+
|
206 |
+
# Copied from espnet https://github.com/espnet/espnet/blob/master/espnet2/text/phoneme_tokenizer.py
|
207 |
+
def _numeric_feature_by_regex(regex, s):
|
208 |
+
match = re.search(regex, s)
|
209 |
+
if match is None:
|
210 |
+
return -50
|
211 |
+
return int(match.group(1))
|
212 |
+
|
213 |
+
def g2p(norm_text, with_prosody=True):
|
214 |
+
norm_text = text_normalize(norm_text)
|
215 |
+
phones = preprocess_jap(norm_text, with_prosody)
|
216 |
+
phones = [post_replace_ph(i) for i in phones]
|
217 |
+
# todo: implement tones and word2ph
|
218 |
+
return phones
|
219 |
+
|
220 |
+
|
221 |
+
if __name__ == "__main__":
|
222 |
+
phones = g2p("���んにちは, hello, AKITOです,よろしくお願いしますね!")
|
223 |
+
print(phones)
|
|
|
|
|
|
|
|