拼音检索

简介:

当我们遇到要处理汉字和拼音之间的转化关系怎么办?如和用程序来实现?

我搜索到一个ChineseChar开发包,然后实现了这一难题

using System;
using Microsoft.International.Converters.PinYinConverter;

namespace 拼音基础
{
    class Program
    {
        static void Main(string[] args)
        {

            #region 判断是否为同音字
            ChineseChar chineseChar = new ChineseChar('微');
            Console.WriteLine("Stroke number of 微 in Chinese is {0}.", chineseChar.StrokeNumber);
            Console.WriteLine("{0} characters' pinyin is \"wei1\".", ChineseChar.GetHomophoneCount("wei1"));
            if (ChineseChar.IsHomophone('微', '薇'))
            {
                Console.WriteLine("微 and 薇 have the same pinyin.");
            }
            else
            {
                Console.WriteLine("微 and 薇 have different pinyins.");
            } 
            #endregion
            ChineseChar char1 = new ChineseChar('单');
            bool f = ChineseChar.IsHomophone('杨','洋');
            Console.Write("杨和洋是否为同音字"+f);
            Console.Write("\n单是否为多音字:"+char1.IsPolyphone);
            char[] chars = ChineseChar.GetChars("ji3");//要加上声调
            foreach (char c in chars)
            {
                Console.Write(c + " ");
            }

            for (int i = 0; i < char1.PinyinCount; i++)
            {
                string s=char1.Pinyins[i];
                Console.WriteLine(s);
            }
            
            
            //判断是否是一个拼音字符串
            Console.WriteLine("de是否是一个合法的拼音"+ChineseChar.IsValidPinyin("de1"));//1,2,3,4表示声调

            #region 输入一段中文,写出拼音
            string str = Console.ReadLine();
            foreach (char c in str)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar cc = new ChineseChar(c);
                    Console.Write(cc.Pinyins[0] + " "); 
                }
                else
                {
                    Console.Write(c);
                }
            } 
            #endregion

            Console.Read();
        }
    }
}

 




















本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366791,如需转载请自行联系原作者


相关文章
|
C#
C#中汉字排序简单示例(拼音/笔划)
可以按照区域语言修改排序规则。 class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)", "錢(QIAN)", "孫(SU...
2437 0
使用微软PinYinConverter查询汉字拼音
原文:使用微软PinYinConverter查询汉字拼音 通过汉字,如何查询拼音? 微软有相应的DLL可直接使用 引用方式 Nuget包管理安装 DLL下载后,引用 可以从微软的网站上下载相关文字处理的类库,下载地址如下: http://download.
1548 0
|
人工智能 关系型数据库 MySQL
一个简单的汉字搜索匹配示例(支持拼音、首字母简写)
在社交应用中,很多场景下需要用到搜索,以微信的搜索通讯录为例。好友自己有昵称,我们可能给他/她备注一个昵称,在输入:拼间、首字母、原文时都应该能匹配到(匹配优先是备注然后才是原来的昵称)。这里以‘芈月传’为例,‘芈’不是常见字,所以输入:myz或者miyuezhuan或者芈月传,都应该能匹配到‘芈月传’这个结果。
1807 0