本文介绍了任何图书馆数字拼音转换成汉语拼音的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道如果任何人知道的一个类库,可以转换中国拼音的人与色调,如nin2 HAO3 mA至NINhǎo马。这将是类似于这个答案,但希望使用.NET框架。

Just wondering if anyone knows of a class library that can convert Chinese Pinyin to ones with tones, such as nin2 hao3 ma to nín hǎo ma. It would be similar to this answer, but hopefully using the .NET framework.

推荐答案

下面是我的@格雷格 - Hewgill python算法为C#。我还没有遇到任何问题至今。

Here is my porting of @Greg-Hewgill python algorithm to C#. I haven't run into any issues so far.

public static string ConvertNumericalPinYinToAccented(string input)
    {
        Dictionary<int, string> PinyinToneMark = new Dictionary<int, string>
        {
            {0, "aoeiuv\u00fc"},
            {1, "\u0101\u014d\u0113\u012b\u016b\u01d6\u01d6"},
            {2, "\u00e1\u00f3\u00e9\u00ed\u00fa\u01d8\u01d8"},
            {3, "\u01ce\u01d2\u011b\u01d0\u01d4\u01da\u01da"},
            {4, "\u00e0\u00f2\u00e8\u00ec\u00f9\u01dc\u01dc"}
        };

        string[] words = input.Split(' ');
        string accented = "";
        string t = "";
        foreach (string pinyin in words)
        {
            foreach (char c in pinyin)
            {
                if (c >= 'a' && c <= 'z')
                {
                    t += c;
                }
                else if (c == ':')
                {
                    if (t[t.Length - 1] == 'u')
                    {
                        t = t.Substring(0, t.Length - 2) + "\u00fc";
                    }
                }
                else
                {
                    if (c >= '0' && c <= '5')
                    {
                        int tone = (int)Char.GetNumericValue(c) % 5;

                        if (tone != 0)
                        {
                            Match match = Regex.Match(t, "[aoeiuv\u00fc]+");
                            if (!match.Success)
                            {
                                t += c;
                            }
                            else if (match.Groups[0].Length == 1)
                            {
                                t = t.Substring(0, match.Groups[0].Index) +
                                    PinyinToneMark[tone][PinyinToneMark[0].IndexOf(match.Groups[0].Value[0])]
                                    + t.Substring(match.Groups[0].Index + match.Groups[0].Length);
                            }
                            else
                            {
                                if (t.Contains("a"))
                                {
                                    t = t.Replace("a", PinyinToneMark[tone][0].ToString());
                                }
                                else if (t.Contains("o"))
                                {
                                    t = t.Replace("o", PinyinToneMark[tone][1].ToString());
                                }
                                else if (t.Contains("e"))
                                {
                                    t = t.Replace("e", PinyinToneMark[tone][2].ToString());
                                }
                                else if (t.Contains("ui"))
                                {
                                    t = t.Replace("i", PinyinToneMark[tone][3].ToString());
                                }
                                else if (t.Contains("iu"))
                                {
                                    t = t.Replace("u", PinyinToneMark[tone][4].ToString());
                                }
                                else
                                {
                                    t += "!";
                                }
                            }
                        }
                    }
                    accented += t;
                    t = "";
                }
            }
            accented += t + " ";
        }
        accented = accented.TrimEnd();
        return accented;
    }

这篇关于任何图书馆数字拼音转换成汉语拼音的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:04