|
最近忙一個地址簿的小項(xiàng)目,需要實(shí)現(xiàn)類似 outlook 的功能,即通過聯(lián)系人姓氏的拼音的聲母找到相關(guān)的聯(lián)系人,比如 按 Z 那么找出所有姓張的聯(lián)系人,其他以拼音 Z 開頭也會找出來,比如姓趙、鄭 ......,當(dāng)然 Zidane(齊達(dá)內(nèi))也會被找出來。 我有 java 實(shí)現(xiàn)的代碼,如下: public static char getInitial(String str) { byte[] bs; try { bs=str.getBytes("gb2312"); } catch(java.io.UnsupportedEncodingException e) { return ‘*‘; } if(bs.length==0) return ‘*‘; switch(bs[0]) { case (byte)‘a(chǎn)‘: case (byte)‘A‘: return ‘a(chǎn)‘; case (byte)‘b‘: case (byte)‘B‘: return ‘b‘; case (byte)‘c‘: case (byte)‘C‘: return ‘c‘; case (byte)‘d‘: case (byte)‘D‘: return ‘d‘; case (byte)‘e‘: case (byte)‘E‘: return ‘e‘; case (byte)‘f‘: case (byte)‘F‘: return ‘f‘; case (byte)‘g‘: case (byte)‘G‘: return ‘g‘; case (byte)‘h‘: case (byte)‘H‘: return ‘h‘; case (byte)‘i‘: case (byte)‘I‘: return ‘i‘; case (byte)‘j‘: case (byte)‘J‘: return ‘j‘; case (byte)‘k‘: case (byte)‘K‘: return ‘k‘; case (byte)‘l‘: case (byte)‘L‘: return ‘l‘; case (byte)‘m‘: case (byte)‘M‘: return ‘m‘; case (byte)‘n‘: case (byte)‘N‘: return ‘n‘; case (byte)‘o‘: case (byte)‘O‘: return ‘o‘; case (byte)‘p‘: case (byte)‘P‘: return ‘p‘; case (byte)‘q‘: case (byte)‘Q‘: return ‘q‘; case (byte)‘r‘: case (byte)‘R‘: return ‘r‘; case (byte)‘s‘: case (byte)‘S‘: return ‘s‘; case (byte)‘t‘: case (byte)‘T‘: return ‘t‘; case (byte)‘u‘: case (byte)‘U‘: return ‘u‘; case (byte)‘v‘: case (byte)‘V‘: return ‘v‘; case (byte)‘w‘: case (byte)‘W‘: return ‘w‘; case (byte)‘x‘: case (byte)‘X‘: return ‘x‘; case (byte)‘y‘: case (byte)‘Y‘: return ‘y‘; case (byte)‘z‘: case (byte)‘Z‘: return ‘z‘; default: if(bs.length>=2) //長度>=2就有可能是中文 { int b1=bs[0]&0xff; int b2=bs[1]&0xff; int value=(b1<<8)|b2; System.out.println(value); if( value>=0xb0a1 && value<=0xb0c4) return ‘a(chǎn)‘; if( value>=0xb0c5 && value<=0xb2c0) return ‘b‘; if( value>=0xb2c1 && value<=0xb4ed) return ‘c‘; if( value>=0xb4ee && value<=0xb6e9) return ‘d‘; if( value>=0xb6ea && value<=0xb7a1) return ‘e‘; if( value>=0xb7a2 && value<=0xb8c0) return ‘f‘; if( value>=0xb8c1 && value<=0xb9fd) return ‘g‘; if( value>=0xb9fe && value<=0xbbf6) return ‘h‘; if( value>=0xbbf7 && value<=0xbfa5) return ‘j‘; if( value>=0xbfa6 && value<=0xc0ab) return ‘k‘; if( value>=0xc0ac && value<=0xc2e7) return ‘l‘; if( value>=0xc2e8 && value<=0xc4c2) return ‘m‘; if( value>=0xc4c3 && value<=0xc5b5) return ‘n‘; if( value>=0xc5b6 && value<=0xc5bd) return ‘o‘; if( value>=0xc5be && value<=0xc6d9) return ‘p‘; if( value>=0xc6da && value<=0xc8ba) return ‘q‘; if( value>=0xc8bb && value<=0xc8f5) return ‘r‘; if( value>=0xc8f6 && value<=0xcbf9) return ‘s‘; if( value>=0xcbfa && value<=0xcdd9) return ‘t‘; if( value>=0xcdda && value<=0xcef3) return ‘w‘; if( value>=0xcef4 && value<=0xd188) return ‘x‘; if( value>=0xd1b9 && value<=0xd4d0) return ‘y‘; if( value>=0xd4d1 && value<=0xd7f9) return ‘z‘; } } return ‘*‘; } 代碼其實(shí)很簡單,要了解為什么要這么寫,則需要了解一下 GB2312 編碼,我覺得HP的官方網(wǎng)站的資料還不錯,簡單明了。網(wǎng)址是這里。里面有一段是這樣的: 國標(biāo) GB2312 基于 1980 年發(fā)布的《信息交換用漢字編碼字符集基本集》,是中文信息處理的中國國家標(biāo)準(zhǔn),是強(qiáng)制執(zhí)行的中文編碼。 國標(biāo)碼共收錄 6763 個簡體漢字、682 個符號,其中漢字部分:一級字 3755 個,以拼音排序,二級字 3008 個,以偏旁部首排序。該標(biāo)準(zhǔn)的制定和應(yīng)用為規(guī)范、推動中文信息化進(jìn)程起了很大作用。該標(biāo)準(zhǔn)用雙字節(jié)表示一個漢字:高字節(jié) A1-F7(其中字符區(qū) A1-F9,漢字區(qū) B0-F7)低字節(jié) A1-FE。 上面的文字已經(jīng)說得很清楚,最常用的 3755 個漢字是用拼音排序的,所以才有可能比較方便的實(shí)現(xiàn)用拼音找到漢字。同樣也說明了,有一些少見的姓通過上面的代碼是找不出來的,比如臺灣有個叫庹中康的主持人。 不過在我那個小項(xiàng)目里,小到覺得編譯 java 都多余,我希望用 javascript 就實(shí)現(xiàn)同上 java 實(shí)現(xiàn)的功能。我再次求助google,找到了 http://www./user/qswh/qswhGB2312.js。網(wǎng)上能人輩出啊。范例可以看看 http://www./user/qswh/GB2312.html。 這個腳本用來解析出漢字的拼音。我加了一個方法以滿足我的需要,代碼如下: function getInitial(str) { if (str == null || /^\s*$/.test(str)) return null; try { var code = str.charCodeAt(0); //charCodeAt方法需要ie5.5 以上才支持 if (code >= 48 && code <= 57) // 如果是數(shù)字,就返回 return str.charAt(0); else if (code <= 127) { return str.charAt(0).toUpperCase(); } else { var py = getSpell(str); //getSpell()得到字符串的拼音 return (py)?py.substring(0,1).toUpperCase():null; } } catch (e) { return null; } } qswhGB2312.js 文件中將常用的3755個漢字組成一個字符串賦予一個變量。因?yàn)榘瑵h字,所以如果 jsp 或者 template 頁面使用的是非gb2312的編碼,比如UTF-8,那么就需要在<script>中加一個 charset=gb2312,比如: <script language="javascript" src="qswhGB2312.js" charset="gb2312"></script> 因?yàn)轫?xiàng)目要求支持中簡繁,我想知道Big5碼是否也有類似的拼音的設(shè)計(jì),看了《由 堃(方方土) 探討 Big5e 編碼》,知道了大概是不行的,同時(shí)感覺大陸在計(jì)算機(jī)語言編碼設(shè)計(jì)方面比臺灣還是要大氣得多。 去年看過一些文章不少人攻擊漢字拉丁化和簡化字等等改革,有些還是有道理,不過說句公道話,拼音對于計(jì)算機(jī)的漢語錄入還是貢獻(xiàn)巨大,我沒有歧視五筆的意思,不過我總是努力勸說身邊的人嘗試用拼音輸入法。 |
|
|