C#获取汉字首字母,多音拼音

前面写的几篇都是没有完善的,目前修改这个是最好的

 

ChineseChar类可在这里找到DLL

http://download.csdn.net/detail/qq873113580/6033615

 

全部代码

 
  

 

using Microsoft.International.Converters.PinYinConverter; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;

namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             List a = PinYingHelper.GetPYList("l奇】");             List b = PinYingHelper.GetPYFullList("l奇】");             List c = PinYingHelper.GetPYInitialList("l奇】");         }     } }

 

 

 

using Microsoft.International.Converters.PinYinConverter; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;

namespace ConsoleApplication1 {     public class PinYingHelper     {         #region  获取汉字首字母包含多音如 “奇” 返回 new List{"Q","J"};         ///

        /// 获取汉字首字母包含多音         ///         ///         ///         public static List GetPYInitialList(string str)         {             List list = new List();             return GetPYInitialList(GetPYList(str));         }

        ///

        /// 获取汉字首字母包含多音         ///         /// 传入GetPY这个方法返回的对象         ///         public static List GetPYInitialList(List pyList)         {             List list = new List();             foreach (string py in pyList)             {                 string[] strs = py.Split('$');                 StringBuilder sb = new StringBuilder();                 foreach (string s in strs)                 {                     if (s.Length > 0)                     {                         sb.Append(s.Substring(0, 1));                     }                 }                 list.Add(sb.ToString());             }             return list;         }         #endregion

        #region 获取拼音包含多音,如"奇"返回new List{"QI","JI"};         ///

        /// 获取拼音包含多音         ///         /// 传入GetPY这个方法返回的对象         ///         public static List GetPYFullList(List pyList)         {             List list = new List();             foreach (string py in pyList)             {                 list.Add(py.Replace("$", ""));             }             return list;         }         ///         /// 获取拼音包含多音         ///         ///         ///         public static List GetPYFullList(string str)         {             List list = new List();             return GetPYFullList(GetPYList(str));         }         #endregion

        #region 获取汉字的拼音包含多音字用"$"连接,如 "奇怪" 返回 new List{"QI$GUAI","JI$GUAI"};         ///

        /// 获取汉字的拼音包含多音字用"$"连接         ///         ///         ///         ///         ///         public static List GetPYList(string str, List pyList = null, bool isFirst = true)         {             if (pyList == null)             {                 pyList = new List();             }             if (str.Length > 1)             {                 string tmp = str.Substring(0, 1);                 string remain = str.Substring(1);                 List tmpList = GetPYALL(tmp);                 if (isFirst)                 {                     pyList.AddRange(tmpList);                     pyList = GetPYList(remain, pyList, false);                 }                 else                 {                     pyList = GetPYList(remain, CombinationList(pyList, tmpList), false);                 }             }             else             {                 if (pyList.Count > 0)                 {                     pyList = CombinationList(pyList, GetPYALL(str));                 }                 else                 {                     pyList.AddRange(GetPYALL(str));                 }             }             return pyList;         }         #endregion

        #region 组合2个List,内部使用         ///

        /// 组合2个List         ///         ///         ///         ///         private static List CombinationList(List list1, List list2)         {             List tmpPYList = new List();             foreach (string i in list1)             {                 foreach (string j in list2)                 {                     tmpPYList.Add(i + "$" + j);                 }             }             return tmpPYList;         }         #endregion

        #region 获取全拼,包含多音,内部使用         ///

        /// 获取全拼,包含多音         ///         ///         /// 返回一个多音集合         private static List GetPYALL(string str)         {             List soruceList = new List();             char[] chars = str.ToCharArray();             for (int i = 0; i < chars.Length; i++)             {                 if (new Regex(@"^[\u4e00-\u9fa5]+$").Match(chars[i].ToString()).Success)                 {                     ChineseChar chineseChar = new ChineseChar(chars[i]);                     ReadOnlyCollection pyColl = chineseChar.Pinyins;                     //循环生成首字母的笛卡尔积,存储到临时拼音列表                     foreach (var item in pyColl)                     {                         if (item != null)                         {                             string temp = item.Remove(item.Length - 1, 1);                             if (!soruceList.Contains(temp))                             {                                 soruceList.Add(temp);                             }                         }                     }                 }                 else//不是汉字的                 {                     soruceList.Add(chars[i].ToString().ToUpper());                 }             }             return soruceList;         }         #endregion     } }

你可能感兴趣的:(C#获取汉字首字母,多音拼音)