importar java.util.Collections; importar java.util.iterator; importar java.util.linkedhashmap; importar java.util.map; / ** * 将汉字转化为全拼 */ classe pública CtosSpell1 {Map estático privado <string, número inteiro> spellmap = null; // 存放生僻字和其拼音的 mapear mapa estático privado <caractere, string> Uncommonwordsmap = null; static {if (spellmap == null) {spellmap = collection.synchronizedmap (new LinkedHashmap <string, número inteiro> (396)); UncommonwordsMap = Coleções.synchronizedmap (new LinkedHashmap <caractere, String> (200)); } inicialize (); inituncommonwords (); } private cntosppell1 () {} / ** * 初始化 * / private estático void inicialize () {spellmap.put ("'a", -20319); spellmap.put ("'ai", -20317); spellmap.put ("'an", -20304); spellmap.put ("'ang", -20295); spellmap.put ("'ao", -20292); spellmap.put ("ba", -20283); spellmap.put ("bai", -20265); spellmap.put ("ban", -20257); spellmap.put ("bang", -20242); spellmap.put ("bao", -20230); spellmap.put ("BEI", -20051); spellmap.put ("ben", -20036); spellmap.put ("beng", -20032); spellmap.put ("bi", -20026); spellmap.put ("bian", -20002); spellmap.put ("biao", -19990); spellmap.put ("bie", -19986); spellmap.put ("bin", -19982); spellmap.put ("bing", -19976); spellmap.put ("bo", -19805); spellmap.put ("bu", -19784); spellmap.put ("CA", -19775); spellmap.put ("CAI", -19774); spellmap.put ("Can", -19763); spellmap.put ("CANG", -19756); spellmap.put ("cao", -19751); spellmap.put ("CE", -19746); spellmap.put ("ceng", -19741); spellmap.put ("Cha", -19739); spellmap.put ("chai", -19728); spellmap.put ("chan", -19725); spellmap.put ("chang", -19715); spellmap.put ("Chao", -19540); spellmap.put ("CHE", -19531); spellmap.put ("chen", -19525); spellmap.put ("Cheng", -19515); spellmap.put ("chi", -19500); spellmap.put ("chong", -19484); spellmap.put ("chou", -19479); spellmap.put ("chu", -19467); spellmap.put ("chuai", -19289); spellmap.put ("Chuan", -19288); spellmap.put ("chuang", -19281); spellmap.put ("chui", -19275); spellmap.put ("chun", -19270); spellmap.put ("Chuo", -19263); spellmap.put ("CI", -19261); spellmap.put ("Cong", -19249); spellmap.put ("cou", -19243); spellmap.put ("Cu", -19242); spellmap.put ("cuan", -19238); spellmap.put ("cui", -19235); spellmap.put ("CUN", -19227); spellmap.put ("CuO", -19224); spellmap.put ("da", -19218); spellmap.put ("DAI", -19212); spellmap.put ("Dan", -19038); spellmap.put ("Dang", -19023); spellmap.put ("dao", -19018); spellmap.put ("de", -19006); spellmap.put ("Deng", -19003); spellmap.put ("di", -18996); spellmap.put ("Dian", -18977); spellmap.put ("diao", -18961); spellmap.put ("Die", -18952); spellmap.put ("Ding", -18783); spellmap.put ("DIU", -18774); spellmap.put ("dong", -18773); spellmap.put ("DOU", -18763); spellmap.put ("du", -18756); spellmap.put ("duan", -18741); spellmap.put ("DUI", -18735); spellmap.put ("dun", -18731); spellmap.put ("duo", -18722); spellmap.put ("'e", -18710); spellmap.put ("'EN", -18697); spellmap.put ("'er", -18696); spellmap.put ("fa", -18526); spellmap.put ("fã", -18518); spellmap.put ("Fang", -18501); spellmap.put ("fei", -18490); spellmap.put ("fen", -18478); spellmap.put ("feng", -18463); spellmap.put ("fo", -18448); spellmap.put ("fou", -18447); spellmap.put ("fu", -18446); spellmap.put ("GA", -18239); spellmap.put ("gai", -18237); spellmap.put ("gan", -18231); spellmap.put ("Gang", -18220); spellmap.put ("gao", -18211); spellmap.put ("ge", -18201); spellmap.put ("GEI", -18184); spellmap.put ("gen", -18183); spellmap.put ("geng", -18181); spellmap.put ("gong", -18012); spellmap.put ("GOU", -17997); spellmap.put ("gu", -17988); spellmap.put ("gua", -17970); spellmap.put ("Guai", -17964); spellmap.put ("Guan", -17961); spellmap.put ("Guang", -17950); spellmap.put ("GUI", -17947); spellmap.put ("Gun", -17931); spellmap.put ("guo", -17928); spellmap.put ("ha", -17922); spellmap.put ("hai", -17759); spellmap.put ("han", -17752); spellmap.put ("Hang", -17733); spellmap.put ("Hao", -17730); spellmap.put ("He", -17721); spellmap.put ("HEI", -17703); spellmap.put ("Hen", -17701); spellmap.put ("heng", -17697); spellmap.put ("Hong", -17692); spellmap.put ("hou", -17683); spellmap.put ("hu", -17676); spellmap.put ("hua", -17496); spellmap.put ("huai", -17487); spellmap.put ("huan", -17482); spellmap.put ("huang", -17468); spellmap.put ("hui", -17454); spellmap.put ("hun", -17433); spellmap.put ("huo", -17427); spellmap.put ("ji", -17417); spellmap.put ("jia", -17202); spellmap.put ("jian", -17185); spellmap.put ("jiang", -16983); spellmap.put ("jiao", -16970); spellmap.put ("jie", -16942); spellmap.put ("jin", -16915); spellmap.put ("jing", -16733); spellmap.put ("Jiong", -16708); spellmap.put ("jiu", -16706); spellmap.put ("JU", -16689); spellmap.put ("juan", -16664); spellmap.put ("JUE", -16657); spellmap.put ("Jun", -16647); spellmap.put ("ka", -16474); spellmap.put ("kai", -16470); spellmap.put ("kan", -16465); spellmap.put ("kang", -16459); spellmap.put ("kao", -16452); spellmap.put ("ke", -16448); spellmap.put ("Ken", -16433); spellmap.put ("Keng", -16429); spellmap.put ("Kong", -16427); spellmap.put ("kou", -16423); spellmap.put ("ku", -16419); spellmap.put ("kua", -16412); spellmap.put ("kuai", -16407); spellmap.put ("kuan", -16403); spellmap.put ("Kuang", -16401); spellmap.put ("kui", -16393); spellmap.put ("Kun", -16220); spellmap.put ("kuo", -16216); spellmap.put ("la", -16212); spellmap.put ("lai", -16205); spellmap.put ("LAN", -16202); spellmap.put ("lang", -16187); spellmap.put ("Lao", -16180); spellmap.put ("LE", -16171); spellmap.put ("Lei", -16169); spellmap.put ("Leng", -16158); spellmap.put ("li", -16155); spellmap.put ("lia", -15959); spellmap.put ("lian", -15958); spellmap.put ("liang", -15944); spellmap.put ("liao", -15933); spellmap.put ("Lie", -15920); spellmap.put ("lin", -15915); spellmap.put ("Ling", -15903); spellmap.put ("Liu", -15889); spellmap.put ("long", -15878); spellmap.put ("Lou", -15707); spellmap.put ("Lu", -15701); spellmap.put ("lv", -15681); spellmap.put ("luan", -15667); spellmap.put ("lue", -15661); spellmap.put ("lun", -15659); spellmap.put ("luo", -15652); spellmap.put ("MA", -15640); spellmap.put ("mai", -15631); spellmap.put ("Man", -15625); spellmap.put ("mag", -15454); spellmap.put ("mao", -15448); spellmap.put ("me", -15436); spellmap.put ("Mei", -15435); spellmap.put ("homens", -15419); spellmap.put ("Meng", -15416); spellmap.put ("mi", -15408); spellmap.put ("mian", -15394); spellmap.put ("miao", -15385); spellmap.put ("mie", -15377); spellmap.put ("min", -15375); spellmap.put ("Ming", -15369); spellmap.put ("miu", -15363); spellmap.put ("MO", -15362); spellmap.put ("MOU", -15183); spellmap.put ("mu", -15180); spellmap.put ("Na", -15165); spellmap.put ("nai", -15158); spellmap.put ("nan", -15153); spellmap.put ("nang", -15150); spellmap.put ("Nao", -15149); spellmap.put ("ne", -15144); spellmap.put ("nei", -15143); spellmap.put ("nen", -15141); spellmap.put ("Neng", -15140); spellmap.put ("ni", -15139); spellmap.put ("nian", -15128); spellmap.put ("niang", -15121); spellmap.put ("niao", -15119); spellmap.put ("nie", -15117); spellmap.put ("nin", -15110); spellmap.put ("ning", -15109); spellmap.put ("NIU", -14941); spellmap.put ("Nong", -14937); spellmap.put ("Nu", -14933); spellmap.put ("nv", -14930); spellmap.put ("Nuan", -14929); spellmap.put ("nue", -14928); spellmap.put ("Nuo", -14926); spellmap.put ("'O", -14922); spellmap.put ("'Ou", -14921); spellmap.put ("PA", -14914); spellmap.put ("PAI", -14908); spellmap.put ("pan", -14902); spellmap.put ("Pang", -14894); spellmap.put ("pao", -14889); spellmap.put ("PEI", -14882); spellmap.put ("PEN", -14873); spellmap.put ("Peng", -14871); spellmap.put ("pi", -14857); spellmap.put ("pian", -14678); spellmap.put ("piao", -14674); spellmap.put ("PIE", -14670); spellmap.put ("Pin", -14668); spellmap.put ("ping", -14663); spellmap.put ("PO", -14654); spellmap.put ("PU", -14645); spellmap.put ("qi", -14630); spellmap.put ("Qia", -14594); spellmap.put ("qian", -14429); spellmap.put ("Qiang", -14407); spellmap.put ("Qiao", -14399); spellmap.put ("qie", -14384); spellmap.put ("Qin", -14379); spellmap.put ("Qing", -14368); spellmap.put ("Qiong", -14355); spellmap.put ("qiu", -14353); spellmap.put ("Qu", -14345); spellmap.put ("Quan", -14170); spellmap.put ("Que", -14159); spellmap.put ("Qun", -14151); spellmap.put ("ran", -14149); spellmap.put ("Rang", -14145); spellmap.put ("rao", -14140); spellmap.put ("re", -14137); spellmap.put ("Ren", -14135); spellmap.put ("reng", -14125); spellmap.put ("ri", -14123); spellmap.put ("rong", -14122); spellmap.put ("rou", -14112); spellmap.put ("ru", -14109); spellmap.put ("Ruan", -14099); spellmap.put ("rui", -14097); spellmap.put ("Run", -14094); spellmap.put ("ruo", -14092); spellmap.put ("SA", -14090); spellmap.put ("SAI", -14087); spellmap.put ("SAN", -14083); spellmap.put ("Sang", -13917); spellmap.put ("SAO", -13914); spellmap.put ("SE", -13910); spellmap.put ("sen", -13907); spellmap.put ("Seng", -13906); spellmap.put ("sha", -13905); spellmap.put ("shai", -13896); spellmap.put ("Shan", -13894); spellmap.put ("shang", -13878); spellmap.put ("shao", -13870); spellmap.put ("ela", -13859); spellmap.put ("Shen", -13847); spellmap.put ("Sheng", -13831); spellmap.put ("shi", -13658); spellmap.put ("shou", -13611); spellmap.put ("shu", -13601); spellmap.put ("shua", -13406); spellmap.put ("shuai", -13404); spellmap.put ("shuan", -13400); spellmap.put ("shuang", -13398); spellmap.put ("shui", -13395); spellmap.put ("Shun", -13391); spellmap.put ("shuo", -13387); spellmap.put ("Si", -13383); spellmap.put ("Song", -13367); spellmap.put ("Sou", -13359); spellmap.put ("su", -13356); spellmap.put ("suan", -13343); spellmap.put ("sui", -13340); spellmap.put ("Sun", -13329); spellmap.put ("suo", -13326); spellmap.put ("ta", -13318); spellmap.put ("tai", -13147); spellmap.put ("tan", -13138); spellmap.put ("tang", -13120); spellmap.put ("Tao", -13107); spellmap.put ("TE", -13096); spellmap.put ("Teng", -13095); spellmap.put ("ti", -13091); spellmap.put ("tian", -13076); spellmap.put ("tiao", -13068); spellmap.put ("tie", -13063); spellmap.put ("Ting", -13060); spellmap.put ("Tong", -12888); spellmap.put ("TOU", -12875); spellmap.put ("tu", -12871); spellmap.put ("tuan", -12860); spellmap.put ("tui", -12858); spellmap.put ("tun", -12852); spellmap.put ("Tuo", -12849); spellmap.put ("wa", -12838); spellmap.put ("wai", -12831); spellmap.put ("wan", -12829); spellmap.put ("wang", -12812); spellmap.put ("wei", -12802); spellmap.put ("wen", -12607); spellmap.put ("weng", -12597); spellmap.put ("wo", -12594); spellmap.put ("wu", -12585); spellmap.put ("xi", -12556); spellmap.put ("xia", -12359); spellmap.put ("xian", -12346); spellmap.put ("xiang", -12320); spellmap.put ("xiao", -12300); spellmap.put ("xie", -12120); spellmap.put ("xin", -12099); spellmap.put ("xing", -12089); spellmap.put ("xiong", -12074); spellmap.put ("xiu", -12067); spellmap.put ("xu", -12058); spellmap.put ("xuan", -12039); spellmap.put ("xue", -11867); spellmap.put ("xun", -11861); spellmap.put ("ya", -11847); spellmap.put ("yan", -11831); spellmap.put ("yang", -11798); spellmap.put ("yao", -11781); spellmap.put ("ye", -11604); spellmap.put ("yi", -11589); spellmap.put ("yin", -11536); spellmap.put ("ying", -11358); spellmap.put ("yo", -11340); spellmap.put ("yong", -11339); spellmap.put ("você", -11324); spellmap.put ("yu", -11303); spellmap.put ("yuan", -11097); spellmap.put ("yue", -11077); spellmap.put ("yun", -11067); spellmap.put ("ZA", -11055); spellmap.put ("Zai", -11052); spellmap.put ("zan", -11045); spellmap.put ("Zang", -11041); spellmap.put ("Zao", -11038); spellmap.put ("ze", -11024); spellmap.put ("zei", -11020); spellmap.put ("Zen", -11019); spellmap.put ("Zeng", -11018); spellmap.put ("zha", -11014); spellmap.put ("Zhai", -10838); spellmap.put ("Zhan", -10832); spellmap.put ("Zhang", -10815); spellmap.put ("Zhao", -10800); spellmap.put ("zhe", -10790); spellmap.put ("Zhen", -10780); spellmap.put ("Zheng", -10764); spellmap.put ("zhi", -10587); spellmap.put ("Zhong", -10544); spellmap.put ("zhou", -10533); spellmap.put ("zhu", -10519); spellmap.put ("zhua", -10331); spellmap.put ("zhuai", -10329); spellmap.put ("zhuan", -10328); spellmap.put ("zhuang", -10322); spellmap.put ("zhui", -10315); spellmap.put ("Zhun", -10309); spellmap.put ("zhuo", -10307); spellmap.put ("zi", -10296); spellmap.put ("Zong", -10281); spellmap.put ("zou", -10274); spellmap.put ("zu", -10270); spellmap.put ("zuan", -10262); spellmap.put ("zui", -10260); spellmap.put ("zun", -10256); spellmap.put ("Zuo", -10254); } /** * 添加生僻字 * @param cnword 生僻字 * @param spell 生僻字的拼音, 如果拼音以 a, o, e 开头 , 请将开头分别改为 'a,' o, 'e , 如:安 ('an) */ public static void putuncommword (char cnword, string spell) {Uncommonwordsmap.put (cnword, spell); } / ** * 初始化生僻字 * / private estático void inituncommonwords () {putuncommonword ('', "Ao"); putuncommonword ('' ', "ba"); putuncommonword ('', "ben"); putuncommonword ('', "biao"); putuncommonword ('', "biao"); putuncommonword ('' ', "cha"); putuncommonword ('', "chen"); putuncommonword ('', "cheng"); putuncommonword ('' ', "chi"); putuncommonword ('', "chong"); putuncommonword ('', "chong"); putuncommonword ('', "cu"); putuncommonword ('' ', "cui"); putuncommonword ('', "fang"); putuncommonword ('' ', "feng"); putuncommonword ('', "Gou"); putuncommonword ('焓', "han"); putuncommonword ('', "han"); putuncommonword ('晗', "han"); putuncommonword ('', "han"); putuncommonword ('翮', "ele"); putuncommonword ('', "ele"); putuncommonword ('', "huan"); putuncommonword ('', "hui"); putuncommonword ('', "jie"); putuncommonword ('', "jing"); putuncommonword ('', "jing"); putuncommonword ('', "jing"); putuncommonword ('', "kun"); putuncommonword ('' ', "kun"); putuncommonword ('', "kun"); putuncommonword ('' ', "li"); putuncommonword ('鎏', "liu"); putuncommonword ('', "homem"); putuncommonword ('', "mei"); putuncommonword ('' ', "miao"); putuncommonword ('', "nan"); putuncommonword ('', "nan"); putuncommonword ('' ', "ning"); putuncommonword ('', "ning"); putuncommonword ('', "pa"); putuncommonword ('', "pi"); putuncommonword ('', "qian"); putuncommonword ('', "qin"); putuncommonword ('', "qing"); putuncommonword ('', "qing"); putuncommonword ('', "qing"); putuncommonword ('', "qing"); putuncommonword ('', "qing"); putuncommonword ('' ', "que"); putuncommonword ('', "Que"); putuncommonword ('', "rong"); putuncommonword ('', "Shen"); putuncommonword ('', "Shen"); putuncommonword ('', "Shen"); putuncommonword ('', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('' ', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('' ', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('', "sheng"); putuncommonword ('', "shi"); putuncommonword ('', "su"); putuncommonword ('', "tao"); putuncommonword ('', "tian"); putuncommonword ('' ', "tong"); putuncommonword ('', "wei"); putuncommonword ('' ', "wei"); putuncommonword ('' ', "wei"); putuncommonword ('', "wu"); putuncommonword ('' ', "wu"); putuncommonword ('', "xi"); putuncommonword ('', "xian"); putuncommonword ('', "xiao"); putuncommonword ('', "xiao"); putuncommonword ('' ', "xiao"); putuncommonword ('' ', "xie"); putuncommonword ('' ', "xin"); putuncommonword ('' ', "xiU"); putuncommonword ('', "xu"); putuncommonword ('', "xu"); putuncommonword ('', "xu"); putuncommonword ('', "xuan"); putuncommonword ('' ', "xuan"); putuncommonword ('', "xuan"); putuncommonword ('', "xuan"); putuncommonword ('' ', "xuan"); putuncommonword ('' ', "ya"); putuncommonword ('' ', "yan"); putuncommonword ('' ', "yan"); putuncommonword ('' ', "yan"); putuncommonword ('', "yang"); putuncommonword ('', "yang"); putuncommonword ('', "yang"); putuncommonword ('', "yao"); putuncommonword ('', "yao"); putuncommonword ('' ', "yi"); putuncommonword ('', "yi"); putuncommonword ('', "yi"); putuncommonword ('', "yi"); putuncommonword ('' ', "ying"); putuncommonword ('', "ying"); putuncommonword ('', "ying"); putuncommonword ('', "ying"); putuncommonword ('' ', "ying"); putuncommonword ('', "ying"); putuncommonword ('' ', "você"); putuncommonword ('' ', "yu"); putuncommonword ('', "yun"); putuncommonword ('', "zhe"); putuncommonword ('', "zhe"); putuncommonword ('', "zhi"); } /*** 获得单个汉字的 ASCII. * @param cn 汉字字符 * @return 汉字对应的 ASCII, 错误时返回 0 */ public static int getCnascii (char cn) {byte [] bytes = (string.valueof (cn)). getBytes (); if (bytes == null || bytes.length == 0 || bytes.length> 2) {// 错误 return 0; } if (bytes.length == 1) {// 英文字符 retorna bytes [0]; } if (bytes.length == 2) {// 中文字符 int hightbyte = 256 + bytes [0]; int lowbyte = 256 + bytes [1]; retorno (256 * hightbyte + lowbyte) - 256 * 256; // 返回 ASCII} retornar 0; // 错误} /** * 根据 ASCII 码到 Spellmap 中查找对应的拼音 * @param ascii ascii * @return ascii 对应的拼音, 如果 ascii 对应的字符为单字符 , 则返回对应的单字符, 如果不是单字符且在 Spellmap 中没找到对应的拼音 , 则返回空字符串 (""), */ string estática pública getSpellbyascii (int ascii) {if (ascii> 0 && ascii <160) {// 单字符 String de retorno. valueof ((char) ASCII); } if (ASCII <-20319 || ASCII> -10247) {// 不知道的字符 Return ""; } String spell = null; // chave inteira asciirang; // value string string spellprevious = null; // 用来保存上次轮循环的 key int AsciirangPrevious = -20319; // 用来保存上一次循环的 valor para (iterator it = spellmap.keyset (). Iterator (); it.hasnext ();) {spell = (string) it.next (); // 拼音 Asciirang = spellmap.get (feitiço); // 拼音的 ascii if (asciirang! = Null) {if (ascii> = asciirangprevious && ascii <asciirang) {// 区间找到, 返回对应的拼音 return (spellPrevious == null)? Feitiço: Spellpevious; } else {spellprevious = spell; asciirangprevious = asciirang; } } } retornar ""; } /** * 获取字符串的全拼或首拼, 是汉字则转化为对应的拼音或拼音首字母, 其它字符不进行转换 * @param cnstr 要获取全拼或首拼的字符串 * @param Somentefirstspell 是否只获取首拼 , 为 TRUE 时 , 只获取首拼 , 为 false 时 , 获取全拼 * @return string cnstr 的全拼或首拼, 如果 cnstr 为 null 时, 返回 "" */ string estática pública getspell (String cnstr, boolean apenasfirstspell) {if (cnstr == null) {return ""; } char [] chars = cnstr.Trim (). TOCharArray (); StringBuffer sb = new StringBuffer (); for (int i = 0, len = chars.length; i <len; i ++) {int ascii = getcnascii (chars [i]); if (ascii == 0) {// 如果获取汉字的 ascii 出错 , 则不进行转换 sb.append (chars [i]); } else {string spell = getSpellbyascii (ASCII); // 根据 ascii 取拼音 if (spell == null || spell.length () == 0) {// 如果根据 ascii 取拼音没取到 , 则到生僻字 mapa 中取 feitiço = incomumwordsmap.get (chars [ eu]); } if (spell == null || spell.length () == 0) {// 如果没有取到对应的拼音 , 则不做转换 , 追加原字符 spell = incomumwordsmap.get (chars [i]); } else {if (apenasfirstspell) {sb.append (spell.startswith ("'")? spell.substring (1, 2): spell.substring (0, 1)); } else {sb.append (spell); }}}} // final do retorno sb.toString (); } public static void main (string [] args) {string [] s = {"获取汉字全拼和首拼测试", "este é um teste", "a, b; c [d]", "标 , ,点。 "," 圆角数字123 , 特殊符号・¥% - — - …… "," 繁体字 : : 西安 "," 西安 "," 山 "}; for (int i = 0; i <s.Lengngth; i ++) {long l1 = System.currenttimemillis (); System.out.println (s [i] + "的全拼:" + getspell (s [i], false)); System.out.println (s [i] + "的首拼:" + getspell (s [i], true)); System.out.println ("获取全拼和首拼 共 用了"+(System.CurrentTimemillis ()-L1)+"毫秒/n"); }}}