C # kanji Pinyin (support Polyphone)

Source: Internet
Author: User

Read Catalogue

    • First, the corresponding package is referenced in NuGet
    • A simple Demo
    • Chinese character to Pinyin class package
    • Source Sharing

Before due to the project needs, the middle of the need for a Chinese character to pinyin and the first spelling function to do the query, feel that the basic function has matured, and then found the relevant code, the first to introduce the eye is the following two articles

1.c# kanji to Pinyin (supports all Chinese characters in the GB2312 character set)

2. "Dry" JS version of Chinese characters and pinyin to the ultimate solution, with simple JS Pinyin Input Method

Thank two bloggers, write the comparison is also very detailed, have provided the source code, we can refer to the next.

Due to the needs of the interface, so reference to the first article, the author of the source of the basic can meet the needs of Chinese characters to pinyin, for other special characters, can also be added to add, the shortcomings are not support polyphone, because the need to support the Polyphone query, so the latter have looked under other articles, Found that there is no ready-made articles (also may be my search level comparison of water). Later find found for Chinese pinyin, originally Microsoft has provided Microsoft Visual Studio International Pack , and very powerful. So I tried.

Back to top first refer to the corresponding package in NuGet

Find Pinyinconverter

Back to the top simple demo

Small try, the use is very simple, as long as the direct use of Chinesechar class to replace the good

1             String ch = console.readline (), 2             chinesechar cc = new Chinesechar (Ch[0]), 3             var pinyins = cc. Pinyins.tolist (); 4             pinyins. ForEach (Console.WriteLine);

The results are as follows:

We can see that the line of Polyphone has hang,heng,xing Three, here the Legato Mark also came out, indeed very convenient. And I need the function is input bank , then convert to Pinyin is yinhang,yinheng,yinxing, the first spell is Yh,yx. It's easy to think of a class with Chinesechar.

Back to top kanji to Pinyin class package

1. First split the input kanji

2. Then each character gets multiple pinyin with Chinesechar

3. Then remove the number, go to the weight, extract the first character, and then make a combination.

Then wrote a helper class to reload, the code is as follows:

 public class Pinyinconverterhelp {public static Pingyinmodel Gettotalpingyin (String str) {V AR chs = str.            ToCharArray (); Record the full 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]; Whether it is a valid kanji if (Chinesechar.isvalidchar (ch)) {Chinesechar cc = new Chines                    Echar (CH); Pinyins = cc. Pinyins.where (p =!string. Isnullorwhitespace (P)).                ToList (); } else {Pinyins. ADD (Ch.                ToString ()); }//Remove tones, turn lowercase pinyins = pinyins. ConvertAll (p = regex.replace (P, @ "\d", "").                ToLower ()); Go to 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 loop matching var newtotalpingyins = new List<s                    Tring> (); foreach (var totalpingyin in result. Totalpingyin) {newtotalpingyins.addrange (items.                    Select (item = Totalpingyin + Item)); } newtotalpingyins = Newtotalpingyins.distinct ().                 ToList ();   Result.                    Totalpingyin = Newtotalpingyins;                    The first letter loop matches the 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; }    }

Call Mode:

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

Results:

Currently tried some rare words are can support, for some too biased has not tried, but for the general Chinese Pinyin, polyphone support here is enough.

This is only used in the Microsoft Visual Studio International Pack This extension package inside the Chinese character to pinyin function, in fact, there are Chinese, Japanese, Korean, English and other countries language packs, and provide methods to achieve mutual transfer, get, Get the word count, even get the number of strokes, and so powerful features, interested friends can query its own API.

Back to the top source of 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 learning, so also hope that we can share the courage.

Finally, the source code to share out to everyone, if there are errors and deficiencies in the place, also hope to correct

Address: Https://github.com/qq1206676756/PinYinParse

C # kanji Pinyin (support Polyphone)

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.