Spaces:
Runtime error
Runtime error
add features.
Browse files- app.py +2 -2
- requirements.txt +3 -0
- text/cleaners.py +5 -1
- text/japanese.py +1 -1
- text/k2j.py +82 -0
- text/korean.py +504 -0
app.py
CHANGED
@@ -68,7 +68,7 @@ if __name__ == '__main__':
|
|
68 |
models_tts = []
|
69 |
name = 'AronaTTS'
|
70 |
lang = '日本語 (Japanese)'
|
71 |
-
example = '先生、今日は天気が本当にいいですね。'
|
72 |
config_path = f"pretrained_model/arona_ms_istft_vits.json"
|
73 |
model_path = f"pretrained_model/arona_ms_istft_vits.pth"
|
74 |
cover_path = f"pretrained_model/cover.gif"
|
@@ -97,7 +97,7 @@ if __name__ == '__main__':
|
|
97 |
app = gr.Blocks(css=css)
|
98 |
|
99 |
with app:
|
100 |
-
gr.Markdown("# BlueArchive
|
101 |
"![visitor badge](https://visitor-badge.glitch.me/badge?page_id=openduckparty.AronaTTS)\n\n")
|
102 |
|
103 |
for i, (name, cover_path, speakers, lang, example, symbols, tts_fn
|
|
|
68 |
models_tts = []
|
69 |
name = 'AronaTTS'
|
70 |
lang = '日本語 (Japanese)'
|
71 |
+
example = '[JA]先生、今日は天気が本当にいいですね。[JA][KO]센세야. 오늘 날 괜찮노.[KO]'
|
72 |
config_path = f"pretrained_model/arona_ms_istft_vits.json"
|
73 |
model_path = f"pretrained_model/arona_ms_istft_vits.pth"
|
74 |
cover_path = f"pretrained_model/cover.gif"
|
|
|
97 |
app = gr.Blocks(css=css)
|
98 |
|
99 |
with app:
|
100 |
+
gr.Markdown("# BlueArchive Arona TTS Using VITS Model\n"
|
101 |
"![visitor badge](https://visitor-badge.glitch.me/badge?page_id=openduckparty.AronaTTS)\n\n")
|
102 |
|
103 |
for i, (name, cover_path, speakers, lang, example, symbols, tts_fn
|
requirements.txt
CHANGED
@@ -8,6 +8,9 @@ scipy
|
|
8 |
tensorboard
|
9 |
ko-pron
|
10 |
jamo
|
|
|
|
|
|
|
11 |
torch
|
12 |
torchvision
|
13 |
torchaudio
|
|
|
8 |
tensorboard
|
9 |
ko-pron
|
10 |
jamo
|
11 |
+
g2pk2
|
12 |
+
eunjeon
|
13 |
+
cmake
|
14 |
torch
|
15 |
torchvision
|
16 |
torchaudio
|
text/cleaners.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import re
|
2 |
from text.japanese import japanese_to_romaji_with_accent
|
|
|
3 |
from text.symbols import symbols
|
4 |
|
|
|
5 |
_cleaner_cleans = re.compile('['+'^'.join(symbols)+']')
|
6 |
|
7 |
|
@@ -13,5 +15,7 @@ def japanese_cleaners(text):
|
|
13 |
|
14 |
def japanese_cleaners2(text):
|
15 |
text = japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
|
16 |
-
text = ''.
|
|
|
|
|
17 |
return text
|
|
|
1 |
import re
|
2 |
from text.japanese import japanese_to_romaji_with_accent
|
3 |
+
from text.k2j import korean2katakana
|
4 |
from text.symbols import symbols
|
5 |
|
6 |
+
|
7 |
_cleaner_cleans = re.compile('['+'^'.join(symbols)+']')
|
8 |
|
9 |
|
|
|
15 |
|
16 |
def japanese_cleaners2(text):
|
17 |
text = japanese_cleaners(text).replace('ts', 'ʦ').replace('...', '…')
|
18 |
+
text = '[JA]'+re.sub(r'\[KO\](.*?)\[KO\]', lambda x: korean2katakana(x.group(1))+'.', text)+'[JA]'
|
19 |
+
text = re.sub(r'\[JA\](.*?)\[JA\]', lambda x: japanese_cleaners(x.group(1))+' ', text)
|
20 |
+
text = ''.join(_cleaner_cleans.findall(text)).replace(' ', '')
|
21 |
return text
|
text/japanese.py
CHANGED
@@ -150,4 +150,4 @@ def japanese_to_ipa3(text):
|
|
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
|
|
|
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/k2j.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from text.cleaners import japanese_to_romaji_with_accent
|
2 |
+
from text.korean import (
|
3 |
+
join_jamos, j2hcj, h2j,
|
4 |
+
latin_to_hangul,
|
5 |
+
number_to_hangul,
|
6 |
+
g2pk,
|
7 |
+
)
|
8 |
+
import re
|
9 |
+
import jaconv
|
10 |
+
|
11 |
+
|
12 |
+
repl_lst = {
|
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 |
+
|
38 |
+
|
39 |
+
def get_word_list(text):
|
40 |
+
text = latin_to_hangul(text)
|
41 |
+
text = number_to_hangul(text)
|
42 |
+
text = g2pk(text)
|
43 |
+
text = j2hcj(h2j(text))
|
44 |
+
text = re.sub(r'([\u3131-\u3163])$', r'\1.', text)
|
45 |
+
return list(join_jamos(text.replace(' ', ' ')[:-1]))
|
46 |
+
|
47 |
+
|
48 |
+
def korean2katakana(text):
|
49 |
+
text = '/' + text.replace('/', ' ').replace('|', ' ').replace('^', ' ').replace(' ', ' ').replace(' ', '^')
|
50 |
+
word_lst = get_word_list(text)
|
51 |
+
new_lst = []
|
52 |
+
|
53 |
+
for i, s in enumerate(word_lst):
|
54 |
+
dh = list(j2hcj(h2j(s)))
|
55 |
+
if len(dh) == 3:
|
56 |
+
if dh[-1] == 'ㄴ':
|
57 |
+
dh[-1] = 'ㄴ'
|
58 |
+
|
59 |
+
elif dh[-1] == 'ㅁ' or dh[-1] == 'ㅇ':
|
60 |
+
dh[-1] = 'ㄴ|'
|
61 |
+
|
62 |
+
elif dh[-1] == 'ㄹ':
|
63 |
+
dh[-1] = '||/'
|
64 |
+
|
65 |
+
else: # ㄱ ㄷ ㅂ
|
66 |
+
dh[-1] = dh[-1]
|
67 |
+
|
68 |
+
|
69 |
+
dh.append('/')
|
70 |
+
new_lst.extend(dh)
|
71 |
+
|
72 |
+
kr = ''.join(new_lst)
|
73 |
+
|
74 |
+
for k, v in repl_lst.items():
|
75 |
+
kr = kr.replace(k, v)
|
76 |
+
kr2ro = japanese_to_romaji_with_accent(kr).replace('si', 'shi').replace('c', 'ts') \
|
77 |
+
.replace('ti', 'ティー').replace('tu', 'トゥー') \
|
78 |
+
.replace('di', 'ディー').replace('du', 'ドゥー')
|
79 |
+
result = jaconv.alphabet2kata(kr2ro)
|
80 |
+
result = result.replace('/', '').replace('|', 'ー').replace('^', '')
|
81 |
+
print(result)
|
82 |
+
return result
|
text/korean.py
ADDED
@@ -0,0 +1,504 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from jamo import h2j, j2hcj
|
3 |
+
import ko_pron
|
4 |
+
from g2pk2 import G2p
|
5 |
+
|
6 |
+
g2pk = G2p()
|
7 |
+
|
8 |
+
|
9 |
+
# This is a list of Korean classifiers preceded by pure Korean numerals.
|
10 |
+
_korean_classifiers = '군데 권 개 그루 닢 대 두 마리 모 모금 뭇 발 발짝 방 번 벌 보루 살 수 술 시 쌈 움큼 정 짝 채 척 첩 축 켤레 톨 통'
|
11 |
+
|
12 |
+
# List of (hangul, hangul divided) pairs:
|
13 |
+
_hangul_divided = [(re.compile('%s' % x[0]), x[1]) for x in [
|
14 |
+
('ㄳ', 'ㄱㅅ'),
|
15 |
+
('ㄵ', 'ㄴㅈ'),
|
16 |
+
('ㄶ', 'ㄴㅎ'),
|
17 |
+
('ㄺ', 'ㄹㄱ'),
|
18 |
+
('ㄻ', 'ㄹㅁ'),
|
19 |
+
('ㄼ', 'ㄹㅂ'),
|
20 |
+
('ㄽ', 'ㄹㅅ'),
|
21 |
+
('ㄾ', 'ㄹㅌ'),
|
22 |
+
('ㄿ', 'ㄹㅍ'),
|
23 |
+
('ㅀ', 'ㄹㅎ'),
|
24 |
+
('ㅄ', 'ㅂㅅ'),
|
25 |
+
('ㅘ', 'ㅗㅏ'),
|
26 |
+
('ㅙ', 'ㅗㅐ'),
|
27 |
+
('ㅚ', 'ㅗㅣ'),
|
28 |
+
('ㅝ', 'ㅜㅓ'),
|
29 |
+
('ㅞ', 'ㅜㅔ'),
|
30 |
+
('ㅟ', 'ㅜㅣ'),
|
31 |
+
('ㅢ', 'ㅡㅣ'),
|
32 |
+
('ㅑ', 'ㅣㅏ'),
|
33 |
+
('ㅒ', 'ㅣㅐ'),
|
34 |
+
('ㅕ', 'ㅣㅓ'),
|
35 |
+
('ㅖ', 'ㅣㅔ'),
|
36 |
+
('ㅛ', 'ㅣㅗ'),
|
37 |
+
('ㅠ', 'ㅣㅜ')
|
38 |
+
]]
|
39 |
+
|
40 |
+
# List of (Latin alphabet, hangul) pairs:
|
41 |
+
_latin_to_hangul = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
|
42 |
+
('a', '에이'),
|
43 |
+
('b', '비'),
|
44 |
+
('c', '시'),
|
45 |
+
('d', '디'),
|
46 |
+
('e', '이'),
|
47 |
+
('f', '에프'),
|
48 |
+
('g', '지'),
|
49 |
+
('h', '에이치'),
|
50 |
+
('i', '아이'),
|
51 |
+
('j', '제이'),
|
52 |
+
('k', '케이'),
|
53 |
+
('l', '엘'),
|
54 |
+
('m', '엠'),
|
55 |
+
('n', '엔'),
|
56 |
+
('o', '오'),
|
57 |
+
('p', '피'),
|
58 |
+
('q', '큐'),
|
59 |
+
('r', '아르'),
|
60 |
+
('s', '에스'),
|
61 |
+
('t', '티'),
|
62 |
+
('u', '유'),
|
63 |
+
('v', '브이'),
|
64 |
+
('w', '더블유'),
|
65 |
+
('x', '엑스'),
|
66 |
+
('y', '와이'),
|
67 |
+
('z', '제트')
|
68 |
+
]]
|
69 |
+
|
70 |
+
# List of (ipa, lazy ipa) pairs:
|
71 |
+
_ipa_to_lazy_ipa = [(re.compile('%s' % x[0], re.IGNORECASE), x[1]) for x in [
|
72 |
+
('t͡ɕ','ʧ'),
|
73 |
+
('d͡ʑ','ʥ'),
|
74 |
+
('ɲ','n^'),
|
75 |
+
('ɕ','ʃ'),
|
76 |
+
('ʷ','w'),
|
77 |
+
('ɭ','l`'),
|
78 |
+
('ʎ','ɾ'),
|
79 |
+
('ɣ','ŋ'),
|
80 |
+
('ɰ','ɯ'),
|
81 |
+
('ʝ','j'),
|
82 |
+
('ʌ','ə'),
|
83 |
+
('ɡ','g'),
|
84 |
+
('\u031a','#'),
|
85 |
+
('\u0348','='),
|
86 |
+
('\u031e',''),
|
87 |
+
('\u0320',''),
|
88 |
+
('\u0339','')
|
89 |
+
]]
|
90 |
+
|
91 |
+
|
92 |
+
def latin_to_hangul(text):
|
93 |
+
for regex, replacement in _latin_to_hangul:
|
94 |
+
text = re.sub(regex, replacement, text)
|
95 |
+
return text
|
96 |
+
|
97 |
+
|
98 |
+
def divide_hangul(text):
|
99 |
+
text = j2hcj(h2j(text))
|
100 |
+
for regex, replacement in _hangul_divided:
|
101 |
+
text = re.sub(regex, replacement, text)
|
102 |
+
return text
|
103 |
+
|
104 |
+
|
105 |
+
def hangul_number(num, sino=True):
|
106 |
+
'''Reference https://github.com/Kyubyong/g2pK'''
|
107 |
+
num = re.sub(',', '', num)
|
108 |
+
|
109 |
+
if num == '0':
|
110 |
+
return '영'
|
111 |
+
if not sino and num == '20':
|
112 |
+
return '스무'
|
113 |
+
|
114 |
+
digits = '123456789'
|
115 |
+
names = '일이삼사오육칠팔구'
|
116 |
+
digit2name = {d: n for d, n in zip(digits, names)}
|
117 |
+
|
118 |
+
modifiers = '한 두 세 네 다섯 여섯 일곱 여덟 아홉'
|
119 |
+
decimals = '열 스물 서른 마흔 쉰 예순 일흔 여든 아흔'
|
120 |
+
digit2mod = {d: mod for d, mod in zip(digits, modifiers.split())}
|
121 |
+
digit2dec = {d: dec for d, dec in zip(digits, decimals.split())}
|
122 |
+
|
123 |
+
spelledout = []
|
124 |
+
for i, digit in enumerate(num):
|
125 |
+
i = len(num) - i - 1
|
126 |
+
if sino:
|
127 |
+
if i == 0:
|
128 |
+
name = digit2name.get(digit, '')
|
129 |
+
elif i == 1:
|
130 |
+
name = digit2name.get(digit, '') + '십'
|
131 |
+
name = name.replace('일십', '십')
|
132 |
+
else:
|
133 |
+
if i == 0:
|
134 |
+
name = digit2mod.get(digit, '')
|
135 |
+
elif i == 1:
|
136 |
+
name = digit2dec.get(digit, '')
|
137 |
+
if digit == '0':
|
138 |
+
if i % 4 == 0:
|
139 |
+
last_three = spelledout[-min(3, len(spelledout)):]
|
140 |
+
if ''.join(last_three) == '':
|
141 |
+
spelledout.append('')
|
142 |
+
continue
|
143 |
+
else:
|
144 |
+
spelledout.append('')
|
145 |
+
continue
|
146 |
+
if i == 2:
|
147 |
+
name = digit2name.get(digit, '') + '백'
|
148 |
+
name = name.replace('일백', '백')
|
149 |
+
elif i == 3:
|
150 |
+
name = digit2name.get(digit, '') + '천'
|
151 |
+
name = name.replace('일천', '천')
|
152 |
+
elif i == 4:
|
153 |
+
name = digit2name.get(digit, '') + '만'
|
154 |
+
name = name.replace('일만', '만')
|
155 |
+
elif i == 5:
|
156 |
+
name = digit2name.get(digit, '') + '십'
|
157 |
+
name = name.replace('일십', '십')
|
158 |
+
elif i == 6:
|
159 |
+
name = digit2name.get(digit, '') + '백'
|
160 |
+
name = name.replace('일백', '백')
|
161 |
+
elif i == 7:
|
162 |
+
name = digit2name.get(digit, '') + '천'
|
163 |
+
name = name.replace('일천', '천')
|
164 |
+
elif i == 8:
|
165 |
+
name = digit2name.get(digit, '') + '억'
|
166 |
+
elif i == 9:
|
167 |
+
name = digit2name.get(digit, '') + '십'
|
168 |
+
elif i == 10:
|
169 |
+
name = digit2name.get(digit, '') + '백'
|
170 |
+
elif i == 11:
|
171 |
+
name = digit2name.get(digit, '') + '천'
|
172 |
+
elif i == 12:
|
173 |
+
name = digit2name.get(digit, '') + '조'
|
174 |
+
elif i == 13:
|
175 |
+
name = digit2name.get(digit, '') + '십'
|
176 |
+
elif i == 14:
|
177 |
+
name = digit2name.get(digit, '') + '백'
|
178 |
+
elif i == 15:
|
179 |
+
name = digit2name.get(digit, '') + '천'
|
180 |
+
spelledout.append(name)
|
181 |
+
return ''.join(elem for elem in spelledout)
|
182 |
+
|
183 |
+
|
184 |
+
def number_to_hangul(text):
|
185 |
+
'''Reference https://github.com/Kyubyong/g2pK'''
|
186 |
+
tokens = set(re.findall(r'(\d[\d,]*)([\uac00-\ud71f]+)', text))
|
187 |
+
for token in tokens:
|
188 |
+
num, classifier = token
|
189 |
+
if classifier[:2] in _korean_classifiers or classifier[0] in _korean_classifiers:
|
190 |
+
spelledout = hangul_number(num, sino=False)
|
191 |
+
else:
|
192 |
+
spelledout = hangul_number(num, sino=True)
|
193 |
+
text = text.replace(f'{num}{classifier}', f'{spelledout}{classifier}')
|
194 |
+
# digit by digit for remaining digits
|
195 |
+
digits = '0123456789'
|
196 |
+
names = '영일이삼사오육칠팔구'
|
197 |
+
for d, n in zip(digits, names):
|
198 |
+
text = text.replace(d, n)
|
199 |
+
return text
|
200 |
+
|
201 |
+
|
202 |
+
def korean_to_lazy_ipa(text):
|
203 |
+
text = latin_to_hangul(text)
|
204 |
+
text = number_to_hangul(text)
|
205 |
+
text=re.sub('[\uac00-\ud7af]+',lambda x:ko_pron.romanise(x.group(0),'ipa').split('] ~ [')[0],text)
|
206 |
+
for regex, replacement in _ipa_to_lazy_ipa:
|
207 |
+
text = re.sub(regex, replacement, text)
|
208 |
+
return text
|
209 |
+
|
210 |
+
|
211 |
+
def korean_to_ipa(text):
|
212 |
+
text = korean_to_lazy_ipa(text)
|
213 |
+
return text.replace('ʧ','tʃ').replace('ʥ','dʑ')
|
214 |
+
|
215 |
+
def korean_to_ipa2(text):
|
216 |
+
text = latin_to_hangul(text)
|
217 |
+
text = number_to_hangul(text)
|
218 |
+
text = g2pk(text)
|
219 |
+
text=re.sub('[\uac00-\ud7af]+',lambda x:ko_pron.romanise(x.group(0),'ipa').split('] ~ [')[0],text)
|
220 |
+
for regex, replacement in _ipa_to_lazy_ipa:
|
221 |
+
text = re.sub(regex, replacement, text)
|
222 |
+
text = text.replace('ʧ','tʃ').replace('ʥ','dʑ')
|
223 |
+
return text
|
224 |
+
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
import itertools
|
231 |
+
|
232 |
+
INITIAL = 0x001
|
233 |
+
MEDIAL = 0x010
|
234 |
+
FINAL = 0x100
|
235 |
+
CHAR_LISTS = {
|
236 |
+
INITIAL: list(map(chr, [
|
237 |
+
0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139,
|
238 |
+
0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147,
|
239 |
+
0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d,
|
240 |
+
0x314e
|
241 |
+
])),
|
242 |
+
MEDIAL: list(map(chr, [
|
243 |
+
0x314f, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154,
|
244 |
+
0x3155, 0x3156, 0x3157, 0x3158, 0x3159, 0x315a,
|
245 |
+
0x315b, 0x315c, 0x315d, 0x315e, 0x315f, 0x3160,
|
246 |
+
0x3161, 0x3162, 0x3163
|
247 |
+
])),
|
248 |
+
FINAL: list(map(chr, [
|
249 |
+
0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136,
|
250 |
+
0x3137, 0x3139, 0x313a, 0x313b, 0x313c, 0x313d,
|
251 |
+
0x313e, 0x313f, 0x3140, 0x3141, 0x3142, 0x3144,
|
252 |
+
0x3145, 0x3146, 0x3147, 0x3148, 0x314a, 0x314b,
|
253 |
+
0x314c, 0x314d, 0x314e
|
254 |
+
]))
|
255 |
+
}
|
256 |
+
CHAR_INITIALS = CHAR_LISTS[INITIAL]
|
257 |
+
CHAR_MEDIALS = CHAR_LISTS[MEDIAL]
|
258 |
+
CHAR_FINALS = CHAR_LISTS[FINAL]
|
259 |
+
CHAR_SETS = {k: set(v) for k, v in CHAR_LISTS.items()}
|
260 |
+
CHARSET = set(itertools.chain(*CHAR_SETS.values()))
|
261 |
+
CHAR_INDICES = {k: {c: i for i, c in enumerate(v)}
|
262 |
+
for k, v in CHAR_LISTS.items()}
|
263 |
+
|
264 |
+
|
265 |
+
def is_hangul_syllable(c):
|
266 |
+
return 0xac00 <= ord(c) <= 0xd7a3 # Hangul Syllables
|
267 |
+
|
268 |
+
|
269 |
+
def is_hangul_jamo(c):
|
270 |
+
return 0x1100 <= ord(c) <= 0x11ff # Hangul Jamo
|
271 |
+
|
272 |
+
|
273 |
+
def is_hangul_compat_jamo(c):
|
274 |
+
return 0x3130 <= ord(c) <= 0x318f # Hangul Compatibility Jamo
|
275 |
+
|
276 |
+
|
277 |
+
def is_hangul_jamo_exta(c):
|
278 |
+
return 0xa960 <= ord(c) <= 0xa97f # Hangul Jamo Extended-A
|
279 |
+
|
280 |
+
|
281 |
+
def is_hangul_jamo_extb(c):
|
282 |
+
return 0xd7b0 <= ord(c) <= 0xd7ff # Hangul Jamo Extended-B
|
283 |
+
|
284 |
+
|
285 |
+
def is_hangul(c):
|
286 |
+
return (is_hangul_syllable(c) or
|
287 |
+
is_hangul_jamo(c) or
|
288 |
+
is_hangul_compat_jamo(c) or
|
289 |
+
is_hangul_jamo_exta(c) or
|
290 |
+
is_hangul_jamo_extb(c))
|
291 |
+
|
292 |
+
|
293 |
+
def is_supported_hangul(c):
|
294 |
+
return is_hangul_syllable(c) or is_hangul_compat_jamo(c)
|
295 |
+
|
296 |
+
|
297 |
+
def check_hangul(c, jamo_only=False):
|
298 |
+
if not ((jamo_only or is_hangul_compat_jamo(c)) or is_supported_hangul(c)):
|
299 |
+
raise ValueError(f"'{c}' is not a supported hangul character. "
|
300 |
+
f"'Hangul Syllables' (0xac00 ~ 0xd7a3) and "
|
301 |
+
f"'Hangul Compatibility Jamos' (0x3130 ~ 0x318f) are "
|
302 |
+
f"supported at the moment.")
|
303 |
+
|
304 |
+
|
305 |
+
def get_jamo_type(c):
|
306 |
+
check_hangul(c)
|
307 |
+
assert is_hangul_compat_jamo(c), f"not a jamo: {ord(c):x}"
|
308 |
+
return sum(t for t, s in CHAR_SETS.items() if c in s)
|
309 |
+
|
310 |
+
|
311 |
+
def split_syllable_char(c):
|
312 |
+
"""
|
313 |
+
Splits a given korean syllable into its components. Each component is
|
314 |
+
represented by Unicode in 'Hangul Compatibility Jamo' range.
|
315 |
+
|
316 |
+
Arguments:
|
317 |
+
c: A Korean character.
|
318 |
+
|
319 |
+
Returns:
|
320 |
+
A triple (initial, medial, final) of Hangul Compatibility Jamos.
|
321 |
+
If no jamo corresponds to a position, `None` is returned there.
|
322 |
+
|
323 |
+
Example:
|
324 |
+
>>> split_syllable_char("안")
|
325 |
+
("ㅇ", "ㅏ", "ㄴ")
|
326 |
+
>>> split_syllable_char("고")
|
327 |
+
("ㄱ", "ㅗ", None)
|
328 |
+
>>> split_syllable_char("ㅗ")
|
329 |
+
(None, "ㅗ", None)
|
330 |
+
>>> split_syllable_char("ㅇ")
|
331 |
+
("ㅇ", None, None)
|
332 |
+
"""
|
333 |
+
check_hangul(c)
|
334 |
+
if len(c) != 1:
|
335 |
+
raise ValueError("Input string must have exactly one character.")
|
336 |
+
|
337 |
+
init, med, final = None, None, None
|
338 |
+
if is_hangul_syllable(c):
|
339 |
+
offset = ord(c) - 0xac00
|
340 |
+
x = (offset - offset % 28) // 28
|
341 |
+
init, med, final = x // 21, x % 21, offset % 28
|
342 |
+
if not final:
|
343 |
+
final = None
|
344 |
+
else:
|
345 |
+
final -= 1
|
346 |
+
else:
|
347 |
+
pos = get_jamo_type(c)
|
348 |
+
if pos & INITIAL == INITIAL:
|
349 |
+
pos = INITIAL
|
350 |
+
elif pos & MEDIAL == MEDIAL:
|
351 |
+
pos = MEDIAL
|
352 |
+
elif pos & FINAL == FINAL:
|
353 |
+
pos = FINAL
|
354 |
+
idx = CHAR_INDICES[pos][c]
|
355 |
+
if pos == INITIAL:
|
356 |
+
init = idx
|
357 |
+
elif pos == MEDIAL:
|
358 |
+
med = idx
|
359 |
+
else:
|
360 |
+
final = idx
|
361 |
+
return tuple(CHAR_LISTS[pos][idx] if idx is not None else None
|
362 |
+
for pos, idx in
|
363 |
+
zip([INITIAL, MEDIAL, FINAL], [init, med, final]))
|
364 |
+
|
365 |
+
|
366 |
+
def split_syllables(s, ignore_err=True, pad=None):
|
367 |
+
"""
|
368 |
+
Performs syllable-split on a string.
|
369 |
+
|
370 |
+
Arguments:
|
371 |
+
s (str): A string (possibly mixed with non-Hangul characters).
|
372 |
+
ignore_err (bool): If set False, it ensures that all characters in
|
373 |
+
the string are Hangul-splittable and throws a ValueError otherwise.
|
374 |
+
(default: True)
|
375 |
+
pad (str): Pad empty jamo positions (initial, medial, or final) with
|
376 |
+
`pad` character. This is useful for cases where fixed-length
|
377 |
+
strings are needed. (default: None)
|
378 |
+
|
379 |
+
Returns:
|
380 |
+
Hangul-split string
|
381 |
+
|
382 |
+
Example:
|
383 |
+
>>> split_syllables("안녕하세요")
|
384 |
+
"ㅇㅏㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ"
|
385 |
+
>>> split_syllables("안녕하세요~~", ignore_err=False)
|
386 |
+
ValueError: encountered an unsupported character: ~ (0x7e)
|
387 |
+
>>> split_syllables("안녕하세요ㅛ", pad="x")
|
388 |
+
'ㅇㅏㄴㄴㅕㅇㅎㅏxㅅㅔxㅇㅛxxㅛx'
|
389 |
+
"""
|
390 |
+
|
391 |
+
def try_split(c):
|
392 |
+
try:
|
393 |
+
return split_syllable_char(c)
|
394 |
+
except ValueError:
|
395 |
+
if ignore_err:
|
396 |
+
return (c,)
|
397 |
+
raise ValueError(f"encountered an unsupported character: "
|
398 |
+
f"{c} (0x{ord(c):x})")
|
399 |
+
|
400 |
+
s = map(try_split, s)
|
401 |
+
if pad is not None:
|
402 |
+
tuples = map(lambda x: tuple(pad if y is None else y for y in x), s)
|
403 |
+
else:
|
404 |
+
tuples = map(lambda x: filter(None, x), s)
|
405 |
+
return "".join(itertools.chain(*tuples))
|
406 |
+
|
407 |
+
|
408 |
+
def join_jamos_char(init, med, final=None):
|
409 |
+
"""
|
410 |
+
Combines jamos into a single syllable.
|
411 |
+
|
412 |
+
Arguments:
|
413 |
+
init (str): Initial jao.
|
414 |
+
med (str): Medial jamo.
|
415 |
+
final (str): Final jamo. If not supplied, the final syllable is made
|
416 |
+
without the final. (default: None)
|
417 |
+
|
418 |
+
Returns:
|
419 |
+
A Korean syllable.
|
420 |
+
"""
|
421 |
+
chars = (init, med, final)
|
422 |
+
for c in filter(None, chars):
|
423 |
+
check_hangul(c, jamo_only=True)
|
424 |
+
|
425 |
+
idx = tuple(CHAR_INDICES[pos][c] if c is not None else c
|
426 |
+
for pos, c in zip((INITIAL, MEDIAL, FINAL), chars))
|
427 |
+
init_idx, med_idx, final_idx = idx
|
428 |
+
# final index must be shifted once as
|
429 |
+
# final index with 0 points to syllables without final
|
430 |
+
final_idx = 0 if final_idx is None else final_idx + 1
|
431 |
+
return chr(0xac00 + 28 * 21 * init_idx + 28 * med_idx + final_idx)
|
432 |
+
|
433 |
+
|
434 |
+
def join_jamos(s, ignore_err=True):
|
435 |
+
"""
|
436 |
+
Combines a sequence of jamos to produce a sequence of syllables.
|
437 |
+
|
438 |
+
Arguments:
|
439 |
+
s (str): A string (possible mixed with non-jamo characters).
|
440 |
+
ignore_err (bool): If set False, it will ensure that all characters
|
441 |
+
will be consumed for the making of syllables. It will throw a
|
442 |
+
ValueError when it fails to do so. (default: True)
|
443 |
+
|
444 |
+
Returns:
|
445 |
+
A string
|
446 |
+
|
447 |
+
Example:
|
448 |
+
>>> join_jamos("ㅇㅏㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ")
|
449 |
+
"안녕하세요"
|
450 |
+
>>> join_jamos("ㅇㅏㄴㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ")
|
451 |
+
"안ㄴ녕하세요"
|
452 |
+
>>> join_jamos()
|
453 |
+
"""
|
454 |
+
last_t = 0
|
455 |
+
queue = []
|
456 |
+
new_string = ""
|
457 |
+
|
458 |
+
def flush(n=0):
|
459 |
+
new_queue = []
|
460 |
+
while len(queue) > n:
|
461 |
+
new_queue.append(queue.pop())
|
462 |
+
if len(new_queue) == 1:
|
463 |
+
if not ignore_err:
|
464 |
+
raise ValueError(f"invalid jamo character: {new_queue[0]}")
|
465 |
+
result = new_queue[0]
|
466 |
+
elif len(new_queue) >= 2:
|
467 |
+
try:
|
468 |
+
result = join_jamos_char(*new_queue)
|
469 |
+
except (ValueError, KeyError):
|
470 |
+
# Invalid jamo combination
|
471 |
+
if not ignore_err:
|
472 |
+
raise ValueError(f"invalid jamo characters: {new_queue}")
|
473 |
+
result = "".join(new_queue)
|
474 |
+
else:
|
475 |
+
result = None
|
476 |
+
return result
|
477 |
+
|
478 |
+
for c in s:
|
479 |
+
if c not in CHARSET:
|
480 |
+
if queue:
|
481 |
+
new_c = flush() + c
|
482 |
+
else:
|
483 |
+
new_c = c
|
484 |
+
last_t = 0
|
485 |
+
else:
|
486 |
+
t = get_jamo_type(c)
|
487 |
+
new_c = None
|
488 |
+
if t & FINAL == FINAL:
|
489 |
+
if not (last_t == MEDIAL):
|
490 |
+
new_c = flush()
|
491 |
+
elif t == INITIAL:
|
492 |
+
new_c = flush()
|
493 |
+
elif t == MEDIAL:
|
494 |
+
if last_t & INITIAL == INITIAL:
|
495 |
+
new_c = flush(1)
|
496 |
+
else:
|
497 |
+
new_c = flush()
|
498 |
+
last_t = t
|
499 |
+
queue.insert(0, c)
|
500 |
+
if new_c:
|
501 |
+
new_string += new_c
|
502 |
+
if queue:
|
503 |
+
new_string += flush()
|
504 |
+
return new_string
|