C # Chinese Pinyin instance (support pronunciation) _c# tutorial

Source: Internet
Author: User

As a result of the project needs, the middle of the need for a Chinese character to pinyin and the first spell of the function to do the query, feeling that this function has been basically mature, and then find the relevant code, the first to introduce the eye is the following two articles

1.c# Chinese Pinyin (support for all Chinese characters in GB2312 character set)

The ultimate scheme of 2.Javascript to realize the reciprocal conversion between Chinese characters and pinyin

Write a comparison of the whole is also very detailed, have provided the source code, we can refer to the next.

Because of the need to interface, so reference to the first article, the author's source code can basically meet the needs of Chinese pinyin, for other special characters, can also be added to add, the disadvantage is not support pronunciation, because of the need to support pronunciation query, so the following check the other articles, Found that there is no ready-made article (also may be my search level compared to water). Later find found that for Chinese pinyin, Microsoft has already provided Microsoft Visual Studio International Pack, and is very powerful. So I tried it.

First, the corresponding package is referenced in the NuGet

Find Pinyinconverter

A simple Demo

Small try, the use is also very simple, as long as the direct use of Chinesechar class to carry on a good

string ch = console.readline ();
Chinesechar cc = new Chinesechar (ch[0]);
var pinyins = cc. Pinyins.tolist ();
Pinyins. ForEach (Console.WriteLine);

The results are as follows:

We can see that the line of pronunciation has hang,heng,xing three, where the legato is also out, it is really convenient. And I need the function is input bank, then convert to Pinyin is yinhang,yinheng,yinxing, the first spell is Yh,yx. There are chinesechar this kind of words to do up the idea is simple.

Chinese Pinyin class encapsulation

1. First, the input of the Chinese characters are split

2. Then each Chinese character uses Chinesechar to obtain multiple pinyin

3. Then remove the number, go to heavy, extract the first character, and then the combination of the good

So I wrote a help class to replace the code as follows:

  public class Pinyinconverterhelp {public static Pingyinmodel Gettotalpingyin (String str) {var CHS = s Tr.
      ToCharArray (); Record the whole spelling dictionary<int of each Chinese character, 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];
          is a valid kanji if (Chinesechar.isvalidchar (ch)) {Chinesechar cc = new Chinesechar (CH); Pinyins = cc. Pinyins.where (P =>!string. Isnullorwhitespace (P)).
        ToList (); else {pinyins. ADD (Ch.
        ToString ()); //Remove tone, turn lowercase pinyins = pinyins. ConvertAll (P => regex.replace (P, @ "\d", "").
        ToLower ()); Go heavy 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 {//full spell cycle matching 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;
          First letter loop matching 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> ();

    }//Full spelling public list<string> Totalpingyin {get; set;}
  First spell public list<string> Firstpingyin {get; set;}

 }

Call Mode:

 Console.WriteLine ("Please enter Chinese:");
 String str = Console.ReadLine ();
var STRs = Pinyinconverterhelp.gettotalpingyin (str). Totalpingyin;
var frists = Pinyinconverterhelp.gettotalpingyin (str). Firstpingyin;
 Console.WriteLine ("Full pinyin:" + string.join (",", STRs));
 Console.WriteLine ("First tone:" + string.join (",", frists));
Console.WriteLine ();

Results:

At present tried some rare words are able to support, for some too biased has not tried, but for the general Chinese Pinyin, pronunciation support here is enough.

Here is only the use of the Microsoft Visual Studio International Pack in the expansion package of the Chinese pinyin function, in fact, there are Chinese, Japanese, Korean, English and other countries language packs, and provide methods to achieve mutual transfer, access, get word, Even get the number of strokes and other powerful features, interested friends can query their own API.

Source Sharing

Sharing is a virtue, sometimes a good article can improve our technical level, but sometimes more demand is the business level, a lot of small knowledge application sharing can help us improve the business level of the problem. As long as the sharing of knowledge points useful, not fraught, even if the size is a kind of learning, so also hope that we can share the courage.

Finally, the source to share out to everyone, if there are errors and deficiencies, but also want to correct

Address: Demo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.