Inflect-Micro-v2-zh / python /cn_frontend /pinyin_to_sequence.py
inoryQwQ's picture
Release 1.0.0: Chinese TTS (Inflect-Micro-v2 arch + BigVGAN), ONNX + AX650 axmodel + checkpoints
0c723b3 verified
Raw
History Blame Contribute Delete
723 Bytes
"""Convert Chinese text to pinyin character sequence."""
import re
from pypinyin import lazy_pinyin, Style
from .symbols import get_phoneme_ids
_whitespace_re = re.compile(r"\s+")
def text_to_sequence(text: str) -> list[int]:
"""Chinese text -> pinyin character IDs.
Example: "你好" -> "ni3 hao3" -> [n, i, 3, <space>, h, a, o, 3]
"""
text = re.sub(_whitespace_re, "", text)
# Convert to pinyin with tone digits
py_list = lazy_pinyin(text, style=Style.TONE3, neutral_tone_with_five=True)
# Join with spaces between words
pinyin_str = " ".join(py_list)
return get_phoneme_ids(pinyin_str)
def pinyin_to_ids(pinyin_str: str) -> list[int]:
return get_phoneme_ids(pinyin_str)