impor java.util.collections; impor java.util.iterator; impor java.util.linkedhashmap; impor java.util.map; / ** * 将汉字转化为全拼 */ kelas publik cntospell1 {peta statis privat <string, integer> mantra = null; // 存放生僻字和其拼音的 peta peta statis pribadi <karakter, string> Unmommonwordsmap = null; static {if (mantra == null) {mantra = collections.synchronizedMap (LinkedHashMap baru <string, integer> (396)); UnmommonWordSmap = collections.synchronizedMap (LinkedHashMap baru <karakter, String> (200)); } initialize (); inituncommonwords (); } private cntospell1 () {} / ** * 初始化 * / private static void initialize () {mantraMap.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 ("fan", -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 ("geng", -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 ("mang", -15454); spellmap.put ("mao", -15448); spellmap.put ("me", -15436); spellmap.put ("mei", -15435); spellmap.put ("pria", -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 ("pena", -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 ("qong", -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 ("dia", -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 ("lagu", -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 ("you", -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 mantra 生僻字的拼音, 如果拼音以 a, o, e 开头 , 请将开头分别改为 'a,' o, 'e , 如 :安 ('an) */ public static void putuncommonword (char cnword, string mantra) {uncommonwordsmap.put (cnword, mantra); } / ** * 初始化生僻字 * / private static 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 ('翮', "he"); putuncommonword ('', "he"); putuncommonword ('', "Huan"); putuncommonword ('', "hui"); putuncommonword ('', "Jie"); putuncommonword ('', "jing"); putuncommonword ('', "jing"); putuncommonword ('', "jing"); putuncommonword ('', "Kun"); putuncommonword ('琨', "kun"); putuncommonword ('', "Kun"); putuncommonword ('骊', "li"); putuncommonword ('鎏', "liu"); putuncommonword ('', "man"); 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 ('莜', "you"); 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) {// 英文字符 return bytes [0]; } if (bytes.length == 2) {// 中文字符 int hightbyte = 256 + bytes [0]; int lowbyte = 256 + byte [1]; kembali (256 * hightbyte + lowbyte) - 256 * 256; // 返回 ascii} return 0; // 错误} /** * 根据 ascii 码到 mantra 中查找对应的拼音 * @param ascii ascii * @return ascii 对应的拼音, 如果 ascii 对应的字符为单字符 , ,, 如果不是单字符且在 Spellmap 中没找到对应的拼音 , 则返回空字符串 (""), */ String statis publik getSpellbyascii (int ascii) {if (ASCII> 0 && ASCII <160) {// 单字符 Return String. nilai ((char) ASCII); } if (ASCII <-20319 || ASCII> -10247) {// 不知道的字符 return ""; } String mantra = null; // Key Integer Asciirang; // nilai string spellprevious = null; // 用来保存上次轮循环的 kunci int asciirangprevious = -20319; // 用来保存上一次循环的 nilai untuk (iterator it = mantra.keyset (). Iterator (); it.hasnext ();) {mantra = (string) it.next (); // 拼音 asciirang = spellmap.get (mantra); // 拼音的 ascii if (asciirang! = Null) {if (ascii> = asciirangprevious && ascii <asciirang) {// 区间找到, 返回对应的拼音 return (spellprevious == null)? Mantra: Spellprevious; } else {spellprevious = mantra; asciirangprevious = asciirang; } } } kembali ""; } /** * 获取字符串的全拼或首拼, 是汉字则转化为对应的拼音或拼音首字母, 其它字符不进行转换 * @param cnstr 要获取全拼或首拼的字符串 * @param OnlyFirstspell 是否只获取首拼 , 为 true 时 , 只获取首拼 , 为 false 时 , 获取全拼 * @return string cnstr 的全拼或首拼, 如果 cnstr 为 null 时, 返回 "" */ string statis public getspell (String CNSTR, Boolean OnlyFirstSpell) {if (cnstr == null) {return ""; } char [] chars = cnstr.trim (). tochararray (); StringBuffer SB = StringBuffer baru (); untuk (int i = 0, len = chars.length; i <len; i ++) {int ascii = getcnascii (chars [i]); if (ASCII == 0) {// 如果获取汉字的 ASCII 出错 , 则不进行转换 SB.Append (chars [i]); } else {string mantra = getspellbyascii (ASCII); // 根据 ASCII 取拼音 if (mantra == null || mantra.length () == 0) {// 如果根据 ascii 取拼音没取到 , 则到生僻字 则到生僻字 peta 中取 mantra = tidak umumwordsmap.get (chars [ Saya]); } if (mantra == null || spell.length () == 0) {// 如果没有取到对应的拼音 , 则不做转换 , 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 追加原字符 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ia; } else {if (OnlyFirstspell) {sb.append (mantra.startswith ("'")? mantra.substring (1, 2): mantra.substring (0, 1)); } else {sb.append (mantra); }}}} // akhir untuk pengembalian sb.toString (); } public static void main (string [] args) {string [] s = {"获取汉字全拼和首拼测试", "Ini adalah tes", "a, b; c [d]", "标 , ,点。 "," 圆角数字123 , 特殊符号・¥%―end …… "," 繁体字 : 西安 "," 西安 "," 山 "}; untuk (int i = 0; i <s.length; 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"); }}}