一、使用PinYinConverterCore获取汉语拼音

最新在做一个搜索组件,需要使用汉语拼音的首字母查询出符合条件的物品名称,由于汉字存在多音字,所以自己写查询组件不太现实,因此,我们使用微软提供的PinYinConverterCore来实现汉字转拼音。使用Nuget搜索PinYinConverterCore下载并安装,具体如下:

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

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

  /// <summary>
/// 汉字转换拼音
/// </summary>
public static class PingYinUtil
{
private static Dictionary<int, List<string>> GetTotalPingYinDictionary(string text)
{
var chs = text.ToCharArray(); //记录每个汉字的全拼
Dictionary<int, List<string>> totalPingYinList = new Dictionary<int, List<string>>(); for (int i = 0; i < chs.Length; i++)
{
var pinyinList = new List<string>(); //是否是有效的汉字
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;
}
/// <summary>
/// 获取汉语拼音全拼
/// </summary>
/// <param name="text">The string.</param>
/// <returns></returns>
public static List<string> GetTotalPingYin(this string text)
{
var result = new List<string>();
foreach (var pys in GetTotalPingYinDictionary(text))
{
var items = pys.Value;
if (result.Count <= 0)
{
result = items;
}
else
{
//全拼循环匹配
var newTotalPingYinList = new List<string>();
foreach (var totalPingYin in result)
{
newTotalPingYinList.AddRange(items.Select(item => totalPingYin + item));
}
newTotalPingYinList = newTotalPingYinList.Distinct().ToList();
result = newTotalPingYinList;
}
}
return result;
} /// <summary>
/// 获取汉语拼音首字母
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static List<string> GetFirstPingYin(this string text)
{
var result = new List<string>();
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<string>();
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<string> Pinyin { get; set; }
}
  StudentList = new List<Student>
{
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();

最新文章

  1. 【C#|.NET】从细节出发(三) 逻辑层事务和page object模式
  2. python2.7.6 , setuptools pip install, 报错:UnicodeDecodeError:&#39;ascii&#39; codec can&#39;t decode byte
  3. HTTP基础03--HTTP报文
  4. 三大平衡树(Treap + Splay + SBT)总结+模板[转]
  5. mongodb 数据导入导出
  6. [译]JavaScript检测浏览器前缀
  7. c++ 怎样获取系统时间
  8. makefile debug
  9. Python核心编程笔记----注释
  10. oracle db server 改动主机名时的注意事项
  11. Jupyter(Python)中无法使用Cache原理分析
  12. 在MAC OS X中默认的Web共享目录
  13. AES加密解密 助手类 CBC加密模式
  14. Oracle数据库 Synonym和DBLink
  15. Asp.net APP 重置密码的方式
  16. [jQuery]相对父级元素的fixed定位
  17. Docker实现运行tomcat并部署项目war包,并实现挂载目录
  18. Python:判断文本中的用户名在数据库中是否存在,存在返回1,不存在返回0
  19. .net core HttpContext(Http上下文)
  20. Go学习笔记03-结构控制

热门文章

  1. [cf878D]Magic Breeding
  2. [bzoj3304]带限制的最长公共子序列
  3. spring boot 动态生成接口实现类
  4. 【2020五校联考NOIP #3】序列
  5. Codeforces 1076G - Array Game(博弈论+线段树)
  6. HDU 6036 Division Game
  7. 深入浅出KMP
  8. 『与善仁』Appium基础 — 19、元素定位工具(三)
  9. android studio 编译NDK android studio 生成.so文件
  10. Mysql不锁表备份之Xtrabackup的备份与恢复