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.
considering the need of the interface, the 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, due to the need to support Polyphone query, so after the next check the 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 inNuGetreferencing the corresponding package
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
1 string ch = console.readline (); 2 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 the Polyphone have hang,heng,xing Three, here the Legato Mark also came out, indeed very convenient. And I need the function is to input the bank , and 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 classPinyinconverterhelp { Public StaticPingyinmodel Gettotalpingyin (stringstr) { varCHS =Str. ToCharArray (); //record the full spelling of each Chinese characterdictionary<int, list<string>> Totalpingyins =Newdictionary<int, list<string>>(); for(inti =0; I < CHS. Length; i++) { varPinyins =Newlist<string>(); varCH =Chs[i]; //whether it is a valid Chinese character if(Chinesechar.isvalidchar (ch)) {Chinesechar cc=NewChinesechar (CH); Pinyins= CC. Pinyins.where (p =!)string. Isnullorwhitespace (P)). ToList (); } Else{pinyins. ADD (Ch. ToString ()); } //remove tones, turn lowercasePinyins = Pinyins. ConvertAll (p = regex.replace (P),@"\d",""). ToLower ()); //Go heavyPinyins = Pinyins. Where (p =!)string. Isnullorwhitespace (P)). Distinct (). ToList (); if(Pinyins. Any ()) {Totalpingyins[i]=Pinyins; }} Pingyinmodel result=NewPingyinmodel (); foreach(varPinyinsinchtotalpingyins) { varItems =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 varNewtotalpingyins =Newlist<string>(); foreach(varTotalpingyininchresult. Totalpingyin) {newtotalpingyins.addrange (items. Select (Item= = Totalpingyin +item)); } newtotalpingyins=newtotalpingyins.distinct (). ToList (); Result. Totalpingyin=Newtotalpingyins; //First letter Loop match varNewfirstpingyins =Newlist<string>(); foreach(varFirstpingyininchresult. Firstpingyin) {newfirstpingyins.addrange (items. Select (Item= = Firstpingyin + Item. Substring (0,1))); } newfirstpingyins=newfirstpingyins.distinct (). ToList (); Result. Firstpingyin=Newfirstpingyins; } } returnresult; } } Public classPingyinmodel { PublicPingyinmodel () {Totalpingyin=Newlist<string>(); Firstpingyin=Newlist<string>(); } //full Spell Publiclist<string> Totalpingyin {Get;Set; } //First spell Publiclist<string> Firstpingyin {Get;Set; } }
Call Mode:
Console.WriteLine ("Please enter Chinese:"); stringstr =Console.ReadLine (); varSTRs =Pinyinconverterhelp.gettotalpingyin (str). Totalpingyin; varFrists =Pinyinconverterhelp.gettotalpingyin (str). Firstpingyin; Console.WriteLine ("full Pinyin:"+ String.Join (",", STRs)); Console.WriteLine ("First Tone:"+ String.Join (",", frists)); 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 just the use of Microsoft Visual Studio International Pack This expansion 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.
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)