"""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, , 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)