`

Python实现返回汉字首字母

阅读更多

Python实现返回汉字首字母,该程序只能识别常规拼音,不能识别多音字。

  • #coding=gbk   
  • def multi_get_letter(str_input):     
  •     if isinstance(str_input, unicode):     
  •         unicode_str = str_input     
  •     else:     
  •         try:     
  •             unicode_str = str_input.decode('utf8')     
  •         except:     
  •             try:     
  •                 unicode_str = str_input.decode('gbk')     
  •             except:     
  •                 print 'unknown coding'    
  •                 return    
  •          
  •     return_list = []     
  •     for one_unicode in unicode_str:     
  • #        print single_get_first(one_unicode)      
  •         return_list.append(single_get_first(one_unicode))     
  •     return "".join(return_list)         
  •          
  • def single_get_first(unicode1):     
  •     str1 = unicode1.encode('gbk')     
  •     try:             
  •         ord(str1)     
  •         return str1     
  •     except:     
  •         asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536    
  •         if asc >= -20319 and asc <= -20284:     
  •             return 'a'    
  •         if asc >= -20283 and asc <= -19776:     
  •             return 'b'    
  •         if asc >= -19775 and asc <= -19219:     
  •             return 'c'    
  •         if asc >= -19218 and asc <= -18711:     
  •             return 'd'    
  •         if asc >= -18710 and asc <= -18527:     
  •             return 'e'    
  •         if asc >= -18526 and asc <= -18240:     
  •             return 'f'    
  •         if asc >= -18239 and asc <= -17923:     
  •             return 'g'    
  •         if asc >= -17922 and asc <= -17418:     
  •             return 'h'    
  •         if asc >= -17417 and asc <= -16475:     
  •             return 'j'    
  •         if asc >= -16474 and asc <= -16213:     
  •             return 'k'    
  •         if asc >= -16212 and asc <= -15641:     
  •             return 'l'    
  •         if asc >= -15640 and asc <= -15166:     
  •             return 'm'    
  •         if asc >= -15165 and asc <= -14923:     
  •             return 'n'    
  •         if asc >= -14922 and asc <= -14915:     
  •             return 'o'    
  •         if asc >= -14914 and asc <= -14631:     
  •             return 'p'    
  •         if asc >= -14630 and asc <= -14150:     
  •             return 'q'    
  •         if asc >= -14149 and asc <= -14091:     
  •             return 'r'    
  •         if asc >= -14090 and asc <= -13119:     
  •             return 's'    
  •         if asc >= -13118 and asc <= -12839:     
  •             return 't'    
  •         if asc >= -12838 and asc <= -12557:     
  •             return 'w'    
  •         if asc >= -12556 and asc <= -11848:     
  •             return 'x'    
  •         if asc >= -11847 and asc <= -11056:     
  •             return 'y'    
  •         if asc >= -11055 and asc <= -10247:     
  •             return 'z'    
  •         return ''    
  • f=multi_get_letter("啊")  
  • print f  
分享到:
评论

相关推荐

    python获取一组汉字拼音首字母的方法

    主要介绍了python获取一组汉字拼音首字母的方法,涉及Python针对汉字操作的相关技巧,需要的朋友可以参考下

    python实现按首字母分类查找功能

    本文实例为大家分享了python实现按首字母分类查找的具体代码,供大家参考,具体内容如下 要求: 1.自己查找一些英文词汇,存储到某个容器类中 2.根据英文词汇的首字母进行分类,类似于手机通讯簿中的快速查找功能 3...

    python实现CNN中文文本分类

    CNN 中文文本挖掘 文本分类 python 深度学习 机器学习 CNN 中文文本挖掘 文本分类 python 深度学习 机器学习

    python实现大风车动画效果实现

    python实现大风车动画效果实现python实现大风车动画效果实现python实现大风车动画效果实现python实现大风车动画效果实现python实现大风车动画效果实现python实现大风车动画效果实现python实现大风车动画效果实现...

    python读写汉字转成中文

    python读取Excel表格的文件,并且把中文转换成英文,然后首字母大写并拼接成@Anqing|安庆|36这种格式,写入txt文件中

    python实现按键精灵工具合集

    python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵工具合集python实现按键精灵...

    实验六 ARQ协议的Python实现

    2、学会使用Python实现ARQ协议 3、通过实验观察和分析ARQ协议在数据传输中的可靠性 二、实验原理 1、ARQ协议:Automatic Repeat Request (ARQ)协议是一种用于提高通信可靠性的错误控制方法。当发送端发送数据包时,...

    《Python提取中文拼音首字母》

    Python转换中文为拼音,提取拼音首字母。 本程序没有使用任何第三方模块,纯ascii码转换,不支持数字的转换提取。 适合不能使用第三方模块,或者想自造轮子等场景。

    (深信息)用python实现贪吃蛇小游戏毕业设计报告 .docx

    (深信息)用python实现贪吃蛇小游戏毕业设计报告 .docx(深信息)用python实现贪吃蛇小游戏毕业设计报告 .docx(深信息)用python实现贪吃蛇小游戏毕业设计报告 .docx(深信息)用python实现贪吃蛇小游戏毕业设计报告 .docx...

    智能问答系统python实现

    基于知识图谱的智能问答系统python实现(复旦大学论文基于qa语料和知识库的问答系统)_python 智能问答,python 智能问答系统-机器学习代码类资源本代码实现是基于python实现的基于复旦大学崔万云博士的learing ...

    python实现自动贪吃蛇效果实现

    python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现python实现自动贪吃蛇效果实现...

    Python实现中文文字雨源代码

    Python实现中文文字雨源代码,代码中用的字体SimHei.ttf可自行下载,该代码基于pygame实现了中文文字雨

    python实现自由落体功能效果实现

    python实现自由落体功能效果实现python实现自由落体功能效果实现python实现自由落体功能效果实现python实现自由落体功能效果实现python实现自由落体功能效果实现python实现自由落体功能效果实现python实现自由落体...

    Eclat算法Python实现

    该资源是Eclat算法Python实现代码,简洁实用。

    python 实现 设计模式

    python 实现 建造者模式 python 实现 状态模式1 python 实现 状态模式2(面向过程的方式-方法版) python 实现 状态模式3(分类版) python 实现 状态模式4 python 实现 抽象工厂模式1 python 实现 抽象工厂模式2 ...

    python实现算术编码

    用python实现算术编码,能够实现编码与译码。编码的方法是通过对一篇已知文章求取概率空间后,对待翻译文章进行算术编码,并能够基于已知文章重新进行算术解码。

    使用python实现Radon变换的反变换

    使用Python复现seismiclab的forward_radon_freq函数 实现Radon变换从变换域到原始数据域的变换。 也就是,使用python实现Radon变换的反变换。

    python实现签名ElGamal算法工程文件

    python实现签名ElGamal算法工程文件 详解博客地址:https://blog.csdn.net/m0_52316372/article/details/125695780

    python实现的弹窗提醒功能

    python实现的弹窗提醒功能

    python实现BP神经网络回归预测模型

    python实现实 BP神经网络回归预测模型 神 主要介绍了python实现BP神经网络回归预测模型,文中通过示例代码介绍的非常详细,对大家的学习或者工作 具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...

Global site tag (gtag.js) - Google Analytics