`
zqc53
  • 浏览: 25421 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

类似qq的汉字拼音首字查询

阅读更多
项目里要用到这个功能,自己边琢磨边找资料,弄出一个demo,大概思路是这样:用Properties的load()来加载一个固定格式的文本文件注1,然后直接当Hashtable用。
package org.navyblue.tests;

import java.util.
*;

/**//**
 *
 * 类似qq的汉字拼音首字查询方法
 * by zqc 2005-08-04
 *
 **
*/

public class pyTest
{
    
public pyTest(){}
    
    
public static void main(String[] args)throws Exception{
        Properties pyMap 
= new Properties();
        
//拼音隐射表
        pyMap.load(new java.io.FileInputStream("pyutf.txt"));
        
//测试字符
        String[] testStr = {"我们","阿哈","嘎嘎","干什么","郑晴川",""};
        
//模拟输入
        String input = "g";
        
        System.
out.println ("输入查询字符:g");
        Vector inis 
= new Vector();
        Vector results 
= new Vector();
        Vector results2 
= new Vector();
        
        
//返回所有测试字符串的首字的打头拼音
        forint i =0 ; i < testStr.length ; i++ ){
            String ini 
=new String( new char[] { testStr[i].charAt(0)} );
            String k 
= (String)pyMap.get(ini);
            results.add(k);
            results2.add(testStr[i]);
        }

        
        
//输出结果
        int l = results.size();
        
for(int i = 0 ; i < l ; i++){
            String c 
= (String)results.get(i);
            
if(c.equals(input)){
                System.
out.println (results2.get(i));
            }

        }

    }

}


----
注1:这个映射文件系网上找来的,大约有14K的汉字量,一般也够用了~用文本工具对原先的格式做了些修改使它符合Properties的格式,然后再用native2ascii转成utf-8编码。



2005年8月8日 0:48:54
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics