汉语拼音转换工具包pypinyin

 1 #pip install pypinyin汉字转换汉语拼音
 2 from pypinyin import lazy_pinyin,TONE,TONE2,TONE3
 3 str="你知道我是谁吗?"
 4 res=lazy_pinyin(str)
 5 res1=lazy_pinyin(str,style=TONE)
 6 res2=lazy_pinyin(str,style=TONE2)
 7 res3=lazy_pinyin(str,style=TONE3)
 8 print(res)
 9 print(res1)
10 print(res2)
11 print(res3)
12 
13 # ['ni', 'zhi', 'dao', 'wo', 'shi', 'shei', 'ma', '?']
14 # ['nǐ', 'zhī', 'dào', 'wǒ', 'shì', 'shéi', 'ma', '?']
15 # ['ni3', 'zhi1', 'da4o', 'wo3', 'shi4', 'she2i', 'ma', '?']
16 # ['ni3', 'zhi1', 'dao4', 'wo3', 'shi4', 'shei2', 'ma', '?']

可应用在同音汉字模糊匹配,比如在ai领域的语音识别方向

原文地址:https://www.cnblogs.com/open-yang/p/11253025.html