C#实现汉字转汉语拼音的示例代码

这篇文章主要介绍了如何利用C#实现汉字转汉语拼音,文中的示例代码讲解详细,对我们学习C#有一定帮助,感兴趣的小伙伴可以跟随小编一起动手试一试

一、使用PinYinConverterCore获取汉语拼音

最新在做一个搜索组件,需要使用汉语拼音的首字母查询出符合条件的物品名称,由于汉字存在多音字,所以自己写查询组件不太现实,因此,我们使用微软提供的CHSPinYinConvCHSPinYinConv在.net core下载安装没有问题,但在.net framework会由于兼容性会安装失败,因此使用了PinYinConverterCore来实现汉字转拼音,PinYinConverterCore应该也是基于CHSPinYinConv开发的兼容包,后续的代码两个安装包环境下都可以使用。使用Nuget搜索PinYinConverterCore下载并安装,具体如下:

二、编写工具扩展类实现获取汉字的拼音

由于汉字存在多音字,因此,通过汉字获取到的拼音是一个数组,具体如下:

 ///  /// 汉字转换拼音 ///  public static class PingYinUtil { private static Dictionary> GetTotalPingYinDictionary(string text) { var chs = text.ToCharArray(); //记录每个汉字的全拼 Dictionary> totalPingYinList = new Dictionary>(); for (int i = 0; i (); //是否是有效的汉字 if (ChineseChar.IsValidChar(chs[i])) { ChineseChar cc = new ChineseChar(chs[i]); pinyinList = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList(); } else { pinyinList.Add(chs[i].ToString()); } //去除声调,转小写 pinyinList = pinyinList.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower()); //去重 pinyinList = pinyinList.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList(); if (pinyinList.Any()) { totalPingYinList[i] = pinyinList; } } return totalPingYinList; } ///  /// 获取汉语拼音全拼 ///  /// The string. ///  public static List GetTotalPingYin(this string text) { var result = new List(); foreach (var pys in GetTotalPingYinDictionary(text)) { var items = pys.Value; if (result.Count <= 0) { result = items; } else { //全拼循环匹配 var newTotalPingYinList = new List(); foreach (var totalPingYin in result) { newTotalPingYinList.AddRange(items.Select(item => totalPingYin + item)); } newTotalPingYinList = newTotalPingYinList.Distinct().ToList(); result = newTotalPingYinList; } } return result; } ///  /// 获取汉语拼音首字母 ///  ///  ///  public static List GetFirstPingYin(this string text) { var result = new List(); foreach (var pys in GetTotalPingYinDictionary(text)) { var items = pys.Value; if (result.Count <= 0) { result = items.ConvertAll(p => p.Substring(0, 1)).Distinct().ToList(); } else { //首字母循环匹配 var newFirstPingYinList = new List(); foreach (var firstPingYin in result) { newFirstPingYinList.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1))); } newFirstPingYinList = newFirstPingYinList.Distinct().ToList(); result = newFirstPingYinList; } } return result; } }

三、编写测试用例

我们编写一个测试用例,通过输入的汉字获取到汉语拼音的全拼和首字母缩写,具体如下:

// 汉字输入 string text = TextBoxInput.Text; // 获取到汉语拼音的全拼 TextBoxTotal.Text = string.Join(",", text.GetTotalPingYin()); // 获取到汉语拼音的首字母 TextBoxFirst.Text = string.Join(",", text.GetFirstPingYin());

我们编写录入一组用户名,然后根据输入输入的用户名的缩写,筛选出符合条件的人,我们可以使用Linq模糊查询,具体如下:

public class Student { public string Name { get; set; } public List Pinyin { get; set; } }
 StudentList = new List { new Student() {Name = "张三"}, new Student() {Name = "章黎"}, new Student() {Name = "张三丰"}, new Student() {Name = "李四"}, new Student() {Name = "王五"}, new Student() {Name = "John"}, new Student() {Name = "W.吴"}, new Student() {Name = "阿姨"}, new Student() {Name = "阿胶"}, new Student() {Name = "麦合苏提.麦合苏提"} };
var text = TextBoxSearch.Text; foreach (var student in StudentList) { student.Pinyin = student.Name.GetFirstPingYin(); } StudentList = StudentList.Where(s => s.Pinyin.Exists(p=>p.Contains(text))).ToList();

到此这篇关于C#实现汉字转汉语拼音的示例代码的文章就介绍到这了,更多相关C#汉字转拼音内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C#实现汉字转汉语拼音的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程