posts - 30,  comments - 14,  trackbacks - 0

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

 下载地址: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 2017-05-04 15:08 £冷☆月№ 阅读(...) 评论(...) 编辑 收藏