python3环境下汉字转拼音
来源:互联网 发布:各种旋转矩阵公式 编辑:程序博客网 时间:2024/05/16 12:58
参考:https://github.com/cleverdeng/pinyin.py
上述代码适合python2.7的代码,但是对于python3.5的环境却不是很友好,所以将代码改成python3.5环境下可以运行的代码,代码如下:
# -*- coding:utf-8 -*-""" Author:cleverdeng E-mail:clverdeng@gmail.com"""__version__ = '0.9'__all__ = ["PinYin"]import os.pathclass PinYin(object): def __init__(self, dict_file='word.data'): self.word_dict = {} self.dict_file = dict_file def load_word(self): if not os.path.exists(self.dict_file): raise IOError("NotFoundFile") with open(self.dict_file) as f_obj: for f_line in f_obj.readlines(): try: line = f_line.split(' ') self.word_dict[line[0]] = line[1] except: line = f_line.split(' ') self.word_dict[line[0]] = line[1] def hanzi2pinyin(self, string=""): result = [] for char in string: key = '%X' % ord(char) result.append(self.word_dict.get(key, char).split()[0][:-1].lower()) return result def hanzi2pinyin_split(self, string="", split=""): result = self.hanzi2pinyin(string=string) if split == "": return result else: return split.join(result)if __name__ == "__main__": test = PinYin() test.load_word() string = "钓鱼岛是中国的" print( "in: %s" % string) print( "out: %s" % str(test.hanzi2pinyin(string=string))) print( "out: %s" % test.hanzi2pinyin_split(string=string, split="-"))
阅读全文
0 0
- python3环境下汉字转拼音
- powershell 下汉字转拼音
- Android下汉字转拼音的方法
- Qt下汉字转拼音,包含二级汉字
- Qt下 汉字转换拼音
- QT下获取汉字拼音
- 汉字转拼音
- 汉字转拼音缩写
- 汉字转拼音缩写
- 实用汉字转拼音
- 汉字转拼音
- 汉字转拼音 asp
- 汉字转拼音缩写
- 汉字转拼音
- 汉字转拼音缩写
- 汉字转拼音
- PHP汉字转拼音
- PHP汉字转拼音
- ofo终于推出智能锁 由超大型国有企业提供技术支持
- 阿里开始用微软Azure网络交换机软件SONiC
- 狼人杀要做社交,绕不开音视频连麦 | 深度
- 爆料 | 龙芯拟发布多款芯片,国产唯一支持多路互联的“中国芯”诞生
- jsp自定义标签的HelloWorld
- python3环境下汉字转拼音
- 渲染管线理解1
- keras在win7上的配置。
- IDEA 在mac环境下设置智能提示不区分大小写
- 华为在国内赢三星获赔8000万,在英输官司面临禁售
- 最“库”手机上线?Xplay6库里定制版就要来了?!
- 人生的4个维度与价值观
- Facebook推出图像对比软件,限制“色情报复”内容传播
- 打响印度全民AI第一枪,英特尔为何启动“AI开发者教育计划”