首先导入拼音转汉字的包pinyin_2_hanzi,测试程序如下:
def pinyin_2_hanzi(pinyinList):
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag
dagParams = DefaultDagParams()
result = dag(dagParams, pinyinList, path_num=10, log=True)#10代表侯选值个数
for item in result:
socre = item.score
res = item.path # 转换结果
print(socre, res)
if __name__ == '__main__':
lists1 = ['jing', 'chang']
pinyin_2_hanzi(lists1)
结果-1.3640561107470588 [‘经常’]
-1.6054959887282985 [‘警长’]
-4.247565153431724 [‘经’, ‘场’]
-4.296189563320052 [‘经’, ‘常’]
-4.321781623770168 [‘经’, ‘长’]
-4.358765159550064 [‘经’, ‘厂’]
-4.369788903224423 [‘经’, ‘唱’]
-4.378400459797502 [‘经’, ‘偿’]
-4.37959239397302 [‘经’, ‘尝’]
-4.379881686157487 [‘经’, ‘畅’]
下载地址:
拼音转汉字https://pypi.python.org/pypi/Pinyin2Hanzi/0.1.1