網站首頁 編程語言 正文
一、使用PinYinConverterCore獲取漢語拼音
最新在做一個搜索組件,需要使用漢語拼音的首字母查詢出符合條件的物品名稱,由于漢字存在多音字,所以自己寫查詢組件不太現實,因此,我們使用微軟提供的CHSPinYinConv
,CHSPinYinConv
在.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 < chs.Length; i++) { var pinyinList = new List (); //是否是有效的漢字 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 ListPinyin { 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();
原文鏈接:https://www.cnblogs.com/dongweian/p/15715099.html
相關推薦
- 2022-06-14 web項目中golang性能監控解析_Golang
- 2023-01-14 C++實現一個簡單消息隊列的示例詳解_C 語言
- 2022-08-18 詳解Flutter中數據傳遞的方式_Android
- 2022-07-22 防火墻.iptables-tcp-flags防止nmap端口掃描
- 2022-04-12 Python實現數據地址實體抽取_python
- 2022-06-01 Vertica集成Apache?Hudi重磅使用指南_相關技巧
- 2022-07-26 當本地ping通虛擬機,虛擬機ping不通本地時。
- 2022-12-11 Dos判斷文件夾是否存在,如果存在刪除,不存在創建_DOS/BAT
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支