/// <summary>
        /// 获取拼音
        /// </summary>
        /// <param name="source">要获取拼音的文字</param>
        /// <param name="mode">获取模式</param>
        /// <param name="spliter">字之间的分隔符</param>
        /// <returns></returns>
        public static string GetPinYin(string source, PinYinMode mode = PinYinMode.Simple, string spliter = " ")
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return(String.Empty);
            }

            StringBuilder sb = new StringBuilder();

            foreach (var c in source)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    var pinYins = GetPinYinWithTone(c);

                    switch (mode)
                    {
                    case PinYinMode.Simple:
                        sb.Append(UpperFirstChar(RemoveTone(pinYins[0])));
                        break;

                    case PinYinMode.WithTone:
                        sb.Append(UpperFirstChar(pinYins[0]));
                        break;

                    case PinYinMode.WithMultiplePronunciations:
                        sb.Append(string.Join(",", pinYins.Where(x => !string.IsNullOrWhiteSpace(x))
                                              .Select(RemoveTone)
                                              .Select(UpperFirstChar)
                                              .Distinct()));
                        break;

                    case PinYinMode.WithToneAndMultiplePronunciations:
                        sb.Append(string.Join(",", pinYins.Where(x => !string.IsNullOrWhiteSpace(x))
                                              .Select(UpperFirstChar)));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("mode", mode, null);
                    }
                }
                else
                {
                    sb.Append(UpperChar(c));
                }
                sb.Append(spliter);
            }

            return(sb.Remove(sb.Length - 1, 1).ToString());
        }
示例#2
0
        /// <summary>
        /// 获得中文拼音
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static IEnumerable <string> GetChinesePinYin(this string inputString, PinYinMode mode = PinYinMode.NoTone)
        {
            var chineseChars = new object[inputString.Length];

            for (var i = 0; i < inputString.Length; i++)
            {
                char inputChar = inputString[i];
                if (!inputChar.ToString().IsChinese())
                {
                    chineseChars[i] = inputChar;
                    continue;
                }
                chineseChars[i] = new ChineseChar(inputChar);
            }
            IEnumerable <string> result = GetPinYin("", 0, chineseChars.ToList(), mode);

            return(result.Distinct());
        }
示例#3
0
        /// <summary>
        /// 获得拼音
        /// </summary>
        /// <param name="nowPinyin"></param>
        /// <param name="index"></param>
        /// <param name="chineseChars"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        private static IEnumerable <string> GetPinYin(string nowPinyin, int index, IReadOnlyList <object> chineseChars, PinYinMode mode)
        {
            var inputPinYin = new List <string>();

            if (chineseChars.Count <= index)
            {
                inputPinYin.Add(nowPinyin);
            }
            else
            {
                switch (chineseChars[index])
                {
                case char charString:
                    inputPinYin.AddRange(GetPinYin(nowPinyin + charString, index + 1, chineseChars, mode));
                    break;

                case ChineseChar chinese:
                    for (var i = 0; i < chinese.PinyinCount; i++)
                    {
                        string pinyin = chinese.Pinyins[i];
                        switch (mode)
                        {
                        case PinYinMode.Full:
                            pinyin = pinyin.ToLower().FirstUpper();
                            break;

                        case PinYinMode.NoTone:
                            pinyin = pinyin.ToLower().FirstUpper().Substring(0, pinyin.Length - 1);
                            break;

                        case PinYinMode.Abbreviation:
                            pinyin = pinyin[0].ToString();
                            break;
                        }
                        string str = pinyin;
                        if (!string.IsNullOrEmpty(nowPinyin))
                        {
                            str = nowPinyin + str;
                        }
                        inputPinYin.AddRange(GetPinYin(str, index + 1, chineseChars, mode));
                    }
                    break;
                }
            }
            return(inputPinYin);
        }