首页 > 代码库 > C# 拼音通用类

C# 拼音通用类

因为项目需求,查找资料并编写了这个拼音通用类。在博客园分享:)

其中的多音字首拼,感觉还是非常实用的。可识别中文、英文和符号

  1 using System;  2 using System.Collections.Generic;  3 using System.Collections.ObjectModel;  4 using System.Linq;  5 using System.Text;  6 using Microsoft.International.Converters.PinYinConverter;  7   8 namespace MyConsoleApp  9 { 10     public sealed class PinYin 11     { 12         private PinYin() { } 13  14         /// <summary> 15         /// 获取首拼或全拼。大写 16         /// </summary> 17         /// <param name="chineseString"></param> 18         /// <param name="isFull">true为全拼,false为首拼</param> 19         /// <returns></returns> 20         public static string GetPY(string chineseString, bool isFull) 21         { 22             StringBuilder sb = new StringBuilder(50); 23             foreach (var item in chineseString.ToCharArray()) 24             { 25                 if (!ChineseChar.IsValidChar(item)) 26                 { 27                     sb.Append(item); 28                 } 29                 else 30                 { 31                     System.Collections.ObjectModel.ReadOnlyCollection<string> py = new ChineseChar(item).Pinyins; 32                     if (py.Count > 0 && py[0].Length > 0) 33                     { 34                         if (isFull) 35                             sb.Append(py[0].Substring(0, py[0].Length - 1)); 36                         else 37                             sb.Append(py[0][0]); 38                     } 39                 } 40             } 41             return sb.ToString(); 42         } 43  44         /// <summary> 45         /// 获取首拼和全拼 46         /// </summary> 47         /// <param name="chineseString"></param> 48         /// <param name="pyStr"></param> 49         /// <param name="pyFullStr"></param> 50         public static void GetPY(string chineseString, out string pyStr, out string pyFullStr) 51         { 52             StringBuilder sb = new StringBuilder(50); 53             StringBuilder sbFull = new StringBuilder(50); 54             foreach (var item in chineseString.ToCharArray()) 55             { 56                 if (!ChineseChar.IsValidChar(item)) 57                 { 58                     sb.Append(item); 59                     sbFull.Append(item); 60                 } 61                 else 62                 { 63                     System.Collections.ObjectModel.ReadOnlyCollection<string> py = new ChineseChar(item).Pinyins; 64                     if (py.Count > 0 && py[0].Length > 0) 65                     { 66                         sbFull.Append(py[0].Substring(0, py[0].Length - 1)); 67                         sb.Append(py[0][0]); 68                     } 69                 } 70             } 71  72             pyStr = sb.ToString(); 73             pyFullStr = sbFull.ToString(); 74         } 75  76         /// <summary> 77         /// 获取首拼(多音字"|"分隔)和全拼 78         /// </summary> 79         /// <param name="chineseString"></param> 80         /// <param name="pyStr"></param> 81         /// <param name="pyFullStr"></param> 82         public static void GetHomoPhonePY(string chineseString, out string pyStr, out string pyFullStr) 83         { 84             StringBuilder sb = new StringBuilder(50); 85             StringBuilder sbFull = new StringBuilder(50); 86  87             char[] chineseCharArray = chineseString.ToCharArray(); 88             Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>(); 89  90             for (int i = 0; i < chineseCharArray.Length; i++) 91             { 92                 char item = chineseCharArray[i]; 93                 if (!ChineseChar.IsValidChar(item)) 94                 { 95                     sb.Append(item); 96                     sbFull.Append(item); 97                 } 98                 else 99                 {100                     ChineseChar chinaChar = new ChineseChar(item);101                     ReadOnlyCollection<string> py = chinaChar.Pinyins;102                     if (py.Count > 0 && py[0].Length > 0)103                     {104                         sbFull.Append(py[0].Substring(0, py[0].Length - 1));105                         dic.Add(i, py.Where(p => !string.IsNullOrEmpty(p)).Select(p => p[0].ToString()).Distinct().ToList());106                     }107                 }108             }109 110             OutPutHomoPhone(dic, dic.Keys.Count, 0, "", ref sb);111 112             pyStr = sb.ToString();113             pyFullStr = sbFull.ToString();114         }115 116         /// <summary>117         /// 获取多音字首拼(多音字"|"分隔)118         /// 西门子SIEMENS商标->XMZSIEMENSSB119         /// </summary>120         /// <param name="chineseString"></param>121         /// <returns></returns>122         public static string GetHomoPhonePY(string chineseString)123         {124             StringBuilder sb = new StringBuilder(50);125 126             char[] chineseCharArray = chineseString.ToCharArray();127             Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();128 129             for (int i = 0; i < chineseCharArray.Length; i++)130             {131                 char item = chineseCharArray[i];132                 if (!ChineseChar.IsValidChar(item))133                 {134                     dic.Add(i, new List<string> { item.ToString() });135                 }136                 else137                 {138                     ChineseChar chinaChar = new ChineseChar(item);139                     ReadOnlyCollection<string> py = chinaChar.Pinyins;140                     if (py.Count > 0 && py[0].Length > 0)141                     {142                         dic.Add(i, py.Where(p => !string.IsNullOrEmpty(p)).Select(p => p[0].ToString()).Distinct().ToList());143                     }144                 }145             }146 147             OutPutHomoPhone(dic, dic.Keys.Count, 0, "", ref sb);148 149             return sb.ToString();150         }151 152         /// <summary>153         /// 递归出多音字的首拼154         /// </summary>155         /// <param name="dic">多音字字典</param>156         /// <param name="len">字数</param>157         /// <param name="key">字典下标</param>158         /// <param name="parentValue">递归的首拼</param>159         /// <param name="sb">结果对象</param>160         private static void OutPutHomoPhone(161             Dictionary<int, List<string>> dic,162             int len,163             int key,164             string parentValue,165             ref StringBuilder sb)166         {167             if (dic.Keys.Contains(key))168             {169                 foreach (string x in dic[key])170                 {171                     if (dic.Keys.Count - 1 >= key)172                     {173                         string outString = string.Format("{0}{1}", parentValue, x);174                         if (outString.Length == len)175                         {176                             sb.AppendFormat("{0}|", outString);177                         }178                         OutPutHomoPhone(dic, len, key + 1, outString, ref sb);179                     }180                 }181             }182         }183 184     }185 }
View Code