美文网首页
C#汉字转拼音

C#汉字转拼音

作者: fanyu__ | 来源:发表于2021-02-02 10:32 被阅读0次
    install-package PinYinConverterCore
    
        public static class PinyinHelper
        {
            /// <summary>
            /// 汉字转拼音/汉字转拼音首字母
            /// </summary>
            /// <param name="chinese"></param>
            /// <returns></returns>
            public static PinyinResult Convert(string chinese)
            {
                var result = new PinyinResult() { Chinese = chinese.Replace(" ", "") };
    
                var initials = new string[chinese.Length][]; // 存放首字母
                var pinyins = new string[chinese.Length][]; // 存放拼音
    
                try
                {
                    for (int i = 0; i < result.Chinese.Length; i++)
                    {
                        if (ChineseChar.IsValidChar(result.Chinese[i]))
                        {
                            var cc = new ChineseChar(result.Chinese[i]);
                            if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
                            {
                                pinyins[i] = cc.Pinyins.ToList().FindAll(item => item != null).ConvertAll(item => item.Substring(0, item.Length - 1).ToLower()).Distinct().ToArray();
                                initials[i] = pinyins[i].ToList().ConvertAll(item => item.Substring(0, 1)).ToArray();
                            }
                        }
                        else
                        {
                            pinyins[i] = new [] { result.Chinese[i].ToString() };
                            initials[i] = new[] { result.Chinese[i].ToString() };
                        }
                    }
    
                    // 拼接拼音
                    for (int i = 0; i < pinyins.Length - 1; i++)
                    {
                        var last = result.Pinyin;
                        result.Pinyin = new List<string>();
                        result.Pinyin.AddRange(JoinPinyin(last.Count == 0 ? pinyins[i] : last.ToArray(), pinyins[i + 1]));
                    }
    
                    // 拼接拼音
                    for (int i = 0; i < initials.Length - 1; i++)
                    {
                        var last = result.Initial;
                        result.Initial = new List<string>();
                        result.Initial.AddRange(JoinInitial(last.Count == 0 ? initials[i] : last.ToArray(), initials[i + 1]));
                    }
                    result.Initial = result.Initial.Distinct().ToList();
                    result.Pinyin = result.Pinyin.Distinct().ToList();
                    result.Success = true;
                }
                catch (Exception)
                {
                    result.Success = false;
                }
    
                return result;
            }
    
            /// <summary>
            /// 拼接拼音
            /// </summary>
            /// <param name="words1"></param>
            /// <param name="words2"></param>
            /// <returns></returns>
            private static List<string> JoinPinyin(string[] words1, string[] words2)
            {
                var pinyins = new List<string>();
                if (words1 == null && words2 != null)
                {
                    foreach (var w2 in words2)
                    {
                        pinyins.Add(w2);
                    }
                }
                if (words1 != null && words2 == null)
                {
                    foreach (var w1 in words1)
                    {
                        pinyins.Add(w1);
                    }
                }
                if (words1 != null && words2 != null)
                {
                    foreach (var w1 in words1)
                    {
                        foreach (var w2 in words2)
                        {
                            pinyins.Add(w1 + w2);
                        }
                    }
                }
                return pinyins;
            }
    
            /// <summary>
            /// 拼接首字母
            /// </summary>
            /// <param name="words1"></param>
            /// <param name="words2"></param>
            /// <returns></returns>
            private static List<string> JoinInitial(string[] words1, string[] words2)
            {
                var initials = new List<string>();
                if (words1 == null && words2 != null)
                {
                    foreach (var w2 in words2)
                    {
                        initials.Add(w2);
                    }
                }
                if (words1 != null && words2 == null)
                {
                    foreach (var w1 in words1)
                    {
                        initials.Add(w1);
                    }
                }
                if (words1 != null && words2 != null)
                {
                    foreach (var w1 in words1)
                    {
                        foreach (var w2 in words2)
                        {
                            initials.Add(w1 + w2);
                        }
                    }
                }
                return initials;
            }
        }
    
        public class PinyinResult
        {
            /// <summary>
            /// 转换结果
            /// </summary>
            public bool Success { get; set; }
    
            /// <summary>
            /// 汉字
            /// </summary>
            public string Chinese { get; set; }
    
            /// <summary>
            /// 拼音
            /// </summary>
            public List<string> Pinyin { get; set; } = new List<string>();
    
            /// <summary>
            /// 首字母
            /// </summary>
            public List<string> Initial { get; set; } = new List<string>();
        }
    }
    

    相关文章

      网友评论

          本文标题:C#汉字转拼音

          本文链接:https://www.haomeiwen.com/subject/bioetltx.html