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 { ...
随机推荐
- dubbo的jmeter压测时jar包的热加载/动态加载
在做dubbo的jmeter压测时,需要把jar包放入jmeter的lib/ext目录下,但是jmeter启动的时候会自动加载这个目录lib目录及lib/ext目录,这样启动后放入这些目录下的jar包 ...
- day03-hdfs的客户端操作\hdfs的java客户端编程
5.hdfs的客户端操作 客户端的理解 hdfs的客户端有多种形式: 1.网页形式 2.命令行形式 3.客户端在哪里运行,没有约束,只要运行客户端的机器能够跟hdfs集群联网 文件的切块大小和存储的副 ...
- [Java Sprint] Spring XML Configuration : Constructor Injection Demo
Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html Now le ...
- CCNP路由实验之八 路由重公布
CCNP路由实验之八 路由重公布 在前面几个实验,已经学习了静态路由和动态路由.如今,我们要掌握怎样使它们在一个网络中融合,即路由重公布. 使用出站口作为静态路由 0 使用下一跳地址作为静态路由 ...
- 三分钟迁移Spring boot工程到Serverless
前言 Spring Boot已成为当今最流行的Java后端开发框架,典型的应用方式是在云上购买一台虚拟机,每天24小时在上面运行Java程序,在这种情况下,用户必须维护自己的虚拟机环境,而且按照包月包 ...
- react 开发过程中的总结/归纳
1.点击元素,获取绑定该事件的父级元素,使用 e.currentTarget.e.target 获取的是,出发该事件的元素,该元素有可能是所绑定事件的元素的子元素. 2.使用 react router ...
- socketserver模块三次登陆验证,身份验证
帅爆太阳的男人 1,socketserver是解决TCP服务器和多个客户端进行通信 服务器: import socketserver class MySocket(socketserver,BaseR ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] C C Problem about Polyline 数学
C. A Problem about Polyline ...
- Explicit Interface Implementation (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173157.aspx If a class implements two interfaces that con ...
- Docker Image发布
Docker Image发布 方法1:导出镜像 #docker save -o centos-httpd-docker-image.tar centos:httpd 使用加载本地镜像 docker l ...