C# 汉字转拼音(比原NPinyin好用)

VS Nuget 上 搜索 NPinyin, 会有好几个选择

 

 

 

经过测试

NPinyin 等都不好用, 好些汉字或生僻字 无法转拼音, 比如 "洺","玥" 等等,

 

 只有 NPinyinPro 可以!   

不过也有个问题, 就是对多音字的支持不是很好,比如"重庆","重阳" .

微软的 PinYinConverter 可以获取汉字的多个拼音, 要注意的是列表是按字母排序.

 测试代码:

public void PingyinTest() {
    Dictionary<string, Tuple<string, string>> dict = new Dictionary<string, Tuple<string, string>>() {
    {"梅钰", new Tuple<string,string>("meiyu","MY")},
    {"张洺", new Tuple<string,string>("zhangming","ZM")},
    {"王玥", new Tuple<string,string>("wangyue","WY")},
    {"王思琪", new Tuple<string,string>("wangsiqi","WSQ")},
    {"董云强", new Tuple<string,string>("dongyunqiang","DYQ")},
    {"宋红培", new Tuple<string,string>("songhongpei","SHP")},
    {"石磊", new Tuple<string,string>("shilei","SL")},
    };
    foreach (var keyval in dict) {
        var name = keyval.Key;

        var full = keyval.Value.Item1;
        var first = keyval.Value.Item2;

        var py = NPinyin.Pinyin.GetPinyin(name).Replace(" ", "");
        var pf = NPinyin.Pinyin.GetInitials(name);
        Console.WriteLine($"{name}\t {py} {(py == full ? "==" : "!=")} {full}\t {pf} {(pf == first ? "==" : "!=")} {first}");
    }
}

 

posted @ 2022-12-09 12:36  _Ong  阅读(741)  评论(0编辑  收藏  举报