美文网首页
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>();
    }
}

相关文章

  • 一些链接

    2018-01-23 开始记录一些链接,以免丢失 1、汉字转拼音,主要用于排序查找 C# 汉字转拼音(支持GB23...

  • Python 拼音汉字互转

    汉字转拼音:pypinyin拼音转汉字:Pinyin2Hanzi Python汉字转拼音-拼音转汉字的效率测评

  • C#汉字转拼音

  • python汉字转拼音

    场景说明 把中文汉字、转成汉语拼音,包括: 纯汉字转拼音 汉字里面加有字母转拼音 转加声调的拼音 转用数字表示声调...

  • 发布 | 汉字转拼音工具

    通过查找汉字拼音库实现实时汉字转拼音的功能。 实现 加载汉字拼音对照文件pinyin.txt(4万+汉字拼音对照)...

  • 发布 | 汉字转拼音工具

    通过查找汉字拼音库实现实时汉字转拼音的功能。 实现 加载汉字拼音对照文件pinyin.txt(4万+汉字拼音对照)...

  • 汉字转拼音

    汉字转拼音有很多种方法, 在这里推荐一个在线的免费转拼音的网站。 点击访问??汉字转拼音??

  • 汉字转拼音

    几种方法 一种是建立一个拼音对应的汉字map,进行查表。一种是利用汉字编码,根据不同的区域求出拼音。 第一种的原...

  • 汉字转拼音

    不过要注意的是,有一些汉字为多音字,可能不太符合需求 具体的请参考这篇文章

  • 汉字转拼音

    CFStringTransform iOS在CoreFoundation中提供了CFStringTransform...

网友评论

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

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