C#漢字轉漢語拼音

Dwaynerbing發表於2021-12-21

一、使用PinYinConverterCore獲取漢語拼音

最新在做一個搜尋元件,需要使用漢語拼音的首字母查詢出符合條件的物品名稱,由於漢字存在多音字,所以自己寫查詢元件不太現實,因此,我們使用微軟提供的PinYinConverterCore來實現漢字轉拼音。使用Nuget搜尋PinYinConverterCore下載並安裝,具體如下:

image-20211221140230709

二、編寫工具擴充套件類實現獲取漢字的拼音

由於漢字存在多音字,因此,通過漢字獲取到的拼音是一個陣列,具體如下:

  /// <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());

image-20211221140904565

我們編寫錄入一組使用者名稱,然後根據輸入輸入的使用者名稱的縮寫,篩選出符合條件的人,我們可以使用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();

image-20211221141311784

相關文章