spellPut("zhi", -10587); spellPut("zhong", -10544); spellPut("zhou", -10533); spellPut("zhu", -10519); spellPut("zhua", -10331); spellPut("zhuai", -10329); spellPut("zhuan", -10328); spellPut("zhuang", -10322); spellPut("zhui", -10315); spellPut("zhun", -10309); spellPut("zhuo", -10307); spellPut("zi", -10296); spellPut("zong", -10281); spellPut("zou", -10274); spellPut("zu", -10270); spellPut("zuan", -10262); spellPut("zui", -10260); spellPut("zun", -10256); spellPut("zuo", -10254); }
/** * 获得单个汉字的Ascii. * @param cn char * 汉字字符 * @return int * 错误返回 0,否则返回ascii */ public static int getCnAscii(char cn) { byte[] bytes = (String.valueOf(cn)).getBytes(); if(bytes == null || bytes.length > 2 || bytes.length <= 0){ //错误 return 0; } if(bytes.length == 1){ //英文字符 return bytes[0]; } if(bytes.length == 2){ //中文字符 int hightByte = 256 bytes[0]; int lowByte = 256 bytes[1];
int ascii = (256 * hightByte lowByte) - 256 * 256;
//System.out.println("ASCII=" ascii);
return ascii; }
return 0; //错误 }
/** * 根据ASCII码到SpellMap中查找对应的拼音 * @param ascii int * 字符对应的ASCII * @return String * 拼音,首先判定ASCII是否>0&<160,假如是返回对应的字符, * 否则到SpellMap中查找,假如没有找到拼音,则返回null,假如找到则返回拼音. */ public static String getSpellByAscii(int ascii) { if(ascii > 0 && ascii < 160){ //单字符 return String.valueOf((char)ascii); }
if(ascii < -20319 || ascii > -10247){ //不知道的字符 return null; }
Set keySet = spellMap.keySet(); Iterator it = keySet.iterator();
String spell0 = null;; String spell = null;
int asciiRang0 = -20319; int asciiRang; while(it.hasNext()){
spell = (String)it.next(); Object valObj = spellMap.get(spell); if(valObj instanceof Integer){ asciiRang = ((Integer)valObj).intValue();
if(ascii >= asciiRang0 && ascii < asciiRang){ //区间找到 return(spell0 == null) ? spell : spell0; } else{ spell0 = spell; asciiRang0 = asciiRang; } } }
return null;
}
/** * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换 * @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 ){ int ascii = getCnAscii(chars[i]); if(ascii == 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(); }
public static String getFirstSpell(String cnStr) { return null; }
public static void main(String[] args) { String str = null; str = "谢海101普降喜雨"; System.out.println("Spell=" CnToSpell.getFullSpell(str)); str = "张牙舞爪》。,"; System.out.println("Spell=" CnToSpell.getFullSpell(str)); str = "李鹏,可耻下场。"; System.out.println("Spell=" CnToSpell.getFullSpell(str)); str = "猪油,猪八戒。"; System.out.println("Spell=" CnToSpell.getFullSpell(str)); } }
共4页: 上一页 [1] [2] [3] 4 下一页 |