When we are converting chinese string to speech, we may have to convert them to pinyin. In this tutorial, we will introduce you how to do in python.
Preliminary
We will use python pypinyin package to implement.
First, we should install it.
pip install pypinyin
Then we can start to convert.
Convert chinese string to pinyin in python
For example:
from pypinyin import pinyin, lazy_pinyin, Style tx = pinyin('同屏互动双向同步需求开发,下周继续开发', style=Style.TONE3, neutral_tone_with_five=True) print(tx) tx = lazy_pinyin('同屏互动双向同步需求开发,下周继续开发', style=Style.TONE3, neutral_tone_with_five=True) print(tx)
In this code, we use pinyin() or lazy_pinyin() to convert a chinese string to pinyin.
We will get:
[['tong2'], ['ping2'], ['hu4'], ['dong4'], ['shuang1'], ['xiang4'], ['tong2'], ['bu4'], ['xu1'], ['qiu2'], ['kai1'], ['fa1'], [','], ['xia4'], ['zhou1'], ['ji4'], ['xu4'], ['kai1'], ['fa1']] ['tong2', 'ping2', 'hu4', 'dong4', 'shuang1', 'xiang4', 'tong2', 'bu4', 'xu1', 'qiu2', 'kai1', 'fa1', ',', 'xia4', 'zhou1', 'ji4', 'xu4', 'kai1', 'fa1']
pinyin() Vs lazy_pinyin()
From above, we can find:
pinyin() will return a python list, its items are also single list.
lazy_pinyin() will return a python list, its items are string.