Python 将中文转拼音
文字转拼音
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("No Found Dict File.") 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 word2pinyin(self, string="", firstcode=False):
result = [] for char in string:
key = '%X' % ord(char)
value = self.word_dict.get(key, char)
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 word2pinyin_split(self, string="", split="", firstcode=False):
result = self.word2pinyin(string=string, firstcode=firstcode)
return split.join(result) def main():
pinyin = PinYin()
pinyin.load_word('G:\\Py_Workspace\\Py_config\\word.data')
string = "Java程序性能优化-让你的Java程序更快更稳定"
print("in: %s" % string)
#print("out: %s" % str(pinyin.word2pinyin(string=string)))
#print("out: %s" % pinyin.word2pinyin_split(string=string, split="", firstcode=True))
print("out: %s" % pinyin.word2pinyin_split(string=string, split="", firstcode=False)) if __name__ == "__main__":
main()
Python 将中文转拼音的更多相关文章
- python 将中文转拼音后填充到url做参数并写入excel
闲着没事写了个小工具,将中文转拼音后填充到url做参数并写如excel 一.先看下演示,是个什么东西 二.代码 代码用到一个中文转拼音的库,库是网上下的,稍微做了下修改,已经找不原来下载的地址了,然后 ...
- Python中文转拼音代码(支持全拼和首字母缩写)
本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级: 1 2 3 4 1.可以传入参数firstcode:如果为 ...
- [Python] Python 获取中文的首字母 和 全部拼音首字母
Python 获取中文的首字母 和 全部拼音首字母 代码如下: import pinyin def getStrAllAplha(str): return pinyin.get_initial(str ...
- Mono 3.2 测试NPinyin 中文转换拼音代码
C#中文转换为拼音NPinyin代码 在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...
- PHP如何将中文转换为拼音
用来得到中文的首字母: 这个是将中文转换为拼音的类:charset <?php/*** 汉字转化为拼音,拼音转化为汉字**/ class charset{private $_code=array ...
- python matplotlib 中文显示参数设置
python matplotlib 中文显示参数设置 方法一:每次编写代码时进行参数设置 #coding:utf-8import matplotlib.pyplot as pltplt.rcParam ...
- php 获取中文字符拼音首字母
//php获取中文字符拼音首字母 function getFirstCharter($str){ if(empty($str)){return '';} $fchar=ord($str{}); }); ...
- python截取中文字符串
python的中文处理还是比较麻烦的,utf-8的字符串的长度是1-6个字符,一不小心就会从中截断,出现所谓的乱码.下面这个函数提供了,从一段utf-8编码的字符串中,截取固定长度的字串.ord(ch ...
- C# 中文转拼音类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SU { ...
随机推荐
- java编程思想-多态
java中除了static方法和final方法(private方法属于final方法)之外,其他所有的方法都是动态绑定即运行时绑定. public class test { private void ...
- PowerDesinger15设置字体大小
使用PowerDesigner时,它默认table的字体大小颜色等很难看: 假设通过 Symbol ---> Format进行设置.仅仅能对选中的最改动,新建的Table无效. 能够通过例如以下 ...
- 任务调度(三)——Timer的替代品ScheduledExecutorService简单介绍
先前的两篇博文<任务调度(一)--jdk自带的Timer>和<任务调度(二)--jdk自带的Timer 动态改动任务运行计划>中,简介了一下Timer,能够实现几本的功能.可是 ...
- web 开发之js---JS变量也要注意初始化
原先以为js作为弱类型语言,变量的初始化没必要,但是: var text; text+="你好"; alert(text); 对话框弹出的内容是:"undefined你好 ...
- hadoop分布式安装部署具体视频教程(网盘附配好环境的CentOS虚拟机文件/hadoop配置文件)
參考资源下载:http://pan.baidu.com/s/1ntwUij3视频安装教程:hadoop安装.flvVirtualBox虚拟机:hadoop.part1-part5.rarhadoop文 ...
- 【bzoj4592】[Shoi2015]脑洞治疗仪
由于脑洞的序列不会改变,考虑用线段树维护区间内sum,左边0的个数,右边0的个数,区间内最大脑洞.对于查询l~r最大脑洞可以将l~r分成logn个区间,总复杂度O(nlogn). #include&l ...
- tflearn 保存模型重新训练
from:https://stackoverflow.com/questions/41616292/how-to-load-and-retrain-tflean-model This is to cr ...
- jQuery.uploadify文件上传组件实例讲解
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
- 关于Flask的默认session
Flask的默认session利用了Werkzeug的SecureCookie,把信息做序列化(pickle)后编码(base64),放到cookie里了. 过期时间是通过cookie的过期时间实现的 ...
- hive 内部表与外部表的区别
hive 内部表: hive> create table soyo55(name STRING,addr STRING,money STRING) row format delimited fi ...