문자 병음 이니셜 가져오기

4181 단어 얻다
/**
 * @date 2010-1-22
 * @bugs          
 */
public class PinyinConv {
    //           B0A1(45217)   F7FE(63486)
    private static int BEGIN = 45217;
    private static int END = 63486;

    //       ,     GB2312          ,    “ ”      a      。
    // i, u, v     ,            
    private static char[] chartable = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
            ' ', ' ', ' ', ' ', ' ', };

    //                 
    // GB2312          
    private static int[] table = new int[27];

    //         
    private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            't', 't', 'w', 'x', 'y', 'z'};

    //    
    static {
        for (int i = 0; i < 26; i++) {
            table[i] = gbValue(chartable[i]);//   GB2312          ,   。
        }
        table[26] = END;//      
    }

    // ------------------------public   ------------------------
    /**
     *                                     ,    :       、  、  
     */
    public static String cn2py(String SourceStr) {
        String Result = "";
        int StrLength = SourceStr.length();
        int i;
        try {
            for (i = 0; i < StrLength; i++) {
                Result += Char2Initial(SourceStr.charAt(i));
            }
        } catch (Exception e) {
            Result = "";
        }
        return Result;
    }

    // ------------------------private   ------------------------
    /**
     *     ,      ,             ,          '0'
     * 
     */
    private static char Char2Initial(char ch) {
        //         :         ,       
        if (ch >= 'a' && ch <= 'z')
            return (char) (ch - 'a' + 'A');
        if (ch >= 'A' && ch <= 'Z')
            return ch;

        //          :      ,            ,
        //    ,     。
        //   ,          。
        int gb = gbValue(ch);//        

        if ((gb < BEGIN) || (gb > END))//        ,    
            return ch;

        int i;
        for (i = 0; i < 26; i++) {//         ,    break,      “[,)”
                if ((gb >= table[i]) && (gb < table[i+1]))
                    break;
        }
        
        if (gb==END) {//  GB2312     
            i=25;
        }
        return initialtable[i]; //       ,     
    }

    /**
     *         cn   
     */
    private static int gbValue(char ch) {//      (GB2312)        。
        String str = String.valueOf(ch);
        try {
            byte[] bytes = str.getBytes("GB2312");
            if (bytes.length < 2)
                return 0;
            return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
        } catch (Exception e) {
            return 0;
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(cn2py("      IT  ,     , ,IBM     "));
    }
}


이 단락의 코드가 인터넷에 올라오는 원리는 한자를 통한 GB2312의 인코딩은 병음 이니셜의 순서대로 배열되어 구간 비교를 통해 이니셜을 얻는 것이지만, 여기서도 다음자는 구분할 수 없고 문자의 기본 음절의 이니셜만 반영된다는 것을 알 수 있다.

좋은 웹페이지 즐겨찾기