JAVA将汉字转化成相应拼音
JAVA将汉字转化成相应拼音 /**
* CnToSpell.java
* @author sunlifei
* 时间:2011-8-16 15:42:53
*/
package com.bhne.web.util;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
/**
* 功能:将汉字转化为拼音
* @author sunlifei
* 时间: 2011-8-16 15:42:53
*/
public class CnToSpell {
private static LinkedHashMap spellMap = null;
static{
? if(spellMap == null){
? spellMap = new LinkedHashMap(400);
? }
? initialize();
? }
@SuppressWarnings("unchecked")
private static void spellPut(String spell,int ascii){
spellMap.put(spell,new Integer(ascii));
}
private static void initialize(){
??? spellPut("a", -20317);
??? spellPut("ai", -20304);
??? spellPut("an", -20295);
spellPut("ang", -20292);
spellPut("ao", -20283);
spellPut("ba", -20265);
spellPut("bai", -20257);
spellPut("ban", -20242);
??? spellPut("bang", -20230);
??? spellPut("bao", -20051);
??? spellPut("bei", -20036);
spellPut("ben", -20032);
spellPut("beng", -20026);软件开发
spellPut("bi", -20002);
??? spellPut("bian", -19990);
spellPut("biao", -19986);
spellPut("bie", -19982);
spellPut("bin", -19976);
??? spellPut("bing", -19805);
??? spellPut("bo", -19784);
??? spellPut("bu", -19775);
???
??? spellPut("ca", -19774);
??? spellPut("cai", -19763);
??? spellPut("can", -19756);
spellPut("cang", -19751);
spellPut("cao", -19746);
spellPut("ce", -19741);
spellPut("ceng", -19739);
spellPut("cha", -19728);
spellPut("chai", -19725);
??? spellPut("chan", -19715);
??? spellPut("chang", -19540);
??? spellPut("chao", -19531);
??? spellPut("che", -19525);
??? spellPut("chen", -19515);
??? spellPut("cheng", -19500);
spellPut("chi", -19484);
spellPut("chong", -19479);
spellPut("chou", -19467);
??? spellPut("chu", -19289);
spellPut("chuai", -19288);
spellPut("chuan", -19281);
spellPut("chuang", -19275);
spellPut("chui", -19270);
spellPut("chun", -19263);
spellPut("chuo", -19261);
spellPut("ci", -19249);
spellPut("cong", -19243);
spellPut("cou", -19242);
spellPut("cu", -19238);
spellPut("cuan", -19235);
spellPut("cui", -19227);
spellPut("cun", -19224);
spellPut("cuo", -19218);
spellPut("da", -19212);
spellPut("dai", -19038);
spellPut("dan", -19023);
spellPut("dang", -19018);
spellPut("dao", -19006);
spellPut("de", -19003);
spellPut("deng", -18996);
spellPut("di", -18977);
spellPut("dian", -18961);
spellPut("diao", -18952);
spellPut("die", -18783);
spellPut("ding", -18774);
spellPut("diu", -18773);
spellPut("dong", -18763);
spellPut("dou", -18756);
spellPut("du", -18741);
spellPut("duan", -18735);
spellPut("dui", -18731);
??? spellPut("dun", -18722);
??? spellPut("duo", -18710);
???
??? spellPut("e", -18697);
??? spellPut("en", -18696);
??? spellPut("er", -18526);
??? spellPut("fa", -18518);
??? spellPut("fan", -18501);
??? spellPut("fang", -18490);
??? spellPut("fei", -18478);
??? spellPut("fen", -18463);
??? spellPut("feng", -18448); ???
??? spellPut("fo", -18447);
??? spellPut("fou", -18446); ???
??? spellPut("fu", -18239);
???
spellPut("ga", -18237);
spellPut("gai", -18231);
??? spellPut("gan", -18220);
??? spellPut("gang", -18211);
spellPut("gao", -18201);
spellPut("ge", -18184);
spellPut("gei", -18183);
??? spellPut("gen", -18181);
??? spellPut("geng", -18012);
??? spellPut("gong", -17997);
??? spellPut("gou", -17988);
spellPut("gu", -17970);
spellPut("gua", -17964);
spellPut("guai", -17961);
spellPut("guan", -17950);
spellPut("guang", -17947);
spellPut("gui", -17931);
spellPut("gun", -17928);
spellPut("guo", -17922);
spellPut("ha", -17759);
spellPut("hai", -17752);
spellPut("han", -17733);
spellPut("hang", -17730);
??? spellPut("hao", -17721);
??? spellPut("he", -17703);
??? spellPut("hei", -17701);
??? spellPut("hen", -17697);
??? spellPut("heng", -17692);
??? spellPut("hong", -17683);
??? spellPut("hou", -17676);
??? spellPut("hu", -17496);
??? spellPut("hua", -17487);
??? spellPut("huai", -17482);
??? spellPut("huan", -17468);
??? spellPut("huang", -17454);
??? spellPut("hui", -17433);
??? spellPut("hun", -17427);
??? spellPut("huo", -17417);
???
??? spellPut("ji", -17202);
??? spellPut("jia", -17185);
??? spellPut("jian", -16983);
spellPut("jiang", -16970);
spellPut("jiao", -16942);
??? spellPut("jie", -16915);
??? spellPut("jin", -16733);
??? spellPut("jing", -16708);
??? spellPut("jiong", -16706);
spellPut("jiu", -16689);
??? spellPut("ju", -16664);
??? spellPut("juan", -16657);
??? spellPut("jue", -16647);
??? spellPut("jun", -16474);
???
??? spellPut("ka", -16470);
??? spellPut("kai", -16465);
spellPut("kan", -16459);
spellPut("kang", -16452);
??? spellPut("kao", -16448);
??? spellPut("ke", -16433);
??? spellPut("ken", -16429);
??? spellPut("keng", -16427);
??? spellPut("kong", -16423);
??? spellPut("kou", -16419);
??? spellPut("ku", -16412);
??? spellPut("kua", -16407);
??? spellPut("kuai", -16403);
??? spellPut("kuan", -16401);
??? spellPut("kuang", -16393);
??? spellPut("kui", -16220);
??? spellPut("kun", -16216);
??? spellPut("kuo", -16212);
???
??? spellPut("la", -16205);
??? spellPut("lai", -16202);
spellPut("lan", -16187);
spellPut("lang", -16180);
spellPut("lao", -16171);
spellPut("le", -16169);
spellPut("lei", -16158);
spellPut("leng", -16155);
spellPut("li", -15959);
spellPut("lia", -15958);
spellPut("lian", -15944);
spellPut("liang", -15933);
spellPut("liao", -15920);
spellPut("lie", -15915);
spellPut("lin", -15903);
spellPut("ling", -15889);
spellPut("liu", -15878);
spellPut("long", -15707);
spellPut("lou", -15701);
spellPut("lu", -15681);
spellPut("lv", -15667);
spellPut("luan", -15661);
spellPut("lue", -15659);
??? spellPut("lun", -15652);
??? spellPut("luo", -15640);
???
??? spellPut("ma", -15631);
??? spellPut("mai", -15625);
??? spellPut("man", -15454);
??? spellPut("mang", -15448);
??? spellPut("mao", -15436);
??? spellPut("me", -15435);
??? spellPut("mei", -15419);
??? spellPut("men", -15416);
??? spellPut("meng", -15408);
??? spellPut("mi", -15394);
??? spellPut("mian", -15385);
??? spellPut("miao", -15377);
spellPut("mie", -15375);
spellPut("min", -15369);
spellPut("ming", -15363);
spellPut("miu", -15362);
spellPut("mo", -15183);
spellPut("mou", -15180);
spellPut("mu", -15165);
spellPut("na", -15158);
spellPut("nai", -15153);
spellPut("nan", -15150);
spellPut("nang", -15149);
spellPut("nao", -15144);
spellPut("ne", -15143);
spellPut("nei", -15141);
spellPut("nen", -15140);
spellPut("neng", -15139);
spellPut("ni", -15128);
spellPut("nian", -15121);
??? spellPut("niang", -15119);
??? spellPut("niao", -15117);
??? spellPut("nie", -15110);
??? spellPut("nin", -15109);
??? spellPut("ning", -14941);
??? spellPut("niu", -14937);
??? spellPut("nong", -14933);
??? spellPut("nu", -14930);
??? spellPut("nv", -14929);
??? spellPut("nuan", -14928);
??? spellPut("nue", -14926);
??? spellPut("nuo", -14922);
???
??? spellPut("o", -14921);
??? spellPut("ou", -14914);
???
??? spellPut("pa", -14908);
??? spellPut("pai", -14902);
??? spellPut("pan", -14894);
??? spellPut("pang", -14889);
spellPut("pao", -14882);
??? spellPut("pei", -14873);
??? spellPut("pen", -14871);
??? spellPut("peng", -14857);
??? spellPut("pi", -14678);
??? spellPut("pian", -14674);
??? spellPut("piao", -14670);
??? spellPut("pie", -14668);
??? spellPut("pin", -14663);
??? spellPut("ping", -14654);
??? spellPut("po", -14645);
spellPut("pu", -14630);
??? spellPut("qi", -14594);
??? spellPut("qia", -14429);
??? spellPut("qian", -14407);
??? spellPut("qiang", -14399);
??? spellPut("qiao", -14384);
??? spellPut("qie", -14379);
??? spellPut("qin", -14368);
??? spellPut("qing", -14355);
??? spellPut("qiong", -14353);
??? spellPut("qiu", -14345);
??? spellPut("qu", -14170);
??? spellPut("quan", -14159);
??? spellPut("que", -14151);
??? spellPut("qun", -14149);
???
??? spellPut("ran", -14145);
??? spellPut("rang", -14140);
??? spellPut("rao", -14137);
??? spellPut("re", -14135);
spellPut("ren", -14125);
spellPut("reng", -14123);
spellPut("ri", -14122);
spellPut("rong", -14112);
spellPut("rou", -14109);
spellPut("ru", -14099);
spellPut("ruan", -14097);
??? spellPut("rui", -14094);
spellPut("run", -14092);
spellPut("ruo", -14090);
spellPut("sa", -14087);
spellPut("sai", -14083);
spellPut("san", -13917);
spellPut("sang", -13914);
spellPut("sao", -13910);
spellPut("se", -13907);
spellPut("sen", -13906);
spellPut("seng", -13905);
spellPut("sha", -13896);
spellPut("shai", -13894);
spellPut("shan", -13878);
spellPut("shang", -13870);
spellPut("shao", -13859);
spellPut("she", -13847);
spellPut("shen", -13831);
spellPut("sheng", -13658);
spellPut("shi", -13611);
spellPut("shou", -13601);
spellPut("shu", -13406);
??? spellPut("shua", -13404);
??? spellPut("shuai", -13400);
??? spellPut("shuan", -13398);
??? spellPut("shuang", -13395);
??? spellPut("shui", -13391);
??? spellPut("shun", -13387);
??? spellPut("shuo", -13383);
??? spellPut("si", -13367);
??? spellPut("song", -13359);
??? spellPut("sou", -13356);
??? spellPut("su", -13343);
??? spellPut("suan", -13340);
??? spellPut("sui", -13329);
??? spellPut("sun", -13326);
??? spellPut("suo", -13318);
???
??? spellPut("ta", -13147);
??? spellPut("tai", -13138);
??? spellPut("tan", -13120);
??? spellPut("tang", -13107);
spellPut("tao", -13096);
spellPut("te", -13095);
spellPut("teng", -13091);
spellPut("ti", -13076);
spellPut("tian", -13068);
spellPut("tiao", -13063);
spellPut("tie", -13060);
spellPut("ting", -12888);
spellPut("tong", -12875);
spellPut("tou", -12871);
spellPut("tu", -12860);
spellPut("tuan", -128590);
spellPut("tui", -12852);
spellPut("tun", -12849);
spellPut("tuo", -12838);
spellPut("wa", -12831);
spellPut("wai", -12829);
spellPut("wan", -12812);
spellPut("wang", -12802);
spellPut("wei", -12607);
spellPut("wen", -12597);
spellPut("weng", -12594);
spellPut("wo", -12585);
spellPut("wu", -12556);
spellPut("xi", -12359);
spellPut("xia", -12346);
spellPut("xian", -12320);
spellPut("xiang", -12300);
spellPut("xiao", -12120);
spellPut("xie", -12099);
spellPut("xin", -12089);
spellPut("xing", -12074);
spellPut("xiong", -12067);
spellPut("xiu", -12058);
spellPut("xu", -12039);
spellPut("xuan", -11867);
spellPut("xue", -11861);
spellPut("xun", -11847);
spellPut("ya", -11831);
spellPut("yan", -11798);
spellPut("yang", -11781);
spellPut("yao", -11604);
spellPut("ye", -11589);
spellPut("yi", -11536);
spellPut("yin", -11358);
spellPut("ying", -11340);
spellPut("yo", -11339);
spellPut("yong", -11324);
spellPut("you", -11303);
spellPut("yu", -11097);
spellPut("yuan", -11077);
spellPut("yue", -11067);
spellPut("yun", -11055);
spellPut("za", -11052);
spellPut("zai", -11045);
spellPut("zan", -11041);
spellPut("zang", -11038);
spellPut("zao", -11024);
spellPut("ze", -11020);
spellPut("zei", -11019);
spellPut("zen", -11018);
spellPut("zeng", -11014);
spellPut("zha", -110838);
spellPut("zhai", -10832);
spellPut("zhan", -10815);
spellPut("zhang", -10800);
spellPut("zhao", -10790);
spellPut("zhe", -10780);
spellPut("zhen", -10764);
spellPut("zheng", -10587);
spellPut("zhi", -10544);
spellPut("zhong", -10533);
spellPut("zhou", -10519);
??? spellPut("zhu", -10331);
??? spellPut("zhua", -10329);
??? spellPut("zhuai", -10328);
??? spellPut("zhuan", -10322);
spellPut("zhuang", -10315);货运专家
spellPut("zhui", -10309);
spellPut("zhun", -10307);
spellPut("zhuo", -10296);
spellPut("zi", -10281);
spellPut("zong", -10274);
spellPut("zou", -10270);
spellPut("zu", -10262);
spellPut("zuan", -10260);
spellPut("zui", -10256);
spellPut("zun", -10254);
spellPut("zuo", -10248);
? }
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){ //英文字符 s(g`&@`4mu0h
????? return bytes[0];
??? }
???
??? if(bytes.length == 2){ //中文字符
??? int hightByte = 256 + bytes[0];
??? int lowByte = 256 + bytes[1];
??? int ascii = (256 * hightByte + lowByte) - 256 * 256;
??? return ascii;
??? }
??? return 0; //错误
}
public static String getSpellByAscii(int ascii){
if(ascii > 0 && ascii < 160){ //单字符
return String.valueOf((char)ascii);
}
??? if(ascii < -20319 || ascii > -10247){ //不知道的字符,lY!{ m c3xK/R0o%F
??? return null;
??? }
??? Set keySet = spellMap.keySet();
??? Iterator it = keySet.iterator();
??? String spell0 = null;
??? String spell = null;
??? int asciiRang0 = -20319;
??? int asciiRang = 0;
??? 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){ //区间找到'r ES c!l"M0PN%X-q)G!z
??? return(spell0 == null) ? spell : spell0;
??? }
??? }else{
????????? spell0 = spell;
????????? asciiRang0 = asciiRang;
?????? }
??? }
??? return null;
}
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);
??? }
??? }
??? }
??? return retuBuf.toString();
}
?????? public static void main(String[] args) {
String str = "做 个 好 孩 子";
System.out.println(CnToSpell.getFullSpell(str));
}
} 1 楼 charyle 2011-08-18 自己弄的,看看这个吧http://charyle.iteye.com/blog/1050392