汉字转拼音/首字母

现在主流的转换有三种:hash匹配,Npinyin,微软PinYinConverter

这边是优先使用Npinyin  翻译失败的使用微软PinYinConverter

经测试大部分生僻字翻译都OK,多音字还是有概率分辨不对(也可能是因为我用的DLL并不是最新版),只能怪我中华文化实在博大精深了,蛤蛤

 

还有数字和字母的互相转换也一并放进去了~

数字转换是1-26转换为小写a-z,字母转换是大写A-Z和小写a-z转换为1-26,原理就是ASCII码的一些计算

代码如下:

using Microsoft.International.Converters.PinYinConverter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebUtility
{
    public class PingYinHelper
    {
        private static Encoding gb2312 = Encoding.GetEncoding("GB2312");

        /// <summary>
        /// 汉字转全拼
        /// </summary>
        /// <param name="strChinese"></param>
        /// <returns></returns>
        public static string ConvertToAllSpell(string strChinese)
        {
            try
            {
                if (strChinese.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < strChinese.Length; i++)
                    {
                        var chr = strChinese[i];
                        fullSpell.Append(GetSpell(chr));
                    }
                    return fullSpell.ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("全拼转化出错!" + e.Message);
            }
            return string.Empty;
        }

        /// <summary>
        /// 汉字转首字母
        /// </summary>
        /// <param name="strChinese"></param>
        /// <returns></returns>
        public static string ConvertToFirstSpell(string strChinese)
        {
            try
            {
                if (strChinese.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < strChinese.Length; i++)
                    {
                        var chr = strChinese[i];
                        fullSpell.Append(GetSpell(chr)[0]);
                    }
                    return fullSpell.ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("首字母转化出错!" + e.Message);
            }
            return string.Empty;
        }

        private static string GetSpell(char chr)
        {
            var coverchr = NPinyin.Pinyin.GetPinyin(chr);
            bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
            if (isChineses)
            {
                ChineseChar chineseChar = new ChineseChar(coverchr[0]);
                foreach (string value in chineseChar.Pinyins)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        return value.Remove(value.Length - 1, 1);
                    }
                }
            }
            return coverchr;
        }

        /// <summary>
        /// 数字转换成字母(1-26)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string NumToChar(int number)
        {
            number = number + 64;
            if (65 <= number && 90 >= number)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] btNumber = new byte[] { (byte)number };
                return asciiEncoding.GetString(btNumber).ToLower();
            }
            return "数字不在转换范围内";
        }

        /// <summary>
        /// 字母转换成数字(1-26)
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CharToNum(char chr)
        {
            byte[] array = new byte[1]; 
            array = System.Text.Encoding.ASCII.GetBytes(chr.ToString().ToUpper());
            int asciicode = (short)(array[0]) - 64; 
            return Convert.ToString(asciicode); 
        }
    }
}

最后附上这边用上的两个DLL文件,并不是最新版本

https://files.cnblogs.com/files/cn2018/ConvertToPinYin.zip

 

备用下载地址:

NPINYIN,不能识别的字很少,而且还在不断维护更新,日趋完美。

在googlecode可以看到它的开源项目,http://code.google.com/p/npinyin/

下载地址

dll:https://files.cnblogs.com/files/guohu/NPinyin-0.2.4588.20158-bin.zip

源码:https://files.cnblogs.com/files/guohu/NPinyin-0.2.x-source_code.zip

 

Microsoft.International.Converters.PinYinConverter

 

原文链接:http://outofmemory.cn/code-snippet/4392/ms-CHSPinYinConv-convert-hanzi-to-pinyin

 

微软为中文,日文以及韩文提供了额外的支持,我们可以从微软的网站上下载相关文字处理的类库,下载地址如下:

 

http://download.microsoft.com/download/5/7/3/57345088-ACF8-4E9B-A9A7-EBA35452DEF2/vsintlpack1.zip

 

下载的是一个zip包,里面有多个安装文件,我们只安装“CHSPinYinConv.msi”就可以了,安装之后在安装目录下会有“ChnCharInfo.dll”文件,这个文件可以做汉字相关的处理,不止有文字,还有笔画读音等汉字相关的信息。

 

posted @ 2018-07-19 15:45  AdolphChen  阅读(2980)  评论(0编辑  收藏  举报