Pinyin2Hanzi實(shí)現(xiàn)拼音轉(zhuǎn)漢子的功能,但是胸蛛,拼音必須是規(guī)范的污茵。否則會(huì)出現(xiàn)如下情形:
# 拼音轉(zhuǎn)漢字
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代表候選集個(gè)數(shù)
for item in result:
score = item.score
res = item.path # 轉(zhuǎn)換結(jié)果
print("score = {}, ressult = {}".format(score, res))
if __name__ == '__main__':
lst = ['xue', 'xiao']
# lst = ['xve', 'xiao']
pinyin_2_hanzi(lst)
返回結(jié)果為空。
解決辦法
因此葬项,我們需要對(duì)拼音進(jìn)行修正泞当。Pinyin2Hanzi模塊提供了修正的算法模塊:simplify_pinyin
# 拼音轉(zhuǎn)漢字
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代表候選集個(gè)數(shù)
for item in result:
score = item.score
res = item.path # 轉(zhuǎn)換結(jié)果
print("score = {}, ressult = {}".format(score, res))
if __name__ == '__main__':
lst = ['xue', 'xiao']
# 拼音轉(zhuǎn)規(guī)范
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)
此時(shí),返回結(jié)果為:
修正后的拼音為:['xve', 'xiao']
score = -1.0809033989236891, ressult = ['學(xué)校']
score = -4.1501850243170555, ressult = ['學(xué)', '小']
score = -4.280325124338203, ressult = ['學(xué)', '效']