使用拼音搜索中文

来源:百度文库 编辑:神马文学网 时间:2024/04/29 19:12:22
最近忙一个地址簿的小项目,需要实现类似 outlook 的功能,即通过联系人姓氏的拼音的声母找到相关的联系人,比如  按 Z 那么找出所有姓张的联系人,其他以拼音 Z 开头也会找出来,比如姓赵、郑 ......,当然 Zidane(齐达内)也会被找出来。
我有 java 实现的代码,如下:
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‘: case (byte)‘A‘: return ‘a‘;
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‘;
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 ‘*‘;
}
代码其实很简单,要了解为什么要这么写,则需要了解一下 GB2312 编码,我觉得HP的官方网站的资料还不错,简单明了。网址是这里。里面有一段是这样的:
国标 GB2312 基于 1980 年发布的《信息交换用汉字编码字符集基本集》,是中文信息处理的中国国家标准,是强制执行的中文编码。
国标码共收录 6763 个简体汉字、682 个符号,其中汉字部分:一级字 3755 个,以拼音排序,二级字 3008 个,以偏旁部首排序。该标准的制定和应用为规范、推动中文信息化进程起了很大作用。该标准用双字节表示一个汉字:高字节 A1-F7(其中字符区 A1-F9,汉字区 B0-F7)低字节 A1-FE。
上面的文字已经说得很清楚,最常用的 3755 个汉字是用拼音排序的,所以才有可能比较方便的实现用拼音找到汉字。同样也说明了,有一些少见的姓通过上面的代码是找不出来的,比如台湾有个叫庹中康的主持人。
不过在我那个小项目里,小到觉得编译 java 都多余,我希望用 javascript 就实现同上 java 实现的功能。我再次求助google,找到了http://www.blueidea.com/user/qswh/qswhGB2312.js。网上能人辈出啊。范例可以看看http://www.blueidea.com/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) // 如果是数字,就返回
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个汉字组成一个字符串赋予一个变量。因为包含汉字,所以如果 jsp 或者 template 页面使用的是非gb2312的编码,比如UTF-8,那么就需要在
因为项目要求支持中简繁,我想知道Big5码是否也有类似的拼音的设计,看了《由 堃(方方土) 探討 Big5e 編碼》,知道了大概是不行的,同时感觉大陆在计算机语言编码设计方面比台湾还是要大气得多。
去年看过一些文章不少人攻击汉字拉丁化和简化字等等改革,有些还是有道理,不过说句公道话,拼音对于计算机的汉语录入还是贡献巨大,我没有歧视五笔的意思,不过我总是努力劝说身边的人尝试用拼音输入法。