Pinyin2Hanzi拼音转汉字失败,结果为空的解决办法

Pinyin2Hanzi实现拼音转汉子的功能,但是,拼音必须是规范的。否则会出现如下情形:

# 拼音转汉字
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag

def pinyin_2_hanzi(pinyin_list):
    dagParams = DefaultDagParams()
    result = dag(dagParams, pinyin_list, path_num=3, log = True) # path_num代表候选集个数
    for item in result:
        score = item.score
        res = item.path # 转换结果
        print("score = {}, ressult = {}".format(score, res))

if __name__ == '__main__':
    lst = ['xue', 'xiao']
    # lst = ['xve', 'xiao']
    pinyin_2_hanzi(lst)

返回结果为空。

解决办法

因此,我们需要对拼音进行修正。Pinyin2Hanzi模块提供了修正的算法模块:simplify_pinyin

# 拼音转汉字
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag

def pinyin_2_hanzi(pinyin_list):
    dagParams = DefaultDagParams()
    result = dag(dagParams, pinyin_list, path_num=3, log = True) # path_num代表候选集个数
    for item in result:
        score = item.score
        res = item.path # 转换结果
        print("score = {}, ressult = {}".format(score, res))

if __name__ == '__main__':
    lst = ['xue', 'xiao']

    # 拼音转规范
    from Pinyin2Hanzi import simplify_pinyin
    lst_simplified = []
    for item in lst:
        lst_simplified.append(simplify_pinyin(item))
    print("修正后的拼音为:", lst_simplified)

    pinyin_2_hanzi(lst_simplified)

此时,返回结果为:

修正后的拼音为:['xve', 'xiao']
score = -1.0809033989236891, ressult = ['学校']
score = -4.1501850243170555, ressult = ['学', '小']
score = -4.280325124338203, ressult = ['学', '效']

你可能感兴趣的:(Pinyin2Hanzi拼音转汉字失败,结果为空的解决办法)