`
wooyon
  • 浏览: 22539 次
  • 性别: Icon_minigender_1
  • 来自: XT
社区版块
存档分类
最新评论

java利用ascii码表,把汉字转成拼音(完整版)

阅读更多

解决的问题实际上是: 输入一个汉语拼音,从数据库或xml文件里查询出对应的拼音开头的汉字

以下为解决问题需要的资源地址:

汉字和ascii码的关系(汉字码也叫内码是ascii码的扩展)
参考url地址: http://tech.ccw.com.cn/article/199466.htm

计算机中汉字是如何表示的(即汉字表示的基础知识)
http://www.cbi.pku.edu.cn/chinese/documents/csdoc/basic/

java利用ascii码表,把汉字转成拼音(完整版) ,即 java 代码示例:
http://blog.lequ8.cn/louis/2006/6/27/

以上三个网址:一步步看看,就可以解决:
   给个“拼音或字母”,就可以从数据库里查询到相对应的 “汉字”。

 

网上资源提供了一个“利用ascii码表,把汉字转成拼音”的源代码,如下:

java利用ascii码表,把汉字转成拼音(完整版)

由于网上的api只能处理常见的汉字对于有些汉字转不了,或者不太准确,本人利用输入法的码表制作了个java的API
该API制作过程种碰到了"exceeding the 65535 bytes limit" java中每个函数不能多余65535个字节的限制,码表的初始化过程分了很多个函数来解决码表的初始化过程。代码部分如下:

完整代码下载:http://www.easyea.com/UserFiles/0/0/0/6/File/CnToSpell.zip


import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Date;

/**
*
* 汉字转化为全拼,码表利用输入法的码表导出,汉字信息非常全,平时见到的汉字
* 转拼音的API只能处理几千个一级汉字,很多字都没有法查到,尤其是我自己的姓
* 都查不到所以只好自己做一个汉字转拼音的API,该API除可以处理99%以上的汉字,
* 而且可以返回多音字的读音
*
* JDK版本: 1.4
*/
public class CnToSpell {
    private static LinkedHashMap spellMap = null;

    static {
        if (spellMap == null) {
            spellMap = new LinkedHashMap(20901);
        }
        initialize();
    }

    private CnToSpell() {
    }

    private static void spellPut(String ascii, String spells) {
        spellMap.put(ascii, spells);
    }
   
    private static void initialize() {
        init0();
        init1();
        init2();
        init3();
        init4();
        init5();
        init6();
        init7();
        init8();
        init9();
        init10();
        init11();
        init12();
        init13();
        init14();
        init15();
        init16();
        init17();
        init18();
        init19();
        init20();
        init21();
        init22();
        init23();
        init24();
        init25();
        init26();
        init27();
        init28();
        init29();
        init30();
        init31();
        init32();
        init33();
        init34();
        init35();
        init36();
        init37();
        init38();
        init39();
        init40();
        init41();
        init42();
        init43();
        init44();
        init45();
        init46();
        init47();
        init48();
        init49();
        init50();
        init51();
        init52();
        init53();
        init54();
        init55();
        init56();
        init57();
        init58();
        init59();
        init60();
        init61();
        init62();
        init63();
        init64();
        init65();
        init66();
        init67();
        init68();
        init69();
        init70();
        init71();
        init72();
        init73();
        init74();
        init75();
        init76();
        init77();
        init78();
        init79();
        init80();
        init81();
        init82();
        init83();
        init84();
        init85();
        init86();
        init87();
        init88();
        init89();
        init90();
        init91();
        init92();
        init93();
        init94();
        init95();
        init96();
        init97();
        init98();
        init99();
        init100();
        init101();
        init102();
        init103();
        init104();
        init105();
        init106();
        init107();
        init108();
        init109();
        init110();
        init111();
        init112();
        init113();
        init114();
        init115();
    }
   
    private static void init0() {
        spellPut("129-64", "kao,qiao,yu");
        spellPut("129-65", "shang");
        spellPut("129-66", "xia");
        spellPut("129-67", "mo ye o ne");
        spellPut("129-68", "mian");
        spellPut("129-69", "chou");
        spellPut("129-70", "shi");
        spellPut("129-71", "diu");
        spellPut("129-72", "qiu");
        spellPut("129-73", "liang");
        spellPut("129-74", "you");
        spellPut("129-75", "bing");
        spellPut("129-76", "jiu");
        spellPut("129-77", "ji");
        spellPut("129-78", "jie");
        spellPut("129-79", "guan,kuang");
        spellPut("129-80", "chan");
        spellPut("129-81", "zhuo");
        spellPut("129-82", "ba");
        spellPut("129-83", "jing");
        spellPut("129-84", "fu");
        spellPut("129-85", "yi,ji");
        spellPut("129-86", "yi");
        spellPut("129-87", "shi me");
        spellPut("129-88", "jiu");
        spellPut("129-89", "ho");
        spellPut("129-90", "yin");
        spellPut("129-91", "hu");
        spellPut("129-92", "cheng,sheng");
        spellPut("129-93", "hao,yi");
        spellPut("129-94", "wBn");
        spellPut("129-95", "gai");
        spellPut("129-96", "jiu");
        spellPut("129-97", "hB ri");
        spellPut("129-98", "ho lo");
        spellPut("129-99", "dou");
        spellPut("129-100", "shi");
        spellPut("129-101", "nang");
        spellPut("129-102", "gB ri");
        spellPut("129-103", "ge ri");
        spellPut("129-104", "dBo ri");
        spellPut("129-105", "mo ri");
        spellPut("129-106", "o lo");
        spellPut("129-107", "cB lo");
        spellPut("129-108", "xue");
        spellPut("129-109", "yan");
        spellPut("129-110", "po ri");
        spellPut("129-111", "sB lo");
        spellPut("129-112", "na");
        spellPut("129-113", "qian");
        spellPut("129-114", "sBo ri");
        spellPut("129-115", "er");
        spellPut("129-116", "zu ri");
        spellPut("129-117", "zB ri");
        spellPut("129-118", "zhi,luan");
        spellPut("129-119", "gui");
        spellPut("129-120", "qian");
        spellPut("129-121", "luan");
        spellPut("129-122", "lin");
        spellPut("129-123", "yi");
        spellPut("129-124", "jue");
        spellPut("129-125", "ma");
        spellPut("129-126", "shi");
        spellPut("129-128", "yu");
        spellPut("129-129", "si");
        spellPut("129-130", "sui");
        spellPut("129-131", "gen");
        spellPut("129-132", "ya");
        spellPut("129-133", "qi,zhai");
        spellPut("129-134", "ya");
        spellPut("129-135", "da");
        spellPut("129-136", "ye");
        spellPut("129-137", "xiang");
        spellPut("129-138", "jing");
        spellPut("129-139", "ye");
        spellPut("129-140", "you");
        spellPut("129-141", "dan");
        spellPut("129-142", "lian");
        spellPut("129-143", "duo");
        spellPut("129-144", "wei,men");
        spellPut("129-145", "ji");
        spellPut("129-146", "ji");
        spellPut("129-147", "wang");
        spellPut("129-148", "ba");
        spellPut("129-149", "bing");
        spellPut("129-150", "fo");
        spellPut("129-151", "jin,san");
        spellPut("129-152", "e o");
        spellPut("129-153", "xian");
        spellPut("129-154", "tuo,cha,duo");
        spellPut("129-155", "hong");
        spellPut("129-156", "gan,han");
        spellPut("129-157", "bo");
        spellPut("129-158", "chao");
        spellPut("129-159", "chang,zhang");
        spellPut("129-160", "chang");
        spellPut("129-161", "ren");
        spellPut("129-162", "fan");
        spellPut("129-163", "chao,miao");
        spellPut("129-164", "qian");
        spellPut("129-165", "wo");
        spellPut("129-166", "yao,fo");
        spellPut("129-167", "feng");
        spellPut("129-168", "cang");
        spellPut("129-169", "wang");
        spellPut("129-170", "di");
        spellPut("129-171", "zhong");
        spellPut("129-172", "pei");
        spellPut("129-173", "yu");
        spellPut("129-174", "diao");
        spellPut("129-175", "dun");
        spellPut("129-176", "wen");
        spellPut("129-177", "yi");
        spellPut("129-178", "xin");
        spellPut("129-179", "ji");
        spellPut("129-180", "ai");
        spellPut("129-181", "jin");
        spellPut("129-182", "pi");
        spellPut("129-183", "dan");
        spellPut("129-184", "fu");
        spellPut("129-185", "tang");
        spellPut("129-186", "cui");
        spellPut("129-187", "yun");
        spellPut("129-188", "che,ju");
        spellPut("129-189", "qian");
        spellPut("129-190", "xun");
        spellPut("129-191", "xin");
        spellPut("129-192", "chi");
        spellPut("129-193", "xian,xuan");
        spellPut("129-194", "nu");
        spellPut("129-195", "ni");
        spellPut("129-196", "xie");
        spellPut("129-197", "xu");
        spellPut("129-198", "zhou");
        spellPut("129-199", "qu");
        spellPut("129-200", "peng");
        spellPut("129-201", "pi");
        spellPut("129-202", "zhi");
        spellPut("129-203", "si");
        spellPut("129-204", "yi,chi");
        spellPut("129-205", "zheng");
        spellPut("129-206", "han,gan");
        spellPut("129-207", "mai");
        spellPut("129-208", "zhu");
        spellPut("129-209", "bu");
        spellPut("129-210", "qu");
        spellPut("129-211", "bi");
        spellPut("129-212", "zhao,shao");
        spellPut("129-213", "ci");
        spellPut("129-214", "yang");
        spellPut("129-215", "zhan,dian");
        spellPut("129-216", "bi");
        spellPut("129-217", "xian");
        spellPut("129-218", "qu");
        spellPut("129-219", "shi");
        spellPut("129-220", "bBo");
        spellPut("129-221", "hui,huai");
        spellPut("129-222", "ge");
        spellPut("129-223", "xiang");
        spellPut("129-224", "ge");
        spellPut("129-225", "fa");
        spellPut("129-226", "ming");
        spellPut("129-227", "bing");
        spellPut("129-228", "hen");
        spellPut("129-229", "huo");
        spellPut("129-230", "gui");
        spellPut("129-231", "quan");
        spellPut("129-232", "ci");
        spellPut("129-233", "xing");
        spellPut("129-234", "shen");
        spellPut("129-235", "tuo");
        spellPut("129-236", "gai");
        spellPut("129-237", "lai");
        spellPut("129-238", "yi");
        spellPut("129-239", "gong");
        spellPut("129-240", "yin");
        spellPut("129-241", "mi");
        spellPut("129-242", "xu");
        spellPut("129-243", "an");
        spellPut("129-244", "lu");
        spellPut("129-245", "er");
        spellPut("129-246", "lun");
        spellPut("129-247", "cha");
        spellPut("129-248", "chi");
        spellPut("129-249", "xun");
        spellPut("129-250", "zhou");
        spellPut("129-251", "ru");
        spellPut("129-252", "cun");
        spellPut("129-253", "si");
        spellPut("129-254", "dai");
    }
    .......
    .......
    .......

    /**
     * 获得单个汉字的Ascii,并用"-"连接成一个字符串
     *
     * @param cn char 汉字字符
     * @return string 错误返回 空字符串,否则返回ascii
     */
    public static String getCnAscii(char cn) {
        byte[] bytes = (String.valueOf(cn)).getBytes();
        System.out.println(bytes.length);
        if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误
            return "";
        }
        if (bytes.length == 1) { // 英文字符
            return new String(bytes);
        }
        if (bytes.length == 2) { // 中文字符
            int hightByte = 256 + bytes[0];
            int lowByte = 256 + bytes[1];

            String ascii = hightByte + "-" + lowByte;

            System.out.println("ASCII=" + ascii);

            return ascii;
        }

        return ""; // 错误
    }

    /**
     * 根据ASCII码连接成的字符串到SpellMap中查找对应的拼音
     *
     * @param ascii 字符对应的ASCII连接的字符串
     * @return String 拼音,首先判断是否是中文如果是英文直接返回字符,如果是中文返回拼音,
     *
     * 否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
     */
    public static String getSpellByAscii(String ascii) {
        if (ascii.indexOf("-") > -1)
        {
            return (String)spellMap.get(ascii);
        } else {
            return ascii;
        }
    }

    /**
     * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
     *
     * @param cnStr String字符串
     * @return String 转换成全拼后的字符串
     */
    public static String getFullSpell(String cnStr)
    {
        if (null == cnStr || "".equals(cnStr.trim())) {
            return cnStr;
        }

        char[] chars = cnStr.toCharArray();
        StringBuffer retuBuf = new StringBuffer();
        for (int i = 0, Len = chars.length; i < Len; i++) {
            String ascii = getCnAscii(chars[i]);
            if (ascii.length() == 0) { // 取ascii时出错
                retuBuf.append(chars[i]);
            } else {
                String spell = getSpellByAscii(ascii);
                if (spell == null) {
                    retuBuf.append(chars[i]);
                } else {
                    retuBuf.append(spell);
                } // end of if spell == null
            } // end of if ascii <= -20400
        } // end of for

        return retuBuf.toString();
    }

    /**
     * 获取汉语字符串的声母组合,每个汉字取拼音的第一个字符组成的一个字符串
     * @param cnStr 汉字的字符串
     * @return 每个汉字拼音的第一个字母所组成的汉字
     */
    public static String getFirstSpell(String cnStr)
    {
        if (null == cnStr || "".equals(cnStr.trim())) {
            return cnStr;
        }

        char[] chars = cnStr.toCharArray();
        StringBuffer retuBuf = new StringBuffer();
        for (int i = 0, Len = chars.length; i < Len; i++) {
            String ascii = getCnAscii(chars[i]);
            System.out.println(ascii);
            if (ascii.length() == 1) { // 取ascii时出错
                retuBuf.append(chars[i]);
            } else {
               
                String spell = getSpellByAscii(ascii).substring(0, 1);
                System.out.println(spell);
                if (spell == null) {
                    retuBuf.append(chars[i]);
                } else {
                    retuBuf.append(spell);
                } // end of if spell == null
            } // end of if ascii <= -20400
        } // end of for
        return retuBuf.toString();
    }

    public static void main(String[] args) {
        Date start = new Date();
        System.out.println(start.getTime());
        String str = null;
        str = "逯 闫 乐";
        System.out.println("Spell=" + CnToSpell.getFullSpell(str));
        System.out.println("Spell=" + CnToSpell.getFirstSpell(str));
        Date end = new Date();
        System.out.println(end.getTime());
    }
}

分享到:
评论
1 楼 jiangdx 2008-05-26  
很好,正需要这个,感谢分享,这个API也比较晚完善,就是类太大了,编译的好慢。有优化版本的M我呀,谢谢。QQ:55574065

相关推荐

Global site tag (gtag.js) - Google Analytics