Download python/cn_frontend/pinyin_to_sequence.py from AXERA-TECH/Inflect-Micro-v2-zh: direct link, hf CLI and curl.
- Browser
- Download file 723 Bytes
-
https://huggingface.co/AXERA-TECH/Inflect-Micro-v2-zh/resolve/main/python/cn_frontend/pinyin_to_sequence.py
- Command line
-
hf download hf://AXERA-TECH/Inflect-Micro-v2-zh/python/cn_frontend/pinyin_to_sequence.py
-
curl -L -o pinyin_to_sequence.py https://huggingface.co/AXERA-TECH/Inflect-Micro-v2-zh/resolve/main/python/cn_frontend/pinyin_to_sequence.py
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) | |