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 |
"\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 |
"\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 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|