之前由于項(xiàng)目需要,中間需要一個漢字轉(zhuǎn)拼音和首拼的功能來做查詢,感覺這種功能基本已經(jīng)成熟化了,于是查找了相關(guān)的代碼,首先引入眼簾的是下面兩篇文章
1.c# 漢字轉(zhuǎn)拼音(支持gb2312字符集中所有漢字)
2.javascript實(shí)現(xiàn)漢字和拼音互轉(zhuǎn)的終極方案
寫的比較全也很詳細(xì),都有提供源碼,大家可以參考下。
由于考慮到接口的需要,于是參考了 第一篇,文章中作者的源碼基本能滿足漢字轉(zhuǎn)拼音的需要,對于其他特殊的字符,也可以在進(jìn)行添加補(bǔ)充,不足之處就是不支持多音字,由于需要支持多音字的查詢,所以后面有查了下其他的文章,發(fā)現(xiàn)還沒有現(xiàn)成的文章(也可能本人的搜索水平比較水)。后來查找發(fā)現(xiàn)對于漢字轉(zhuǎn)拼音,原來微軟已經(jīng)提供了 microsoft visual studio international pack ,而且很強(qiáng)大。于是試了一下
首先在nuget引用對應(yīng)的包
查找 pinyinconverter
簡單的demo
小試一下,使用也非常簡單,只要直接使用chinesechar類進(jìn)行裝換就好
1
2
3
4
|
string ch = console.readline(); chinesechar cc = new chinesechar(ch[0]); var pinyins = cc.pinyins.tolist(); pinyins. foreach (console.writeline); |
結(jié)果如下:
我們可以看到, 行 的多音字有 hang,heng,xing 三個,這里連音標(biāo)也出來了,確實(shí)很方便。而我需要的功能是輸入 銀行 ,然后轉(zhuǎn)換為拼音是 yinhang,yinheng,yinxing, 首拼是 yh,yx。有chinesechar 這個類的話做起來思路就簡單了。
漢字轉(zhuǎn)拼音類封裝
1.首先對輸入的漢字進(jìn)行拆分
2.接著每個漢字用chinesechar 獲取多個拼音
3.然后除去數(shù)字,去重,提取首字符,再在進(jìn)行組合就好了
于是寫了個幫助類進(jìn)行裝換,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
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()); } //去除聲調(diào),轉(zhuǎn)小寫 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 { //全拼循環(huán)匹配 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; //首字母循環(huán)匹配 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 ; } } |
調(diào)用方式:
1
2
3
4
5
6
7
|
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(); |
結(jié)果:
目前試過一些生僻字都是能支持,對于一些太偏的還沒試過,不過對于一般漢字轉(zhuǎn)拼音的,多音字支持這里就已經(jīng)足夠了。
這里僅僅是使用了 microsoft visual studio international pack 這個擴(kuò)展包里面的漢字轉(zhuǎn)拼音功能,其實(shí)里面還有中文、日文、韓文、英語等各國語言包,并提供方法實(shí)現(xiàn)互轉(zhuǎn)、獲、獲取字?jǐn)?shù)、甚至獲取筆畫數(shù)等等強(qiáng)大的功能,有興趣的朋友可以自行查詢下它的api。
源碼分享
分享是一種美德,有時候牛逼的文章可以提高我們的技術(shù)層面,但有時候更多的需求是業(yè)務(wù)層面,很多小知識應(yīng)用的分享卻可以幫我們提高業(yè)務(wù)層面的問題。只要分享的知識點(diǎn)有用,不誤人子弟,哪怕大小都是一種學(xué)習(xí),所以也希望大家能勇于分享。
最后,源碼分享出來給大家,如果有錯誤和不足的地方,也希望指正
地址:demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/qtqq/p/6195641.html#_label0