随笔 - 83,  文章 - 6,  评论 - 20,  阅读 - 10万

 微软官方的语言包实现汉字转拼音。

 下载地址:Microsoft Visual Studio International Pack 1.0 SR1

选择安装CHSPinYinConv.msi。完成之后在项目中引用安装目录中的ChnCharInfo.dll即可。

使用命名空间:using Microsoft.International.Converters.PinYinConverter;

自己根据网上的自己组合了一个类:CHSPinYinConv.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.International.Converters.PinYinConverter;

/// <summary>
/// CHSPinYinConv 的摘要说明
/// </summary>
public static class CHSPinYinConv
{
    public class PingYinModel
    {
        public List<string> TotalPingYin = new List<string>();
        public List<string> FirstPingYin = new List<string>();
    }
    //获取汉字全拼(包含多音字)
    public static PingYinModel GetTotalPingYin(string str)
    {
        var chs = str.ToCharArray();
        //记录每个汉字的全拼
        Dictionary<int, List<string>> totalPingYins = new Dictionary<int, List<string>>();
        for (int i = 0; i < chs.Length; i++)
        {
            var pinyins = new List<string>();
            var ch = chs[i];
            //是否是有效的汉字
            if (ChineseChar.IsValidChar(ch))
            {
                ChineseChar cc = new ChineseChar(ch);
                pinyins = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
            }
            else
            {
                pinyins.Add(ch.ToString());
            }

            //去除声调,转小写
            pinyins = pinyins.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower());
            //去重
            pinyins = pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList();
            if (pinyins.Any())
            {
                totalPingYins[i] = pinyins;
            }
        }
        PingYinModel result = new PingYinModel();
        foreach (var pinyins in totalPingYins)
        {
            var items = pinyins.Value;
            if (result.TotalPingYin.Count <= 0)
            {
                result.TotalPingYin = items;
                result.FirstPingYin = items.ConvertAll(p => p.Substring(0, 1)).Distinct().ToList();
            }
            else
            {
                //全拼循环匹配
                var newTotalPingYins = new List<string>();
                foreach (var totalPingYin in result.TotalPingYin)
                {
                    newTotalPingYins.AddRange(items.Select(item => totalPingYin + item));
                }
                newTotalPingYins = newTotalPingYins.Distinct().ToList();
                result.TotalPingYin = newTotalPingYins;

                //首字母循环匹配
                var newFirstPingYins = new List<string>();
                foreach (var firstPingYin in result.FirstPingYin)
                {
                    newFirstPingYins.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1)));
                }
                newFirstPingYins = newFirstPingYins.Distinct().ToList();
                result.FirstPingYin = newFirstPingYins;
            }
        }
        return result;
    }

    /// <summary>
    /// 汉字转化为拼音
    /// </summary>
    /// <param name="str">汉字</param>
    /// <returns>全拼</returns>
    public static string GetPinyin(string str)
    {
        string r = string.Empty;
        foreach (char obj in str)
        {
            try
            {
                ChineseChar chineseChar = new ChineseChar(obj);
                string t = chineseChar.Pinyins[0].ToString();
                r += t.Substring(0, t.Length - 1);
            }
            catch
            {
                r += obj.ToString();
            }
        }
        return r.ToLower();
    }

    /// <summary>
    /// 汉字转化为拼音首字母
    /// </summary>
    /// <param name="str">汉字</param>
    /// <returns>首字母</returns>
    public static string GetFirstPinyin(string str)
    {
        string r = string.Empty;
        foreach (char obj in str)
        {
            try
            {
                ChineseChar chineseChar = new ChineseChar(obj);
                string t = chineseChar.Pinyins[0].ToString();
                r += t.Substring(0, 1);
            }
            catch
            {
                r += obj.ToString();
            }
        }
        return r.ToLower();
    }
}
复制代码

 以下是我的一个小Demo:https://pan.baidu.com/s/1mhKtqiO

posted on   £冷☆月№  阅读(321)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示