当前位置: 移动技术网 > IT编程>开发语言>c# > C#汉字转拼音实例(支持多音字)

C#汉字转拼音实例(支持多音字)

2019年07月18日  | 移动技术网IT编程  | 我要评论

之前由于项目需要,中间需要一个汉字转拼音和首拼的功能来做查询,感觉这种功能基本已经成熟化了,于是查找了相关的代码,首先引入眼帘的是下面两篇文章

1.c# 汉字转拼音(支持gb2312字符集中所有汉字)

2.javascript实现汉字和拼音互转的终极方案

写的比较全也很详细,都有提供源码,大家可以参考下。

由于考虑到接口的需要,于是参考了 第一篇,文章中作者的源码基本能满足汉字转拼音的需要,对于其他特殊的字符,也可以在进行添加补充,不足之处就是不支持多音字,由于需要支持多音字的查询,所以后面有查了下其他的文章,发现还没有现成的文章(也可能本人的搜索水平比较水)。后来查找发现对于汉字转拼音,原来微软已经提供了 microsoft visual studio international pack ,而且很强大。于是试了一下

首先在nuget引用对应的包

查找 pinyinconverter

简单的demo

小试一下,使用也非常简单,只要直接使用chinesechar类进行装换就好

string ch = console.readline();
chinesechar cc = new chinesechar(ch[0]);
var pinyins = cc.pinyins.tolist();
pinyins.foreach(console.writeline);

结果如下:

我们可以看到, 行 的多音字有 hang,heng,xing 三个,这里连音标也出来了,确实很方便。而我需要的功能是输入 银行 ,然后转换为拼音是 yinhang,yinheng,yinxing,  首拼是 yh,yx。有chinesechar 这个类的话做起来思路就简单了。

汉字转拼音类封装

1.首先对输入的汉字进行拆分

2.接着每个汉字用chinesechar 获取多个拼音

3.然后除去数字,去重,提取首字符,再在进行组合就好了

于是写了个帮助类进行装换,代码如下:

  public class pinyinconverterhelp
  {
    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;
    }
  }

  public class pingyinmodel
  {
    public pingyinmodel()
    {
      totalpingyin = new list<string>();
      firstpingyin = new list<string>();
    }

    //全拼
    public list<string> totalpingyin { get; set; }

    //首拼
    public list<string> firstpingyin { get; set; }
  }

调用方式:

 console.writeline("请输入中文:");
 string str = console.readline();
var strs = pinyinconverterhelp.gettotalpingyin(str).totalpingyin;
var frists = pinyinconverterhelp.gettotalpingyin(str).firstpingyin;
 console.writeline("全拼音:" + string.join(",", strs));
 console.writeline("首音:" + string.join(",", frists));
console.writeline();

结果:

 

目前试过一些生僻字都是能支持,对于一些太偏的还没试过,不过对于一般汉字转拼音的,多音字支持这里就已经足够了。

这里仅仅是使用了 microsoft visual studio international pack 这个扩展包里面的汉字转拼音功能,其实里面还有中文、日文、韩文、英语等各国语言包,并提供方法实现互转、获、获取字数、甚至获取笔画数等等强大的功能,有兴趣的朋友可以自行查询下它的api。

 源码分享

分享是一种美德,有时候牛逼的文章可以提高我们的技术层面,但有时候更多的需求是业务层面,很多小知识应用的分享却可以帮我们提高业务层面的问题。只要分享的知识点有用,不误人子弟,哪怕大小都是一种学习,所以也希望大家能勇于分享。

 最后,源码分享出来给大家,如果有错误和不足的地方,也希望指正

地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网