File size: 723 Bytes
0c723b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""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)