熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#漢字轉拼音功能類

2013-11-13 10:26:03  來源: .NET編程 

  此代碼屬改造而來沒有太深的技術難度僅僅是排除了原來代碼的錯誤增加了一些常規需求目前此類可以實現的功能如下
    對中文字符串進行拼音轉換的基本功能
    支持並可自定義拼音單詞間的分隔符
    支持並可自定義是否將拼音單詞首字母大寫
    支持並可自定義多音詞預定音
    支持並可自定義轉換過程中要排除解析的字符例如中文標點

  輸出示例

  引發 SystemWebUIControlInit 事件以對頁進行初始化
    YinFa SystemWebUIControlInit ShiJianYiDuiYeJinXingChuShiHua

  Hello!重慶和深圳的特色各是什麼呢?
    Hello!Chong Qing He Shen Zhen De Te Se Ge Shi ShenMe Ne ?

  代碼如下
    /**//// <summary>
    /// 漢字轉拼音工具
    /// </summary>
    public sealed class CHSPinYin
    {
    /**//// <summary>
    /// 包含字符 ASC 碼的整形數組
    /// </summary>
    private static int[] pv = new int[] { };

  /**//// <summary>
    /// 包含漢字拼音的字符串數組
    /// </summary>
    private static string[] ps = new string[] { a ai an ang ao ba bai ban bang bao bei ben beng bi bian biao bie bin bing bo bu ca cai can cang cao ce ceng cha chai chan chang chao che chen cheng chi chong chou chu chuai chuan chuang chui chun chuo ci cong cou cu cuan cui cun cuo da dai dan dang dao de deng di dian diao die ding diu dong dou du duan dui dun duo e en er fa fan fang fei fen feng fo fou fu ga gai gan gang gao ge gei gen geng gong gou gu gua guai guan guang gui gun guo ha hai han hang hao he hei hen heng hong hou hu hua huai huan huang hui hun huo ji jia jian jiang jiao jie jin jing jiong jiu ju juan jue jun ka kai kan kang kao ke ken keng kong kou ku kua kuai kuan kuang kui kun kuo la lai lan lang lao le lei leng li lia lian liang liao lie lin ling liu long lou lu lv luan lue lun luo ma mai man mang mao me mei men meng mi mian miao mie min ming miu mo mou mu na nai nan nang nao ne nei nen neng ni nian niang niao nie nin ning niu nong nu nv nuan nue nuo o ou pa pai pan pang pao pei pen peng pi pian piao pie pin ping po pu qi qia qian qiang qiao qie qin qing qiong qiu qu quan que qun ran rang rao re ren reng ri rong rou ru ruan rui run ruo sa sai san sang sao se sen seng sha shai shan shang shao she shen sheng shi shou shu shua shuai shuan shuang shui shun shuo si song sou su suan sui sun suo ta tai tan tang tao te teng ti tian tiao tie ting tong tou tu tuan tui tun tuo wa wai wan wang wei wen weng wo wu xi xia xian xiang xiao xie xin xing xiong xiu xu xuan xue xun ya yan yang yao ye yi yin ying yo yong you yu yuan yue yun za zai zan zang zao ze zei zen zeng zha zhai zhan zhang zhao zhe zhen zheng zhi zhong zhou zhu zhua zhuai zhuan zhuang zhui zhun zhuo zi zong zou zu zuan zui zun zuo };

  /**//// <summary>
    /// 包含要排除處理的字符的字符串數組
    /// </summary>
    private static string[] bd = new string[] { };


    private static Hashtable _Phrase;

  /**//// <summary>
    /// 設置或獲取包含列外詞組讀音的鍵/值對的組合
    /// </summary>
    public static Hashtable Phrase
    {
    get
    {
    if (_Phrase == null)
    {
    _Phrase = new Hashtable();

  _PhraseAdd(重慶 Chong Qing);
    _PhraseAdd(深圳 Shen Zhen);
    _PhraseAdd(什麼 Shen Me);
    }

  return _Phrase;
    }

  set { _Phrase = value; }
    }

  /**//// <summary>
    /// 將指定中文字符串轉換為拼音形式
    /// </summary>
    /// <param name=chs>要轉換的中文字符串</param>
    /// <param name=separator>連接拼音之間的分隔符</param>
    /// <param name=initialCap>指定是否將首字母大寫</param>
    /// <returns>包含中文字符串的拼音的字符串</returns>
    public static string Convert(string chs string separator bool initialCap)
    {
    if (chs == null || chsLength == ) return ;
    if (separator == null || separatorLength == ) separator = ;

  // 例外詞組
    foreach (DictionaryEntry de in CHSPinYinPhrase)
    {
    chs = chsReplace(deKeyToString() StringFormat( {} deValueToString()Replace( separator)));
    }

  byte[] array = new byte[];
    string returnstr = ;
    int chrasc = ;
    int i = ;
    int i = ;
    bool b = false;
    char[] nowchar = chsToCharArray();

  CultureInfo ci = ThreadCurrentThreadCurrentCulture;
    TextInfo ti = ciTextInfo;

  for (int j = ; j < nowcharLength; j++)
    {
    array = EncodingDefaultGetBytes(nowchar[j]ToString());
    string s = nowchar[j]ToString(); ;

  if (arrayLength == )
    {
    b = true;
    returnstr += s;
    }
    else
    {
    if (s == )
    {
    if (returnstr == || b == true) returnstr += s;
    else returnstr += separator + s;

  continue;
    }

  i = (short)(array[]);
    i = (short)(array[]);

  chrasc = i * + i ;

  for (int i = (pvLength ); i >= ; i)
    {
    if (pv <= chrasc)
    {
    s = ps;

  if (initialCap == true) s = tiToTitleCase(s);

  if (returnstr == || b == true) returnstr += s;
    else returnstr += separator + s;

  break;
    }
    }

  b = false;
    }
    }

  returnstr = returnstrReplace( separator);
    return returnstr;
    }

  /**//// <summary>
    /// 將指定中文字符串轉換為拼音形式
    /// </summary>
    /// <param name=chs>要轉換的中文字符串</param>
    /// <param name=separator>連接拼音之間的分隔符</param>
    /// <returns>包含中文字符串的拼音的字符串</returns>
    public static string Convert(string chs string separator)
    {
    return CHSPinYinConvert(chs separator false);
    }

  /**//// <summary>
    /// 將指定中文字符串轉換為拼音形式
    /// </summary>
    /// <param name=chs>要轉換的中文字符串</param>
    /// <param name=initialCap>指定是否將首字母大寫</param>
    /// <returns>包含中文字符串的拼音的字符串</returns>
    public static string Convert(string chs bool initialCap)
    {
    return CHSPinYinConvert(chs initialCap);
    }

  /**//// <summary>
    /// 將指定中文字符串轉換為拼音形式
    /// </summary>
    /// <param name=chs>要轉換的中文字符串</param>
    /// <returns>包含中文字符串的拼音的字符串</returns>
    public static string Convert(string chs)
    {
    return CHSPinYinConvert(chs );
    }
    }


From:http://tw.wingwit.com/Article/program/net/201311/13608.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.