C # kanji Pinyin (support Polyphone)

Source: Internet
Author: User
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

C # kanji to Pinyin (supports all Chinese characters in the GB2312 character set) (http://www.cnblogs.com/cxd4321/p/4203383.html)

"Dry" JS version of Chinese characters and Pinyin mutual turn the ultimate solution, with simple JS Pinyin Input method (http://www.cnblogs.com/liuxianan/p/pinyinjs.html)

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.

First, the corresponding package is referenced in NuGet

Find Pinyinconverter

A simple Demo

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

string ch = console.readline (); Chinesechar cc = new Chinesechar (ch[0]); var pinyins = cc. Pinyins.tolist ();p inyins. 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.

Chinese character 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) {VA R 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; }    }

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, gain, get word count, Even with powerful features such as the number of strokes, an interested friend can query its API on its own.


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

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

The above is C # kanji Pinyin (support polyphone) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.