Pinyin2Hanzi
拼音转汉字, 拼音输入法引擎, pin yin -> 拼音
Install / Use
/learn @letiantian/Pinyin2HanziREADME
Pinyin2Hanzi
拼音转汉字,可以作为拼音输入法的转换引擎,兼容Python 2、Python 3。
安装
Python 2:
$ python setup.py install --user
Python 3:
$ python3 setup.py install --user
使用
下面的示例在Python 3中运行。
基于HMM的转换
原理是viterbi算法。
from Pinyin2Hanzi import DefaultHmmParams
from Pinyin2Hanzi import viterbi
hmmparams = DefaultHmmParams()
## 2个候选
result = viterbi(hmm_params=hmmparams, observations=('ni', 'zhi', 'bu', 'zhi', 'dao'), path_num = 2)
for item in result:
print(item.score, item.path)
'''输出
1.3155294593897203e-08 ['你', '知', '不', '知', '道']
3.6677865125992192e-09 ['你', '只', '不', '知', '道']
'''
## 2个候选,使用对数打分
result = viterbi(hmm_params=hmmparams, observations=('ni', 'zhi', 'bu', 'zhi', 'dao'), path_num = 2, log = True)
for item in result:
print(item.score, item.path)
'''输出
-18.14644152864202 ['你', '知', '不', '知', '道']
-19.423677486918002 ['你', '只', '不', '知', '道']
'''
## 2个候选,使用对数打分
result = viterbi(hmm_params=hmmparams, observations=('ni', 'zhii', 'bu', 'zhi', 'dao'), path_num = 2, log = True)
for item in result:
print(item.score, item.path)
# 发生KeyError,`zhii`不规范
基于DAG的转换
原理是词库+动态规划。
from Pinyin2Hanzi import DefaultDagParams
from Pinyin2Hanzi import dag
dagparams = DefaultDagParams()
## 2个候选
result = dag(dagparams, ('ni', 'bu', 'zhi', 'dao', 'de', 'shi'), path_num=2)
for item in result:
print(item.score, item.path)
''' 输出
0.08117536840088911 ['你不知道', '的是']
0.04149191639287887 ['你不知道', '的诗']
'''
## 2个候选,使用对数打分
result = dag(dagparams, ('ni', 'bu', 'zhi', 'dao', 'de', 'shi'), path_num=2, log=True)
for item in result:
print(item.score, item.path)
''' 输出
-2.5111434226494866 ['你不知道', '的是']
-3.1822566564324477 ['你不知道', '的诗']
'''
## 1个候选
print( dag(dagparams, ['ti', 'chu', 'le', 'bu', 'cuo', 'de', 'jie', 'jve', 'fang', 'an'], path_num=1) )
'''输出
[< score=0.0017174549839096384, path=['提出了', '不错', '的', '解决方案'] >]
'''
## 2个候选,使用对数打分
result = dag(dagparams, ('ni', 'bu', 'zhi', 'dao', 'de', 'shii'), path_num=2, log=True)
print(result)
# 输出空列表,因为`shii`不存在
自定义params
实现AbstractHmmParams, AbstractDagParams这两个接口即可。具体可以参考源码。
关于拼音
给出的拼音必须是“规范”的。例如
- 略 -> lve
- 据 -> ju
列举所有“规范”的拼音:
from Pinyin2Hanzi import all_pinyin
for py in all_pinyin():
print(py)
将拼音转换为“规范”的拼音:
from Pinyin2Hanzi import simplify_pinyin
print(simplify_pinyin('lue'))
# 输出:'lve'
print(simplify_pinyin('lüè'))
# 输出:'lve'
判断是否是“规范”的拼音:
from Pinyin2Hanzi import is_pinyin
print(is_pinyin('lue'))
# 输出:False
print(is_pinyin('lüè'))
# 输出:False
print(is_pinyin('lvee'))
# 输出:False
print(is_pinyin('lve'))
# 输出:True
训练
原始数据和训练代码在train目录下。数据来自jpinyin、pinyin、搜狗语料库-互联网词库等。处理数据时用到了汉字转拼音
工具ChineseTone。
原理
License
MIT
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
