根据汉字得到拼音的类库(三)

        /// <summary>

        /// 存放排好序的汉字-拼音对照字典,格式如下

        /// han

        /// zi

        /// 既汉字作为字典的Key,拼音作为value

        /// </summary>

        private SortedDictionary<Char, string> mSortDiction = new SortedDictionary<Char, string>() ;

      

        private static SpellLib _Instance ;

 

        /// <summary>

        /// 取汉字拼音类的实例

        /// </summary>

        /// <returns></returns>

        public static SpellLib Instance()

        {

            if (_Instance == null)

                _Instance = new SpellLib();

            return _Instance;

        }

 

        /// <summary>

        /// 构造函数

        /// </summary>

        private SpellLib()

        {

            BuildSortDiction();

        }

 

        #region 提供的方法

        /// <summary>

        /// 把给定的字转化为拼音

        /// </summary>

        /// <param name="word">要转化的字</param>

        /// <returns>字的拼音</returns>

        /// <example>

        /// string strCode = SpellLib.Instance().GetCodesByWord('');

        /// </example>

        /// <remarks>如果该字不存在拼音,就返回该字本身</remarks>

        public string GetCodeByWord(Char word)

        {

            if (mSortDiction.ContainsKey(word))

                return mSortDiction[word];

            else

                return word.ToString();

        }

        /// <summary>

        /// 取得字符串第一个字的拼音

        /// </summary>

        /// <param name="word">要转化的字符串</param>

        /// <returns>字的拼音</returns>

        /// <example>

        /// string strCode = SpellLib.Instance().GetCodesByWord("汉字");

        /// </example>

        /// <remarks>如果第一个字不存在拼音,就返回该字本身</remarks>

        public string GetCodeByWord(string word)

        {

            if (word == null)

                throw new ArgumentNullException("word");

 

            if (word.Length > 0)

                return GetCodeByWord(word[0]);

            else

                return null;             

        }

        /// <summary>

        /// 取得一个字符串中所有字的拼音

        /// </summary>

        /// <param name="sentence"></param>

        /// <returns></returns>

        public string GetCodesByWord(string sentence)

        {

            if (sentence == null)

                throw new ArgumentNullException("sentence");

 

            bool blAddSpace = false;

             StringBuilder sbResult = new StringBuilder();

 

            foreach (Char chr in sentence)

            {

                string strCodes = GetCodeByWord(chr) ;

 

                if (strCodes.Length > 1)

                {

                    if (blAddSpace == false)

                    {

                        sbResult.Append(" ");                       

                    }

                    sbResult.Append(strCodes);

                    sbResult.Append(" ");

                    blAddSpace = true;

                }

                else

                {

                    sbResult.Append(strCodes);

                    blAddSpace = false;

                }

            }

            return sbResult.ToString();        

        }

        /// <summary>

        /// 构造排序字典对象

        /// </summary>

        private void BuildSortDiction()

        {

           // mSortDiction.

            for (int i = 0; i < this.WordList.Length; i++)

            {

                string value = this.CodeList[i];

                string strWord = WordList[i];

 

                foreach (char key in strWord)

                {

                    if (mSortDiction.ContainsKey(key))

                    {                       

                        continue;

                    }

                    else

                    {

                        mSortDiction.Add(key, value);

                    }

                }//end foreach

 

            }//end for

        }

        /// <summary>

        /// 取得给定拼音的声母

        /// </summary>

        /// <param name="spell">拼音</param>

        /// <returns>声母</returns>

        /// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>

        public static string GetVoicePart(string spell)

        {

            string strVoicePart = null;

            if (spell.Length == 1)

            {

                strVoicePart = spell;

            }

            else

            {

 

                char firstChr = spell[0];

                char SecondChr = spell[1];

                strVoicePart = firstChr.ToString();

                if (SecondChr.Equals('h') || SecondChr.Equals('H'))

                    strVoicePart += "h";

 

                switch (strVoicePart.ToLower(System.Globalization.CultureInfo.CurrentCulture))

                {

                    case "b":

                    case "p":

                    case "m":

                    case "f":

                    case "d":

                    case "t":

                    case "n":

                    case "l":

                    case "g":

                    case "k":

                    case "h":

                    case "j":

                    case "q":

                    case "x":

                    case "r":

                    case "z":

                    case "c":

                    case "s":

                    case "zh":

                    case "ch":

                    case "sh":

                        break;

                    default:

                        strVoicePart = firstChr.ToString();

                        break;

                }//end switch

            }

            return strVoicePart;

        }

        /// <summary>

        /// 取得给定汉字的声母部分

        /// </summary>

        /// <param name="word"></param>

        /// <returns></returns>

        /// <example>

        /// string strVoicePart = SpellLib.Instance().GetCodeOfVoicePart("");

        /// </example>

        /// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>

        public string GetCodeOfVoicePart(string word)

        {

           return GetVoicePart(this.GetCodeByWord(word));

        }

        /// <summary>

        /// 取得给定汉字的声母部分

        /// </summary>

        /// <param name="word"></param>

        /// <returns></returns>

        /// <remarks>如果不存在声母就返回第一个字母,如啊返回a,嗯返回e</remarks>

        public string GetCodeOfVoicePart(Char word)

        {

            return GetVoicePart(this.GetCodeByWord(word));

        }

        #endregion

 

    }//end class

   

}//end namespace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值