日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#汉字转拼音

發布時間:2024/9/20 C# 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#汉字转拼音 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下載并引入兩個dll文件?NPinyin.dll?和?ChnCharInfo.dll?

  其實這兩個dll 任何一個都可以實現漢字轉拼音,然而?NPinyin.dll 收錄的漢字并不全,但是很人性化,能識別一些常用的漢字。ChnCharInfo.dll?是微軟的很全但是不人性化。另外本套代碼外有一個自己維護的個別漢字文件,例如一些多音字姓氏。

  本程序的使用場景是姓名轉拼音,所以先判斷第一個漢字是否在自己維護的拼音庫中存在,如果存在者優先使用這個庫中漢字所對應的拼音,如果不存在者優先使用NPinyin庫中轉化拼音的方法,轉化失敗再使用微軟提供的ChnCharInfo中轉拼音的方法。

  Main函數

static void Main(string[] args){string path = @"PinYinDic.txt";StreamReader sr = new StreamReader(path, Encoding.Default);Dictionary<char, string> pinyinDic = JsonConvert.DeserializeObject<Dictionary<char, string>>(sr.ReadToEnd());string name = "薄露露";Console.WriteLine(name+PinYinHelper.ConvertToAllSpell(name, pinyinDic).ToLower());Console.ReadKey();} PinYinHelper類 public class PinYinHelper{private static Encoding gb2312 = Encoding.GetEncoding("GB2312");/// <summary>/// 漢字轉全拼/// </summary>/// <param name="strChinese"></param>/// <returns></returns>public static string ConvertToAllSpell(string strChinese, IDictionary<char, string> pinyinDic = null){try{if (strChinese.Length != 0){StringBuilder fullSpell = new StringBuilder();for (int i = 0; i < strChinese.Length; i++){var chr = strChinese[i];string pinyin = string.Empty;if (i == 0){pinyin = GetFromPinYinDic(chr, pinyinDic); }if (pinyin.Length == 0){pinyin = GetSpell(chr);}fullSpell.Append(pinyin);}return fullSpell.ToString().ToLower();}}catch (Exception e){Console.WriteLine("全拼轉化出錯!" + e.Message);}return string.Empty;}/// <summary>/// 漢字轉首字母/// </summary>/// <param name="strChinese"></param>/// <returns></returns>public static string GetFirstSpell(string strChinese){//NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺無法識別//return NPinyin.Pinyin.GetInitials(strChinese);try{if (strChinese.Length != 0){StringBuilder fullSpell = new StringBuilder();for (int i = 0; i < strChinese.Length; i++){var chr = strChinese[i];fullSpell.Append(GetSpell(chr)[0]);}return fullSpell.ToString().ToUpper();}}catch (Exception e){Console.WriteLine("首字母轉化出錯!" + e.Message);}return string.Empty;}private static string GetSpell(char chr){var coverchr = NPinyin.Pinyin.GetPinyin(chr);bool isChineses = ChineseChar.IsValidChar(coverchr[0]);if (isChineses){ChineseChar chineseChar = new ChineseChar(coverchr[0]);foreach (string value in chineseChar.Pinyins){if (!string.IsNullOrEmpty(value)){return value.Remove(value.Length - 1, 1);}}}return coverchr;}/// <summary>/// 從字典獲取拼音/// </summary>/// <param name="c">字</param>/// <param name="pinyinDic">字典</param>/// <returns></returns>private static string GetFromPinYinDic(char c, IDictionary<char, string> pinyinDic){if (pinyinDic == null|| pinyinDic.Count == 0|| !pinyinDic.ContainsKey(c)){return "";}return pinyinDic[c];}}

接下來是本程序維護的?PinYinDic.txt,該文件放在"\bin\Debug"目錄下,當然也可以不使用。

{ "紅":"hong", "賈":"jia", "薄":"bo", "褚":"chu", "翟":"zhai", "郇":"xun", "蓋":"ge", "樂":"yue", "區":"ou", "卜":"bu", "曾":"zeng", "丁":"ding", "無":"wu", "長":"chang", "其":"qi", "巷":"xiang", "將":"jiang", "氏":"shi", "色":"se", "系":"xi", "重":"chong", "乜":"nie", "孛":"bo", "卒":"zu", "單":"shan", "解":"xie", "仇":"qiu", "隗":"wei", "查":"zha", "繁":"po", "樸":"piao" }

總結

以上是生活随笔為你收集整理的C#汉字转拼音的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。