当前位置: 首页 > 工具软件 > Pinyin2Hanzi > 使用案例 >

Python 将拼音转换成汉字

翟志新
2023-12-01

利用Pinyin2Hanzi 可以将拼音转换为汉字,可以作为拼音输入法的转换引擎:

示例:

def pinyin_2_hanzi(pinyinList):
    from Pinyin2Hanzi import DefaultDagParams
    from Pinyin2Hanzi import dag

    dagParams = DefaultDagParams()
    # 10个候选值
    result = dag(dagParams, pinyinList, path_num=10, log=True)
    for item in result:
        socre = item.score # 得分
        res = item.path # 转换结果
        print(socre, res)

if __name__ == '__main__':
    lists = ['wo', 'you', 'yi', 'zhi', 'xiao', 'mao', 'lv']
    pinyin_2_hanzi(lists)

输出结果:

-4.2052842930967245 [‘我有’, ‘一直’, ‘小毛驴’]
-4.251573260413308 [‘我又’, ‘一直’, ‘小毛驴’]
-4.336179175173976 [‘握有’, ‘一直’, ‘小毛驴’]
-4.507350673777502 [‘我有’, ‘一只’, ‘小毛驴’]
-4.553639641094085 [‘我又’, ‘一只’, ‘小毛驴’]
-4.587233017834571 [‘我有’, ‘一致’, ‘小毛驴’]
-4.60454386418968 [‘我有’, ‘一支’, ‘小毛驴’]
-4.633521985151154 [‘我又’, ‘一致’, ‘小毛驴’]
-4.638245555854753 [‘握有’, ‘一只’, ‘小毛驴’]
-4.6420019261545225 [‘我有’, ‘抑制’, ‘小毛驴’]

 类似资料: