C#漢字轉拼音
下載並引入兩個dll文件 NPinyin.dll 和 ChnCharInfo.dll
其實這兩個dll 任何一個都可以實現漢字轉拼音,然而 NPinyin.dll 收錄的漢字並不全,但是很人性化,能識別一些常用的漢字。ChnCharInfo.dll 是微軟的很全但是不人性化。另外本套代碼外有一個自己維護的個別漢字文件,例如一些多音字姓氏。
本程序的使用場景是姓名轉拼音,所以先判斷第一個漢字是否在自己維護的拼音庫中存在,如果存在者優先使用這個庫中漢字所對應的拼音,如果不存在者優先使用NPinyin庫中轉化拼音的方法,轉化失敗再使用微軟提供的ChnCharInfo中轉拼音的方法。
Main函數
static void Main(string[] args) { //方案: //Npinyin+微軟PinYinConverter +自己維護的個別漢字文件(多音字姓氏) #region 姓名轉拼音
//獲取全拼 string path = @"PinYinDic.txt"; //本程序維護的 PinYinDic.txt,該文件放在"\bin\Debug"目錄下,當然也可以不使用。 StreamReader sr = new StreamReader(path, Encoding.Default); Dictionary<char, string> pinyinDic = JsonConvert.DeserializeObject<Dictionary<char, string>>(sr.ReadToEnd());
string name = "薄露露"; //bolulu Console.WriteLine(name + PinYinNameHelper.ConvertToAllSpell(name, pinyinDic)); Console.WriteLine(name + PinYinNameHelper.GetFirstSpell(name, pinyinDic)); Console.ReadKey();
string name2 = "單丹單"; //shandandan Console.WriteLine(name2 + PinYinNameHelper.ConvertToAllSpell(name2, pinyinDic)); Console.WriteLine(name2 + PinYinNameHelper.GetFirstSpell(name2, pinyinDic)); Console.ReadKey();
string name3 = "洺"; // Console.WriteLine(name3 + PinYinNameHelper.ConvertToAllSpell(name3, pinyinDic)); Console.WriteLine(name3 + PinYinNameHelper.GetFirstSpell(name3, pinyinDic)); Console.ReadKey();
#endregion } |
PinYinNameHelper類
using Microsoft.International.Converters.PinYinConverter; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Function.ClassLibrary { /// <summary> /// 漢字轉拼音(姓名轉拼音) /// 方案: /// Npinyin+微軟PinYinConverter +自己維護的個別漢字文件(多音字姓氏) /// 其實這兩個dll 任何一個都可以實現漢字轉拼音,然而 NPinyin.dll 收錄的漢字並不全,但是很人性化,能識別一些常用的漢字。 /// 微軟的很全但是不人性化。另外本套代碼外有一個自己維護的個別漢字文件,例如一些多音字姓氏。 /// ///本程序的使用場景是姓名轉拼音,所以先判斷第一個漢字是否在自己維護的拼音庫中存在, ///如果存在者優先使用這個庫中漢字所對應的拼音, ///如果不存在者優先使用NPinyin庫中轉化拼音的方法,轉化失敗再使用微軟提供的ChnCharInfo中轉拼音的方法。 /// </summary> public class PinYinNameHelper { private static Encoding gb2312 = Encoding.GetEncoding("GB2312");
/// <summary> /// 漢字轉全拼 /// </summary> /// <param name="strChinese"></param> /// <param name="pinyinDic"></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> /// <param name="pinyinDic"></param> /// <returns></returns> public static string GetFirstSpell(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); }
char[] chars = pinyin.ToCharArray(); //字符串轉Char[]
fullSpell.Append(pinyin[0]); //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" } |