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());
    }
}

 
發佈了28 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章