Python 汉字转拼音
本文参考:
Python中文转拼音代码(支持全拼和首字母缩写)
中文中不可以有“()”
# -*- coding: utf-8 -*-
__version__ = '0.9'
__all__ = ["PinYin"]
import os.path class PinYin(object):
def __init__(self):
self.word_dict = {} def load_word(self, dict_file):
self.dict_file = dict_file
if not os.path.exists(self.dict_file):
raise IOError("NotFoundFile") with file(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="", firstcode=False):
result = []
if not isinstance(string, unicode):
string = string.decode("utf-8") for char in string:
key = '%X' % ord(char)
value = self.word_dict.get(key, char)
# print("===================")+value
# print ("str(value).split()===")
# for i in str(value).split():
# print i
if value is not None and len(value)>0: s1=str(value).split()
# print(type(s1))
# print(str(len(s1)))
if s1 is not None and len(s1)>0:
outpinyin = str(value).split()[0][:-1].lower() if not outpinyin:
outpinyin = char
if firstcode:
result.append(outpinyin[0])
else:
result.append(outpinyin)
return result def hanzi2pinyin_split(self, string="", split="", firstcode=False):
"""提取中文的拼音
@param string:要提取的中文
@param split:分隔符
@param firstcode: 提取的是全拼还是首字母?如果为true表示提取首字母,默认为False提取全拼
"""
result = self.hanzi2pinyin(string=string, firstcode=firstcode)
return split.join(result) if __name__ == "__main__":
test = PinYin()
test.load_word('word.data')
string = "Java程序性能优化-让你的Java程序更快更稳定"
print "in: %s" % string
print "out: %s" % str(test.hanzi2pinyin(string=string))
print "out: %s" % test.hanzi2pinyin_split(string=string, split="", firstcode=True)
print "out: %s" % test.hanzi2pinyin_split(string=string, split="", firstcode=False)
使用:
import app.model.explore.util.pinyin as pinyin
pyCvtor = pinyin.PinYin()
path=os.path.dirname(os.getcwd())+'\\util\\word.data'
print(path)
pyCvtor.load_word(path)
目录结构

具体使用:

转换效果:
肉眼及镜下血尿==========ryjjxxn
尿毒症
==========ndz
智力发育迟缓==========zlfych
氮质血症
==========dzxz
空腹血糖及糖耐量试验均正常==========kfxtjtnlsyjzc
血尿==========xn
大量蛋白尿==========dldbn
多尿==========dn
少尿==========sn
贫血氮质血症==========pxdzxz
Python 汉字转拼音的更多相关文章
- Python 汉字转拼音库 pypinyin
一.初衷: 一些开源软件的配置文件中识别区分的部分用英文,那么我们在批量生成配置文件的时候,可以从CMDB导入汉字(idc_name), 然后将它转换成拼音,再或者拼接上IP地址,以便更准确的识别.例 ...
- python汉字转拼音
上代码: #!/usr/bin/env python # -*- coding:utf-8 -*- """ Author:cleverdeng E-mail:clverd ...
- Hanlp汉字转拼音使用python调用详解
1.hanlp简介 HanLP是一系列模型与算法组成的NLP工具包,由大快搜索主导并完全开源,目标是普及自然语言处理在生产环境中的应用.HanLP具备功能完善.性能高效.架构清晰.语料时新.可自定义的 ...
- 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...
- JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
原文:http://www.cnblogs.com/liuxianan/p/pinyinjs.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多 ...
- 隐马尔科夫模型python实现简单拼音输入法
在网上看到一篇关于隐马尔科夫模型的介绍,觉得简直不能再神奇,又在网上找到大神的一篇关于如何用隐马尔可夫模型实现中文拼音输入的博客,无奈大神没给可以运行的代码,只能纯手动网上找到了结巴分词的词库,根据此 ...
- iOS 汉字的拼音
获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** * 汉字的拼音 * * @return ...
- JavaScript 汉字与拼音互转终极方案 附JS拼音输入法
转:http://www.codeceo.com/article/javascript-pinyin.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的 ...
- SQL汉字转拼音函数-支持首字母、全拼
SQL汉字转拼音函数-支持首字母.全拼 FROM :http://my.oschina.net/ind/blog/191659 作者不详 --方法一sqlserver汉字转拼音首字母 --调用方法 s ...
随机推荐
- oracle中计算百分比,并同时解决小数点前0不显示的问题
select a.catalog_name,decode(substr(trunc((a.s/b.count2),4)*100||'%',0,1),'.',replace(trunc((a.s/b.c ...
- Java实现检验一串数字的出栈合法性
题目描述: 解题思路: 判断出栈合法性的关键在于,对于每一个数,在它后面出栈且比它小的数,必是以降序排列的. 比如说3 4 2 1 5这一组数,对于第一个数 3 来说,后面比它小的数有 1.2,而在4 ...
- django xss过滤
django对于xss的过滤有其本身自带的safe等 但是如果通过jsonResponse返回再在前端加载,无法对XSS进行有效的过滤. 因此需自己写一个XSS过滤器,作为装饰器对request的GE ...
- ES6+javaScript原型
Symbol 属性私有化,数据保护 let _gender = Symbol('gender') function Person(name, gender) { this.name = name; t ...
- Alpha(5/10)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- ENQUIRE the predecessor to the World Wide Web.
看Building Responsive Data Visualization for the Web时介绍到了Enquire,表示wiki类系统实现了它的核心思想. 有点好奇是如何实现的,所以大概看 ...
- java获取一年的周数和间隔天数
java获取一年的周数和间隔天数 import java.text.ParseException; import java.text.SimpleDateFormat; import java.uti ...
- LeetCode(283. 移动零)
问题描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...
- js数组根据指定字段(true or false)排序
const obj = [ {name:'1',bind:true}, {name:'2',bind:false}, {name:'3',bind:true}, {name:'4',bind:fals ...
- IDEA+Maven:cannot download sources
把IDEA的maven换成maven2