Spaces:
Running
Running
# Convert Japanese text to phonemes which is | |
# compatible with Julius https://github.com/julius-speech/segmentation-kit | |
import re | |
import unicodedata | |
from transformers import AutoTokenizer | |
from text import punctuation, symbols | |
from num2words import num2words | |
import pyopenjtalk | |
import jaconv | |
# Mapping of hiragana to phonetic representation | |
hiragana_map = { | |
"ใใใ": " v a", | |
"ใใใ": " v i", | |
"ใใใ": " v e", | |
"ใใใ": " v o", | |
"ใใใ ": " by u", | |
"ใ ใ": " v u", | |
# ใ็ญใฎๅฆ็ใ่ฟฝๅ | |
"ใใ": " v a", | |
"ใใ": " v i", | |
"ใใ": " v e", | |
"ใใ": " v o", | |
"ใใ ": " by u", | |
# 2ๆๅญใใใชใๅคๆ่ฆๅ | |
"ใใ": " a a", | |
"ใใ": " i i", | |
"ใใ": " i e", | |
"ใใ": " y a", | |
"ใใ ": " u:", | |
"ใใ": " e e", | |
"ใใ": " o:", | |
"ใใ": " k a:", | |
"ใใ": " k i:", | |
"ใใ ": " k u:", | |
"ใใ": " ky a", | |
"ใใ ": " ky u", | |
"ใใ": " ky o", | |
"ใใ": " k e:", | |
"ใใ": " k o:", | |
"ใใ": " g a:", | |
"ใใ": " g i:", | |
"ใใ ": " g u:", | |
"ใใ": " gy a", | |
"ใใ ": " gy u", | |
"ใใ": " gy o", | |
"ใใ": " g e:", | |
"ใใ": " g o:", | |
"ใใ": " s a:", | |
"ใใ": " sh i", | |
"ใใ ": " s u:", | |
"ใใ": " sh a", | |
"ใใ ": " sh u", | |
"ใใ": " sh o", | |
"ใใ": " s e:", | |
"ใใ": " s o:", | |
"ใใ": " z a:", | |
"ใใ": " j i:", | |
"ใใ ": " z u:", | |
"ใใ": " zy a", | |
"ใใ ": " zy u", | |
"ใใ": " zy o", | |
"ใใ": " z e:", | |
"ใใ": " z o:", | |
"ใใ": " t a:", | |
"ใกใ": " ch i", | |
"ใคใ": " ts a", | |
"ใคใ": " ts i", | |
"ใคใ ": " ts u", | |
"ใคใ": " ch a", | |
"ใคใ ": " ch u", | |
"ใคใ": " ch o", | |
"ใคใ": " ts e", | |
"ใคใ": " ts o", | |
"ใฆใ": " t e:", | |
"ใจใ": " t o:", | |
"ใ ใ": " d a:", | |
"ใขใ": " j i:", | |
"ใฅใ ": " d u:", | |
"ใฅใ": " zy a", | |
"ใฅใ ": " zy u", | |
"ใฅใ": " zy o", | |
"ใงใ": " d e:", | |
"ใชใ": " n a:", | |
"ใซใ": " n i:", | |
"ใฌใ ": " n u:", | |
"ใฌใ": " ny a", | |
"ใฌใ ": " ny u", | |
"ใฌใ": " ny o", | |
"ใญใ": " n e:", | |
"ใฎใ": " n o:", | |
"ใฏใ": " h a:", | |
"ใฒใ": " h i:", | |
"ใตใ ": " f u:", | |
"ใตใ": " hy a", | |
"ใธใ": " h e:", | |
"ใปใ": " h o:", | |
"ใฐใ": " b a:", | |
"ใณใ": " b i:", | |
"ใถใ ": " b u:", | |
"ใถใ ": " by u", | |
"ในใ": " b e:", | |
"ใผใ": " b o:", | |
"ใฑใ": " p a:", | |
"ใดใ": " p i:", | |
"ใทใ ": " p u:", | |
"ใทใ": " py a", | |
"ใทใ ": " py u", | |
"ใทใ": " py o", | |
"ใบใ": " p e:", | |
"ใฝใ": " p o:", | |
"ใพใ": " m a:", | |
"ใฟใ": " m i:", | |
"ใใ ": " m u:", | |
"ใใ": " my a", | |
"ใใ ": " my u", | |
"ใใ": " my o", | |
"ใใ": " m e:", | |
"ใใ": " m o:", | |
"ใใ": " y a:", | |
"ใใ ": " y u:", | |
"ใใ": " y a:", | |
"ใใ ": " y u:", | |
"ใใ": " y o:", | |
"ใใ": " y o:", | |
"ใใ": " r a:", | |
"ใใ": " r i:", | |
"ใใ ": " r u:", | |
"ใใ": " ry a", | |
"ใใ ": " ry u", | |
"ใใ": " ry o", | |
"ใใ": " r e:", | |
"ใใ": " r o:", | |
"ใใ": " w a:", | |
"ใใ": " o:", | |
"ใใ": " b u", | |
"ใงใ": " d i", | |
"ใงใ": " dy a", | |
"ใงใ ": " dy u", | |
"ใงใ": " dy o", | |
"ใฆใ": " t i", | |
"ใฆใ": " ty a", | |
"ใฆใ ": " ty u", | |
"ใฆใ": " ty o", | |
"ใใ": " s i", | |
"ใใ": " z u", | |
"ใใ": " z i", | |
"ใใ": " z e", | |
"ใใ": " z o", | |
"ใใ": " ky a", | |
"ใใ ": " ky u", | |
"ใใ": " ky o", | |
"ใใ": " sh a", | |
"ใใ ": " sh u", | |
"ใใ": " sh e", | |
"ใใ": " sh o", | |
"ใกใ": " ch a", | |
"ใกใ ": " ch u", | |
"ใกใ": " ch e", | |
"ใกใ": " ch o", | |
"ใจใ ": " t u", | |
"ใจใ": " ty a", | |
"ใจใ ": " ty u", | |
"ใจใ": " ty o", | |
"ใฉใ": " d o ", | |
"ใฉใ ": " d u", | |
"ใฉใ": " dy a", | |
"ใฉใ ": " dy u", | |
"ใฉใ": " dy o", | |
"ใฉใ": " d o:", | |
"ใซใ": " ny a", | |
"ใซใ ": " ny u", | |
"ใซใ": " ny o", | |
"ใฒใ": " hy a", | |
"ใฒใ ": " hy u", | |
"ใฒใ": " hy o", | |
"ใฟใ": " my a", | |
"ใฟใ ": " my u", | |
"ใฟใ": " my o", | |
"ใใ": " ry a", | |
"ใใ ": " ry u", | |
"ใใ": " ry o", | |
"ใใ": " gy a", | |
"ใใ ": " gy u", | |
"ใใ": " gy o", | |
"ใขใ": " j e", | |
"ใขใ": " j a", | |
"ใขใ ": " j u", | |
"ใขใ": " j o", | |
"ใใ": " j e", | |
"ใใ": " j a", | |
"ใใ ": " j u", | |
"ใใ": " j o", | |
"ใณใ": " by a", | |
"ใณใ ": " by u", | |
"ใณใ": " by o", | |
"ใดใ": " py a", | |
"ใดใ ": " py u", | |
"ใดใ": " py o", | |
"ใใ": " u a", | |
"ใใ": " w i", | |
"ใใ": " w e", | |
"ใใ": " w o", | |
"ใตใ": " f a", | |
"ใตใ": " f i", | |
"ใตใ ": " hy u", | |
"ใตใ": " hy o", | |
"ใตใ": " f e", | |
"ใตใ": " f o", | |
# 1้ณใใใชใๅคๆ่ฆๅ | |
"ใ": " a", | |
"ใ": " i", | |
"ใ": " u", | |
"ใ": " v u", # ใใฎๅฆ็ใ่ฟฝๅ | |
"ใ": " e", | |
"ใ": " o", | |
"ใ": " k a", | |
"ใ": " k i", | |
"ใ": " k u", | |
"ใ": " k e", | |
"ใ": " k o", | |
"ใ": " s a", | |
"ใ": " sh i", | |
"ใ": " s u", | |
"ใ": " s e", | |
"ใ": " s o", | |
"ใ": " t a", | |
"ใก": " ch i", | |
"ใค": " ts u", | |
"ใฆ": " t e", | |
"ใจ": " t o", | |
"ใช": " n a", | |
"ใซ": " n i", | |
"ใฌ": " n u", | |
"ใญ": " n e", | |
"ใฎ": " n o", | |
"ใฏ": " h a", | |
"ใฒ": " h i", | |
"ใต": " f u", | |
"ใธ": " h e", | |
"ใป": " h o", | |
"ใพ": " m a", | |
"ใฟ": " m i", | |
"ใ": " m u", | |
"ใ": " m e", | |
"ใ": " m o", | |
"ใ": " r a", | |
"ใ": " r i", | |
"ใ": " r u", | |
"ใ": " r e", | |
"ใ": " r o", | |
"ใ": " g a", | |
"ใ": " g i", | |
"ใ": " g u", | |
"ใ": " g e", | |
"ใ": " g o", | |
"ใ": " z a", | |
"ใ": " j i", | |
"ใ": " z u", | |
"ใ": " z e", | |
"ใ": " z o", | |
"ใ ": " d a", | |
"ใข": " j i", | |
"ใฅ": " z u", | |
"ใง": " d e", | |
"ใฉ": " d o", | |
"ใฐ": " b a", | |
"ใณ": " b i", | |
"ใถ": " b u", | |
"ใน": " b e", | |
"ใผ": " b o", | |
"ใฑ": " p a", | |
"ใด": " p i", | |
"ใท": " p u", | |
"ใบ": " p e", | |
"ใฝ": " p o", | |
"ใ": " y a", | |
"ใ": " y u", | |
"ใ": " y o", | |
"ใ": " w a", | |
"ใ": " i", | |
"ใ": " e", | |
"ใ": " N", | |
"ใฃ": " q", | |
# ใใใพใงใซๅฆ็ใใใฆใชใ ใใใ ใใ ใฏใใฎใพใพๅคงๆๅญๆฑใ | |
"ใ": " a", | |
"ใ": " i", | |
"ใ ": " u", | |
"ใ": " e", | |
"ใ": " o", | |
"ใ": " w a", | |
# ้ท้ณใฎๅฆ็ | |
# for (pattern, replace_str) in JULIUS_LONG_VOWEL: | |
# text = pattern.sub(replace_str, text) | |
# text = text.replace("o u", "o:") # ใใ -> ใใผใฎ้ณไพฟ | |
"ใผ": ":", | |
"ใ": ":", | |
"โ": ":", | |
"-": ":", | |
# ใใฎไป็นๅฅใชๅฆ็ | |
"ใ": " o", | |
# ใใใพใงใซๅฆ็ใใใฆใใชใใ ็ญใใใฎใพใพๅคงๆๅญๆฑใ๏ผ่ฟฝๅ ๏ผ | |
"ใ": " y a", | |
"ใ ": " y u", | |
"ใ": " y o", | |
} | |
def hiragana2p(txt: str) -> str: | |
""" | |
Modification of `jaconv.hiragana2julius`. | |
- avoid using `:`, instead, `ใใผใผใผ` -> `a a a a`. | |
- avoid converting `o u` to `o o` (because the input is already actual `yomi`). | |
- avoid using `N` for `ใ` (for compatibility) | |
- use `v` for `ใ` related text. | |
- add bare `ใ` `ใ ` `ใ` to `y a` `y u` `y o` (for compatibility). | |
""" | |
result = [] | |
skip = 0 | |
for i in range(len(txt)): | |
if skip: | |
skip -= 1 | |
continue | |
for length in range(3, 0, -1): | |
if txt[i : i + length] in hiragana_map: | |
result.append(hiragana_map[txt[i : i + length]]) | |
skip = length - 1 | |
break | |
txt = "".join(result) | |
txt = txt.strip() | |
txt = txt.replace(":+", ":") | |
# ใใใพใง`jaconv.hiragana2julius`ใจ้ณไพฟๅฆ็ใจ้ท้ณๅฆ็ใใฎใใใฆๅใ | |
# ใใใใ`k a:: k i:`โ`k a a a k i i`ใฎใใใซ`:`ใฎๆฐใ ใ็นฐใ่ฟใๅฆ็ | |
pattern = r"(\w)(:*)" | |
replacement = lambda m: m.group(1) + (" " + m.group(1)) * len(m.group(2)) | |
txt = re.sub(pattern, replacement, txt) | |
txt = txt.replace("N", "n") # ไฟ้ณใฎNใnใซๅคๆ | |
return txt | |
def kata2phoneme(text: str) -> str: | |
"""Convert katakana text to phonemes.""" | |
text = text.strip() | |
if text == "ใผ": | |
return ["ใผ"] | |
elif text.startswith("ใผ"): | |
return ["ใผ"] + kata2phoneme(text[1:]) | |
res = [] | |
prev = None | |
while text: | |
if re.match(_MARKS, text): | |
res.append(text) | |
text = text[1:] | |
continue | |
if text.startswith("ใผ"): | |
if prev: | |
res.append(prev[-1]) | |
text = text[1:] | |
continue | |
res += hiragana2p(jaconv.kata2hira(text)).split(" ") | |
break | |
# res = _COLON_RX.sub(":", res) | |
return res | |
_SYMBOL_TOKENS = set(list("ใปใใ๏ผ๏ผ")) | |
_NO_YOMI_TOKENS = set(list("ใใใใโ๏ผ๏ผ๏ผป๏ผฝ[]")) | |
_MARKS = re.compile( | |
r"[^A-Za-z\d\u3005\u3040-\u30ff\u4e00-\u9fff\uff11-\uff19\uff21-\uff3a\uff41-\uff5a\uff66-\uff9d]" | |
) | |
def text2sep_kata(text: str): | |
parsed = pyopenjtalk.run_frontend(text) | |
res = [] | |
sep = [] | |
for parts in parsed: | |
word, yomi = replace_punctuation(parts["string"]), parts["pron"].replace( | |
"โ", "" | |
) | |
if yomi: | |
if re.match(_MARKS, yomi): | |
if len(word) > 1: | |
word = [replace_punctuation(i) for i in list(word)] | |
yomi = word | |
res += yomi | |
sep += word | |
continue | |
elif word not in rep_map.keys() and word not in rep_map.values(): | |
word = "," | |
yomi = word | |
res.append(yomi) | |
else: | |
if word in _SYMBOL_TOKENS: | |
res.append(word) | |
elif word in ("ใฃ", "ใ"): | |
res.append("ใ") | |
elif word in _NO_YOMI_TOKENS: | |
pass | |
else: | |
res.append(word) | |
sep.append(word) | |
return sep, res, get_accent(parsed) | |
def get_accent(parsed): | |
labels = pyopenjtalk.make_label(parsed) | |
phonemes = [] | |
accents = [] | |
for n, label in enumerate(labels): | |
phoneme = re.search(r"\-([^\+]*)\+", label).group(1) | |
if phoneme not in ["sil", "pau"]: | |
phonemes.append(phoneme.replace("cl", "q").lower()) | |
else: | |
continue | |
a1 = int(re.search(r"/A:(\-?[0-9]+)\+", label).group(1)) | |
a2 = int(re.search(r"\+(\d+)\+", label).group(1)) | |
if re.search(r"\-([^\+]*)\+", labels[n + 1]).group(1) in ["sil", "pau"]: | |
a2_next = -1 | |
else: | |
a2_next = int(re.search(r"\+(\d+)\+", labels[n + 1]).group(1)) | |
# Falling | |
if a1 == 0 and a2_next == a2 + 1: | |
accents.append(-1) | |
# Rising | |
elif a2 == 1 and a2_next == 2: | |
accents.append(1) | |
else: | |
accents.append(0) | |
return list(zip(phonemes, accents)) | |
_ALPHASYMBOL_YOMI = { | |
"#": "ใทใฃใผใ", | |
"%": "ใใผใปใณใ", | |
"&": "ใขใณใ", | |
"+": "ใใฉใน", | |
"-": "ใใคใใน", | |
":": "ใณใญใณ", | |
";": "ใปใใณใญใณ", | |
"<": "ๅฐใชใ", | |
"=": "ใคใณใผใซ", | |
">": "ๅคงใชใ", | |
"@": "ใขใใ", | |
"a": "ใจใผ", | |
"b": "ใใผ", | |
"c": "ใทใผ", | |
"d": "ใใฃใผ", | |
"e": "ใคใผ", | |
"f": "ใจใ", | |
"g": "ใธใผ", | |
"h": "ใจใคใ", | |
"i": "ใขใค", | |
"j": "ใธใงใผ", | |
"k": "ใฑใผ", | |
"l": "ใจใซ", | |
"m": "ใจใ ", | |
"n": "ใจใ", | |
"o": "ใชใผ", | |
"p": "ใใผ", | |
"q": "ใญใฅใผ", | |
"r": "ใขใผใซ", | |
"s": "ใจใน", | |
"t": "ใใฃใผ", | |
"u": "ใฆใผ", | |
"v": "ใใค", | |
"w": "ใใใชใฅใผ", | |
"x": "ใจใใฏใน", | |
"y": "ใฏใค", | |
"z": "ใผใใ", | |
"ฮฑ": "ใขใซใใก", | |
"ฮฒ": "ใใผใฟ", | |
"ฮณ": "ใฌใณใ", | |
"ฮด": "ใใซใฟ", | |
"ฮต": "ใคใใทใญใณ", | |
"ฮถ": "ใผใผใฟ", | |
"ฮท": "ใคใผใฟ", | |
"ฮธ": "ใทใผใฟ", | |
"ฮน": "ใคใชใฟ", | |
"ฮบ": "ใซใใ", | |
"ฮป": "ใฉใ ใ", | |
"ฮผ": "ใใฅใผ", | |
"ฮฝ": "ใใฅใผ", | |
"ฮพ": "ใฏใตใค", | |
"ฮฟ": "ใชใใฏใญใณ", | |
"ฯ": "ใใค", | |
"ฯ": "ใญใผ", | |
"ฯ": "ใทใฐใ", | |
"ฯ": "ใฟใฆ", | |
"ฯ ": "ใฆใใทใญใณ", | |
"ฯ": "ใใกใค", | |
"ฯ": "ใซใค", | |
"ฯ": "ใใตใค", | |
"ฯ": "ใชใกใฌ", | |
} | |
_NUMBER_WITH_SEPARATOR_RX = re.compile("[0-9]{1,3}(,[0-9]{3})+") | |
_CURRENCY_MAP = {"$": "ใใซ", "ยฅ": "ๅ", "ยฃ": "ใใณใ", "โฌ": "ใฆใผใญ"} | |
_CURRENCY_RX = re.compile(r"([$ยฅยฃโฌ])([0-9.]*[0-9])") | |
_NUMBER_RX = re.compile(r"[0-9]+(\.[0-9]+)?") | |
def japanese_convert_numbers_to_words(text: str) -> str: | |
res = _NUMBER_WITH_SEPARATOR_RX.sub(lambda m: m[0].replace(",", ""), text) | |
res = _CURRENCY_RX.sub(lambda m: m[2] + _CURRENCY_MAP.get(m[1], m[1]), res) | |
res = _NUMBER_RX.sub(lambda m: num2words(m[0], lang="ja"), res) | |
return res | |
def japanese_convert_alpha_symbols_to_words(text: str) -> str: | |
return "".join([_ALPHASYMBOL_YOMI.get(ch, ch) for ch in text.lower()]) | |
def is_japanese_character(char): | |
# ๅฎไนๆฅ่ฏญๆๅญ็ณป็ป็ Unicode ่ๅด | |
japanese_ranges = [ | |
(0x3040, 0x309F), # ๅนณๅๅ | |
(0x30A0, 0x30FF), # ็ๅๅ | |
(0x4E00, 0x9FFF), # ๆฑๅญ (CJK Unified Ideographs) | |
(0x3400, 0x4DBF), # ๆฑๅญๆฉๅฑ A | |
(0x20000, 0x2A6DF), # ๆฑๅญๆฉๅฑ B | |
# ๅฏไปฅๆ นๆฎ้่ฆๆทปๅ ๅ ถไปๆฑๅญๆฉๅฑ่ๅด | |
] | |
# ๅฐๅญ็ฌฆ็ Unicode ็ผ็ ่ฝฌๆขไธบๆดๆฐ | |
char_code = ord(char) | |
# ๆฃๆฅๅญ็ฌฆๆฏๅฆๅจไปปไฝไธไธชๆฅ่ฏญ่ๅดๅ | |
for start, end in japanese_ranges: | |
if start <= char_code <= end: | |
return True | |
return False | |
rep_map = { | |
"๏ผ": ",", | |
"๏ผ": ",", | |
"๏ผ": ",", | |
"ใ": ".", | |
"๏ผ": "!", | |
"๏ผ": "?", | |
"\n": ".", | |
"๏ผ": ".", | |
"โฆ": "...", | |
"ยทยทยท": "...", | |
"ใปใปใป": "...", | |
"ยท": ",", | |
"ใป": ",", | |
"ใ": ",", | |
"$": ".", | |
"โ": "'", | |
"โ": "'", | |
'"': "'", | |
"โ": "'", | |
"โ": "'", | |
"๏ผ": "'", | |
"๏ผ": "'", | |
"(": "'", | |
")": "'", | |
"ใ": "'", | |
"ใ": "'", | |
"ใ": "'", | |
"ใ": "'", | |
"[": "'", | |
"]": "'", | |
"โ": "-", | |
"โ": "-", | |
"๏ฝ": "-", | |
"~": "-", | |
"ใ": "'", | |
"ใ": "'", | |
} | |
def replace_punctuation(text): | |
pattern = re.compile("|".join(re.escape(p) for p in rep_map.keys())) | |
replaced_text = pattern.sub(lambda x: rep_map[x.group()], text) | |
replaced_text = re.sub( | |
r"[^\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\u3400-\u4DBF\u3005" | |
+ "".join(punctuation) | |
+ r"]+", | |
"", | |
replaced_text, | |
) | |
return replaced_text | |
def text_normalize(text): | |
res = unicodedata.normalize("NFKC", text) | |
res = japanese_convert_numbers_to_words(res) | |
# res = "".join([i for i in res if is_japanese_character(i)]) | |
res = replace_punctuation(res) | |
res = res.replace("ใ", "") | |
return res | |
def distribute_phone(n_phone, n_word): | |
phones_per_word = [0] * n_word | |
for task in range(n_phone): | |
min_tasks = min(phones_per_word) | |
min_index = phones_per_word.index(min_tasks) | |
phones_per_word[min_index] += 1 | |
return phones_per_word | |
def handle_long(sep_phonemes): | |
for i in range(len(sep_phonemes)): | |
if sep_phonemes[i][0] == "ใผ": | |
sep_phonemes[i][0] = sep_phonemes[i - 1][-1] | |
if "ใผ" in sep_phonemes[i]: | |
for j in range(len(sep_phonemes[i])): | |
if sep_phonemes[i][j] == "ใผ": | |
sep_phonemes[i][j] = sep_phonemes[i][j - 1][-1] | |
return sep_phonemes | |
tokenizer = AutoTokenizer.from_pretrained("./bert/deberta-v2-large-japanese-char-wwm") | |
def align_tones(phones, tones): | |
res = [] | |
for pho in phones: | |
temp = [0] * len(pho) | |
for idx, p in enumerate(pho): | |
if len(tones) == 0: | |
break | |
if p == tones[0][0]: | |
temp[idx] = tones[0][1] | |
if idx > 0: | |
temp[idx] += temp[idx - 1] | |
tones.pop(0) | |
temp = [0] + temp | |
temp = temp[:-1] | |
if -1 in temp: | |
temp = [i + 1 for i in temp] | |
res.append(temp) | |
res = [i for j in res for i in j] | |
assert not any([i < 0 for i in res]) and not any([i > 1 for i in res]) | |
return res | |
def rearrange_tones(tones, phones): | |
res = [0] * len(tones) | |
for i in range(len(tones)): | |
if i == 0: | |
if tones[i] not in punctuation: | |
res[i] = 1 | |
elif tones[i] == prev: | |
if phones[i] in punctuation: | |
res[i] = 0 | |
else: | |
res[i] = 1 | |
elif tones[i] > prev: | |
res[i] = 2 | |
elif tones[i] < prev: | |
res[i - 1] = 3 | |
res[i] = 1 | |
prev = tones[i] | |
return res | |
def g2p(norm_text): | |
sep_text, sep_kata, acc = text2sep_kata(norm_text) | |
sep_tokenized = [] | |
for i in sep_text: | |
if i not in punctuation: | |
sep_tokenized.append(tokenizer.tokenize(i)) | |
else: | |
sep_tokenized.append([i]) | |
sep_phonemes = handle_long([kata2phoneme(i) for i in sep_kata]) | |
# ๅผๅธธๅค็๏ผMeCabไธ่ฎค่ฏ็่ฏ็่ฏไผไธ่ทฏไผ ๅฐ่ฟ้ๆฅ๏ผ็ถๅ็ธๆใ็ฎๅๆฅ็ๅชๆ้ฃไบ่ถ ็บง็จๆ็็ๅป่ฏไผๅบ็ฐ่ฟ็งๆ ๅต | |
for i in sep_phonemes: | |
for j in i: | |
assert j in symbols, (sep_text, sep_kata, sep_phonemes) | |
tones = align_tones(sep_phonemes, acc) | |
word2ph = [] | |
for token, phoneme in zip(sep_tokenized, sep_phonemes): | |
phone_len = len(phoneme) | |
word_len = len(token) | |
aaa = distribute_phone(phone_len, word_len) | |
word2ph += aaa | |
phones = ["_"] + [j for i in sep_phonemes for j in i] + ["_"] | |
# tones = [0] + rearrange_tones(tones, phones[1:-1]) + [0] | |
tones = [0] + tones + [0] | |
word2ph = [1] + word2ph + [1] | |
assert len(phones) == len(tones) | |
return phones, tones, word2ph | |
if __name__ == "__main__": | |
tokenizer = AutoTokenizer.from_pretrained("./bert/deberta-v2-large-japanese") | |
text = "hello,ใใใซใกใฏใไธ็ใผ๏ผโฆโฆ" | |
from text.japanese_bert import get_bert_feature | |
text = text_normalize(text) | |
print(text) | |
phones, tones, word2ph = g2p(text) | |
bert = get_bert_feature(text, word2ph) | |
print(phones, tones, word2ph, bert.shape) | |