C# 真正完美的 汉字转拼音
网上有很多说自己整理的汉字转拼音是完美的,但使用后才发现都是半吊的瓶子,问题多多。
常见的生僻字,或多音字识别,转换后简直让人感觉可怕。
主流的转换有三种:hash匹配,Npinyin,微软PinYinConverter。
但单用这三个,都没法做到完美,为什么没人考虑融合呢?
我的方案:Npinyin+微软PinYinConverter(首选Npinyin)
微软PinYinConverter
为什么:微软PinYinConverter很强大,但在多音字面前,犯了传统的错误,按拼音字母排序。如【强】微软居然优先【jiang】而不是】【qiang】
所以不能优选 PinYinConverter。
Npinyin
很人性,很不错的第三方库,在传统多音字前优先使用率较高的,但在生僻字面前有点无法转换。(GetInitials(strChinese) 有Bug 如【洺】无法识别,但GetPinyin可以正常转换。)
总结:优先Npinyin 翻译失败的使用微软PinYinConverter。目测完美。
上代码:
public class PingYinHelper
{
private static Encoding gb2312 = Encoding.GetEncoding("GB2312"); /// <summary>
/// 汉字转全拼
/// </summary>
/// <param name="strChinese"></param>
/// <returns></returns>
public static string ConvertToAllSpell(string strChinese)
{
try
{
if (strChinese.Length != )
{
StringBuilder fullSpell = new StringBuilder();
for (int i = ; i < strChinese.Length; i++)
{
var chr = strChinese[i];
fullSpell.Append(GetSpell(chr));
} return fullSpell.ToString().ToUpper();
}
}
catch (Exception e)
{
Console.WriteLine("全拼转化出错!" + e.Message);
} return string.Empty;
} /// <summary>
/// 汉字转首字母
/// </summary>
/// <param name="strChinese"></param>
/// <returns></returns>
public static string GetFirstSpell(string strChinese)
{
//NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
//return NPinyin.Pinyin.GetInitials(strChinese); try
{
if (strChinese.Length != )
{
StringBuilder fullSpell = new StringBuilder();
for (int i = ; i < strChinese.Length; i++)
{
var chr = strChinese[i];
fullSpell.Append(GetSpell(chr)[]);
} return fullSpell.ToString().ToUpper();
}
}
catch (Exception e)
{
Console.WriteLine("首字母转化出错!" + e.Message);
} return string.Empty;
} private static string GetSpell(char chr)
{
var coverchr = NPinyin.Pinyin.GetPinyin(chr); bool isChineses = ChineseChar.IsValidChar(coverchr[]);
if (isChineses)
{
ChineseChar chineseChar = new ChineseChar(coverchr[]);
foreach (string value in chineseChar.Pinyins)
{
if (!string.IsNullOrEmpty(value))
{
return value.Remove(value.Length - , );
}
}
} return coverchr; }
}
抽了几个常见错字和姓名
测试如下:
[TestMethod]
public void PingyinTest()
{
Dictionary<string, Tuple<string, string>> dict = new
Dictionary<string, Tuple<string, string>>() {
{"梅钰", new Tuple<string,string>( "meiyu","MY")},
{"张洺", new Tuple<string,string>( "zhangming","ZM")},
{"王玥", new Tuple<string,string>( "wangyue","WY")},
{"王思琪", new Tuple<string,string>( "wangsiqi","WSQ")},
{"董云强", new Tuple<string,string>( "dongyunqiang","DYQ")},
{"宋红培", new Tuple<string,string>( "songhongpei","SHP")},
{"石磊", new Tuple<string,string>( "shilei","SL")},
}; foreach (var keyval in dict)
{
var name = keyval.Key; var spell1 = keyval.Value.Item1;
var spell2 = keyval.Value.Item2; var val = ChineseSpell.ConvertToAllSpell(name).TrimAll(); val = FlexLogicFramework.Library.CommonLib.PingYinHelper.ConvertToAllSpell(name)
.TrimAll().ToLower(); Assert.IsTrue(val == spell1, "转换错误"); val = FlexLogicFramework.Library.CommonLib.ChineseSpell.GetFirstSpell(name).TrimAll(); val = FlexLogicFramework.Library.CommonLib.PingYinHelper.GetFirstSpell(name).TrimAll(); Assert.IsTrue(val == spell2, "转换错误");
} }
C# 真正完美的 汉字转拼音的更多相关文章
- C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
汉字转拼音貌似一直是C#开发的一个难题,无论什么方案都有一定的bug,之前使用了两种方案. 1.Chinese2Spell.cs 一些不能识别的汉字全部转为Z 2.Microsoft Visual S ...
- js中文汉字按拼音排序
JavaScript 提供本地化文字排序,比如对中文按照拼音排序,不需要程序显示比较字符串拼音. String.prototype.localeCompare 在不考虑多音字的前提下,基本可以完美实现 ...
- 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 ...
- 【干货】JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多音字,有的不支持声调,有的字典文件太大,还比如有时候我仅仅是需要获取汉字拼音首字母却要引入200kb的字 ...
- C#汉字转拼音(支持多音字)
之前由于项目需要,中间需要一个汉字转拼音和首拼的功能来做查询,感觉这种功能基本已经成熟化了,于是查找了相关的代码,首先引入眼帘的是下面两篇文章 1.C# 汉字转拼音(支持GB2312字符集中所有汉字) ...
- C#汉字转拼音帮助类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- js汉字与拼音互转终极方案,附简单的JS拼音输入法【转】
github项目地址:https://github.com/liuxianan/pinyinjs 完整demo演示:http://demo.liuxianan.com/pinyinjs/ 汉字转拼音: ...
随机推荐
- TCP协议中的序列号
TCP 协议工作在OSI的传输层,是一种可靠的面向连接的数据流协议,TCP之所以可靠,是因为它保证了传送数据包的顺序.顺序是用一个序列号来保证的.响应包内也包括一个序列号,表示接收方准备好这个序列号的 ...
- FPGA前世今生(一)
关于FPGA,我想做硬件的或多或少都听过.从上世纪80年代算来,FPGA已走过了30多个年头.我们以FPGA两大生产厂商,两大巨头之一的INTEL(altera)公司的FPGA为例,为大家逐步介绍FP ...
- 1147 Heaps
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- xunsearch安装使用
目录 1.下载 2.进入,安装 3.安装成功后,启动后台服务 4.安装PHP-SDK 安装步骤 1.下载 wget http://www.xunsearch.com/download/xunsearc ...
- thinkphp实现多个子查询语句
sql语句博大精深 理解好sql语句,就能用好thinkphp等框架中的数据库操作 原sql SELECT a.*,b.* from (SELECT a.id as opener_id,a.name, ...
- PHP AST学习
前一阵和前同事交流在检测webshell方面的相关方法,其中提出了使用lex yacc做一套语法解析来解析字节码段来判断是否存在webshell. 后来在查找相关资料中,找到了github开源的一个工 ...
- MFC 控件使用汇总
一.动态创建button CButton *button=new CButton; button->Create(_T(,,,),);//最后一个是ID BEGIN_MESSAGE_MAP(CM ...
- leetcode421
public class Solution { public int FindMaximumXOR(int[] nums) { , mask = ; ; i >= ; i--) { mask = ...
- python发送包含html、图片、附件和链接的邮件
1.smtplib模块的使用 smtplib库用来发送邮件.需要用到的函数如下: 连接到SMTP服务器,参数为SMTP主机和端口: SMTP.connect([host[,port]]) 登录SMTP ...
- 一次 Mysql 字符集的报错,最后让我万马奔腾!!!
wuba---深圳---龙岗周边----3000元--------- wuba---深圳---龙岗周边----5000元--------- wuba---深圳---龙岗周边----8000元----- ...