예제 #1
0
 /// <summary>
 /// 尝试将指定的中文汉字字符转换为 <see cref="ChineseChar"/> 对象,并返回一个指示是否转换成功的值。
 /// <para>如果传入的字符 <paramref name="ch"/> 不在简体中文扩展字符集中时,则将转换失败。</para>
 /// <para>如果该方法返回一个表示转换失败的布尔值 false,则输出参数 <paramref name="result"/> 的值将会被设置为 null。</para>
 /// </summary>
 /// <param name="ch"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryParse(char ch, out ChineseChar result)
 {
     lock (_chars)
     {
         if (_chars.TryGetValue(ch, out result))
         {
             return(true);
         }
         else
         {
             ChineseCharUnit charUnit;
             if (ChineseResources.TryGetCharUnit(ch, out charUnit))
             {
                 result = new ChineseChar(ch, charUnit);
                 _chars.Add(ch, result);
                 return(true);
             }
             else
             {
                 result = null;
                 return(false);
             }
         }
     }
 }
예제 #2
0
        internal static bool IsValidPinYin(string pinyin, bool validateTone, out int pinyinIndex, out Tone tone)
        {
            pinyin = pinyin != null?pinyin.Trim() : null;

            if (string.IsNullOrEmpty(pinyin))
            {
                pinyinIndex = -1;
                tone        = Tone.Undefined;
                return(false);
            }
            char c = pinyin[pinyin.Length - 1];

            tone = ConvertTone(c);

            if (tone == Tone.Undefined)
            {
                if (validateTone)
                {
                    pinyinIndex = -1;
                    return(false);
                }
                else
                {
                    return(ChineseResources.TryGetPinYinIndex(pinyin + '1', out pinyinIndex) ||
                           ChineseResources.TryGetPinYinIndex(pinyin + '2', out pinyinIndex) ||
                           ChineseResources.TryGetPinYinIndex(pinyin + '3', out pinyinIndex) ||
                           ChineseResources.TryGetPinYinIndex(pinyin + '4', out pinyinIndex) ||
                           ChineseResources.TryGetPinYinIndex(pinyin + '5', out pinyinIndex));
                }
            }
            else
            {
                return(ChineseResources.TryGetPinYinIndex(pinyin, out pinyinIndex));
            }
        }
예제 #3
0
        /// <summary>
        /// 检索指定字符的笔画数。
        /// </summary>
        /// <param name="ch">指出需要识别的字符。</param>
        /// <returns>返回指定字符的笔画数。如果字符不是有效值则返回 -1。</returns>
        public static short GetStrokeNumber(char ch)
        {
            ChineseCharUnit charUnit;

            return(ChineseResources.TryGetCharUnit(ch, out charUnit)
                ? charUnit.StrokeNumber
                : (short)-1);
        }
예제 #4
0
        /// <summary>
        /// 将给出的字符和实例字符的笔画数进行比较。
        /// </summary>
        /// <param name="ch">显示给出的字符。</param>
        /// <returns>说明比较操作的结果。如果给出字符和实例字符的笔画数相同,返回值为 0。如果实例字符比给出字符的笔画多,则返回值大于 0。 如果实例字符比给出字符的笔画少,则返回值小于 0。</returns>
        /// <exception cref="ArgumentException">传入的字符 <paramref name="ch"/> 不是一个汉字字符时,抛出该异常。</exception>
        public int CompareStrokeNumber(char ch)
        {
            ChineseCharUnit charUnit;

            if (!ChineseResources.TryGetCharUnit(ch, out charUnit))
            {
                throw new ArgumentException(string.Format("传入的字符 {0} 不是一个汉字字符。", ch));
            }
            return(this.StrokeNumber - charUnit.StrokeNumber);
        }
예제 #5
0
        /// <summary>
        /// 获取与当前 <see cref="PinYin"/> 对象的 <see cref="QuanPin"/> 全拼属性相同的拼音和给定声调的所有同音字。
        /// <para>如果参数 <paramref name="tone"/> 的值为 <see cref="PinYins.Tone.Undefined"/>,则该方法将返回同全拼情况下所有音调的汉字(相当于方法 <see cref="GetCharsWithNonTone"/>)。</para>
        /// </summary>
        /// <param name="tone"></param>
        /// <returns></returns>
        public ReadOnlyCollection <char> GetChars(Tone tone)
        {
            if (tone == PinYins.Tone.Undefined)
            {
                return(this.GetCharsWithNonTone());
            }

            string pinyin = this.QuanPin + (int)tone;
            int    index;

            if (ChineseResources.TryGetPinYinIndex(pinyin, out index))
            {
                return(ChineseResources.PinYinCharDictionary[index]);
            }
            else
            {
                return(null);
            }
        }