Classes written in Java that can translate Chinese characters into pinyin

Source: Internet
Author: User

/***//**
######################################## #####################################
# Describe converting Chinese characters into pinyin
# Date 2006-7-12
# Company flx
# Porject Java
######################################## #####################################
*/
Import java. util. iterator;
Import java. util. linkedhashmap;
Import java. util. Set;

Public class cntospell {
Private Static linkedhashmap spellmap = NULL;

Static {
If (spellmap = NULL ){
Spellmap = new linkedhashmap (400 );
}
Initialize ();
System. Out. println ("Chinese transfer spell done .");
}

Private cntospell (){
}

/***//**
* Obtain the ASCII of a single Chinese character.
* @ Param CN char
* Chinese Characters
* @ Return int
* 0 is returned for an error. Otherwise, ASCII is returned.
*/
Public static int getcnascii (char CN ){
Byte [] bytes = (string. valueof (CN). getbytes ();
If (Bytes = NULL | bytes. length> 2 | bytes. Length <= 0) {// Error
Return 0;
}
If (bytes. Length = 1) {// english character
Return bytes [0];
}
If (bytes. Length = 2) {// Chinese Character
Int hightbyte = 256 + bytes [0];
Int lowbyte = 256 + bytes [1];
Int ASCII = (256 * hightbyte + lowbyte)-256*256;
Return ASCII;
}
Return 0; // Error
}

/***//**
* Returns the full spelling of strings, which are converted from Chinese characters to full spelling. Other characters are not converted.
* @ Param cnstr string
* String
* @ Return string
* Convert the string to a fully-assembled 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) {// An error occurred while retrieving 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

Return retubuf. tostring ();
}

/***//**
* Search for the corresponding pinyin in spellmap Based on the ASCII code
* @ Param ASCII int
* ASCII character
* @ Return string
* For pinyin, first determine whether the ASCII value is greater than 0 & <160. If it is a corresponding character,
*
Otherwise, search in spellmap. If no Pinyin is found, null is returned. If no Pinyin is found, Pinyin is returned.
*/
Public static string getspellbyascii (int ascii ){
If (ASCII> 0 & ASCII <160) {// Single Character
Return string. valueof (char) ASCII );
}

If (ASCII <-20319 | ASCII>-10247) {// unknown characters
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) {// interval found
Return (spell0 = NULL )? Spell: spell0;
} Else {
Spell0 = spell;
Asciirang0 = asciirang;
}
}
}

Return NULL;

}

Private Static void initialize (){
Spellput ("A",-20319 );
Spellput ("artificial intelligence",-20317 );
Spellput ("A",-20304 );
Spellput ("Ang",-20295 );
Spellput ("Ao",-20292 );
Spellput ("ba",-20283 );
Spellput ("bai",-20265 );
Spellput ("ban",-20257 );
Spellput ("bang",-20242 );
Spellput ("Bao",-20230 );
Spellput ("Bei",-20051 );
Spellput ("Ben",-1, 20036 );
Spellput ("Beng",-20032 );
Spellput ("bi",-20026 );
Spellput ("Bian",-1, 20002 );
Spellput ("Biao",-19990 );
Spellput ("bie",-19986 );
Spellput ("bin",-19982 );
Spellput ("bing",-19976 );
Spellput ("Bo",-19805 );
Spellput ("bu",-19784 );
Spellput ("ca",-19775 );
Spellput ("Cai",-19774 );
Spellput ("can",-1, 19763 );
Spellput ("Cang",-19756 );
Spellput ("Cao",-19751 );
Spellput ("CE",-19746 );
Spellput ("Ceng",-19741 );
Spellput ("cha",-19739 );
Spellput ("chai",-19728 );
Spellput ("Chan",-19725 );
Spellput ("Chang",-1, 19715 );
Spellput ("Chao",-19540 );
Spellput ("Che",-19531 );
Spellput ("Chen",-19525 );
Spellput ("Cheng",-19515 );
Spellput ("Chi",-19500 );
Spellput ("Chong",-19484 );
Spellput ("Chou",-19479 );
Spellput ("chu",-19467 );
Spellput ("Chuai",-19289 );
Spellput ("Chuan",-19288 );
Spellput ("Chuang",-19281 );
Spellput ("Chui",-19275 );
Spellput ("Chun",-19270 );
Spellput ("Chuo",-19263 );
Spellput ("Ci",-19261 );
Spellput ("Cong",-19249 );
Spellput ("cou",-19243 );
Spellput ("Cu",-19242 );
Spellput ("Cuan",-19238 );
Spellput ("Cui",-19235 );
Spellput ("cun",-19227 );
Spellput ("CuO",-19224 );
Spellput ("da",-19218 );
Spellput ("Dai",-19212 );
Spellput ("dan",-19038 );
Spellput ("dang",-19023 );
Spellput ("Dao",-1, 19018 );
Spellput ("de",-19006 );
Spellput ("Deng",-19003 );
Spellput ("Di",-1, 18996 );
Spellput ("Dian",-18977 );
Spellput ("Diao",-18961 );
Spellput ("die",-1, 18952 );
Spellput ("ding",-18783 );
Spellput ("Diu",-18774 );
Spellput ("Dong",-18773 );
Spellput ("dou",-18763 );
Spellput ("du",-18756 );
Spellput ("Duan",-18741 );
Spellput ("DUI",-18735 );
Spellput ("Dun",-18731 );
Spellput ("duo",-18722 );
Spellput ("e",-18710 );
Spellput ("en",-18697 );
Spellput ("er",-18696 );
Spellput ("Fa",-18526 );
Spellput ("fan",-18518 );
Spellput ("fang",-18501 );
Spellput ("fei",-18490 );
Spellput ("Fen",-18478 );
Spellput ("Feng",-18463 );
Spellput ("FO",-18448 );
Spellput ("fou",-18447 );
Spellput ("Fu",-18446 );
Spellput ("Ga",-18239 );
Spellput ("Gai",-18237 );
Spellput ("Gan",-18231 );
Spellput ("Gang",-18220 );
Spellput ("Gao",-18211 );
Spellput ("Ge",-18201 );
Spellput ("gei",-18184 );
Spellput ("gen",-1, 18183 );
Spellput ("Geng",-18181 );
Spellput ("Gong",-18012 );
Spellput ("gou",-17997 );
Spellput ("Gu",-17988 );
Spellput ("Gua",-17970 );
Spellput ("guai",-17964 );
Spellput ("Guan",-17961 );
Spellput ("Guang",-17950 );
Spellput ("Gui",-17947 );
Spellput ("gun",-17931 );
Spellput ("guo",-17928 );
Spellput ("ha",-17922 );
Spellput ("Hai",-17759 );
Spellput ("Han",-17752 );
Spellput ("hang",-17733 );
Spellput ("hao",-17730 );
Spellput ("he",-17721 );
Spellput ("Hei",-17703 );
Spellput ("success",-17701 );
Spellput ("Heng",-17697 );
Spellput ("Hong",-17692 );
Spellput ("Hou",-17683 );
Spellput ("Hu",-17676 );
Spellput ("Hua",-17496 );
Spellput ("Huai",-17487 );
Spellput ("Huan",-17482 );
Spellput ("Huang",-17468 );
Spellput ("Hui",-17454 );
Spellput ("hun",-17433 );
Spellput ("Huo",-17427 );
Spellput ("Ji",-17417 );
Spellput ("jia",-17202 );
Spellput ("jian",-17185 );
Spellput ("Jiang",-16983 );
Spellput ("Jiao",-16970 );
Spellput ("Jie",-16942 );
Spellput ("Jin",-16915 );
Spellput ("jing",-16733 );
Spellput ("jiong",-16708 );
Spellput ("Jiu",-16706 );
Spellput ("Ju",-16689 );
Spellput ("Juan",-16664 );
Spellput ("Jue",-16657 );
Spellput ("Jun",-16647 );
Spellput ("ka",-16474 );
Spellput ("Kai",-16470 );
Spellput ("kan",-16465 );
Spellput ("Kang",-16459 );
Spellput ("kao",-16452 );
Spellput ("Ke",-16448 );
Spellput ("Ken",-16433 );
Spellput ("Keng",-16429 );
Spellput ("Kong",-16427 );
Spellput ("Kou",-16423 );
Spellput ("ku",-16419 );
Spellput ("Kua",-16412 );
Spellput ("Kuai",-16407 );
Spellput ("Kuan",-16403 );
Spellput ("Kuang",-16401 );
Spellput ("Kui",-16393 );
Spellput ("Kun",-16220 );
Spellput ("Kuo",-16216 );
Spellput ("La",-16212 );
Spellput ("Lai",-16205 );
Spellput ("Lan",-16202 );
Spellput ("Lang",-16187 );
Spellput ("Lao",-16180 );
Spellput ("Le",-16171 );
Spellput ("lei",-16169 );
Spellput ("leng",-16158 );
Spellput ("Li",-16155 );
Spellput ("Lia",-15959 );
Spellput ("Lian",-15958 );
Spellput ("Liang",-15944 );
Spellput ("liao",-15933 );
Spellput ("lie",-1, 15920 );
Spellput ("Lin",-15915 );
Spellput ("Ling",-15903 );
Spellput ("Liu",-15889 );
Spellput ("long",-1, 15878 );
Spellput ("Lou",-15707 );
Spellput ("Lu",-15701 );
Spellput ("LV",-15681 );
Spellput ("Luan",-15667 );
Spellput ("Lue",-15661 );
Spellput ("Lun",-15659 );
Spellput ("Luo",-15652 );
Spellput ("Ma",-15640 );
Spellput ("Mai",-15631 );
Spellput ("man",-1, 15625 );
Spellput ("mang",-15454 );
Spellput ("Mao",-15448 );
Spellput ("me",-15436 );
Spellput ("Mei",-15435 );
Spellput ("man",-15419 );
Spellput ("Meng",-15416 );
Spellput ("mi",-15408 );
Spellput ("Mian",-15394 );
Spellput ("Miao",-15385 );
Spellput ("mini",-15377 );
Spellput ("min",-15375 );
Spellput ("Ming",-15369 );
Spellput ("Miu",-15363 );
Spellput ("Mo",-15362 );
Spellput ("MOU",-15183 );
Spellput ("Mu",-15180 );
Spellput ("Na",-15165 );
Spellput ("Nai",-15158 );
Spellput ("Nan",-15153 );
Spellput ("Nang",-15150 );
Spellput ("Nao",-15149 );
Spellput ("ne",-15144 );
Spellput ("Nei",-15143 );
Spellput ("Nen",-15141 );
Spellput ("Neng",-15140 );
Spellput ("Ni",-15139 );
Spellput ("Nian",-15128 );
Spellput ("Niang",-15121 );
Spellput ("niao",-15119 );
Spellput ("Nie",-15117 );
Spellput ("Nin",-15110 );
Spellput ("Ning",-15109 );
Spellput ("niu",-14941 );
Spellput ("Nong",-14937 );
Spellput ("Nu",-14933 );
Spellput ("NV",-14930 );
Spellput ("Nuan",-14929 );
Spellput ("nue",-14928 );
Spellput ("nuo",-14926 );
Spellput ("O",-14922 );
Spellput ("ou",-14921 );
Spellput ("Pa",-14914 );
Spellput ("Pai",-14908 );
Spellput ("pan",-14902 );
Spellput ("Pang",-14894 );
Spellput ("pao",-14889 );
Spellput ("Pei",-1, 14882 );
Spellput ("pen",-14873 );
Spellput ("peng",-14871 );
Spellput ("Pi",-14857 );
Spellput ("pian",-14678 );
Spellput ("Piao",-14674 );
Spellput ("pie",-14670 );
Spellput ("pin",-14668 );
Spellput ("ping",-14663 );
Spellput ("po",-14654 );
Spellput ("Pu",-14645 );
Spellput ("Qi",-14630 );
Spellput ("QIA",-14594 );
Spellput ("Qian",-14429 );
Spellput ("Qiang",-14407 );
Spellput ("Qiao",-14399 );
Spellput ("Qie",-14384 );
Spellput ("Qin",-14379 );
Spellput ("Qing",-14368 );
Spellput ("Qiong",-14355 );
Spellput ("Qiu",-14353 );
Spellput ("Qu",-14345 );
Spellput ("Quan",-14170 );
Spellput ("que",-14159 );
Speller put ("Qun",-14151 );
Spellput ("ran",-14149 );
Spellput ("rang",-14145 );
Spellput ("Rao",-14140 );
Spellput ("re",-1, 14137 );
Spellput ("Ren",-14135 );
Spellput ("reng",-14125 );
Spellput ("Ri",-14123 );
Spellput ("rong",-14122 );
Spellput ("Rou",-14112 );
Spellput ("Ru",-14109 );
Spellput ("Ruan",-14099 );
Spellput ("Rui",-14097 );
Spellput ("run",-14094 );
Spellput ("RuO",-14092 );
Spellput ("sa",-14090 );
Spellput ("sai",-14087 );
Spellput ("San",-14083 );
Spellput ("sang",-13917 );
Spellput ("Sao",-13914 );
Spellput ("se",-13910 );
Spellput ("Sen",-13907 );
Spellput ("Seng",-13906 );
Spellput ("Sha",-13905 );
Spellput ("Shai",-13896 );
Spellput ("Shan",-13894 );
Spellput ("Shang",-13878 );
Spellput ("shao",-13870 );
Spellput ("she",-13859 );
Spellput ("shen",-13847 );
Spellput ("Sheng",-13831 );
Spellput ("Shi",-13658 );
Spellput ("Shou",-13611 );
Spellput ("Shu",-13601 );
Spellput ("Shua",-13406 );
Spellput ("Shuai",-13404 );
Spellput ("Shuan",-13400 );
Spellput ("Shuang",-13398 );
Spellput ("Shui",-13395 );
Spellput ("Shun",-13391 );
Spellput ("Shuo",-13387 );
Spellput ("Si",-13383 );
Spellput ("song",-13367 );
Spellput ("Sou",-13359 );
Spellput ("Su",-13356 );
Spellput ("Suan",-13343 );
Spellput ("Sui",-13340 );
Spellput ("sun",-13329 );
Spellput ("Suo",-13326 );
Spellput ("ta",-13318 );
Spellput ("Tai",-13147 );
Spellput ("Tan",-13138 );
Spellput ("Tang",-13120 );
Spellput ("Tao",-13107 );
Spellput ("te",-13096 );
Spellput ("Teng",-13095 );
Spellput ("ti",-13091 );
Spellput ("Tian",-13076 );
Spellput ("tiao",-13068 );
Spellput ("Tie",-1, 13063 );
Spellput ("ting",-1, 13060 );
Spellput ("tong",-12888 );
Spellput ("TOU",-12875 );
Spellput ("tu",-12871 );
Spellput ("tuan",-12860 );
Spellput ("Tui",-12858 );
Spellput ("tun",-12852 );
Spellput ("Tuo",-12849 );
Spellput ("wa",-12838 );
Spellput ("Wai",-12831 );
Spellput ("Wan",-12829 );
Spellput ("Wang",-1, 12812 );
Spellput ("wei",-12802 );
Spellput ("Wen",-12607 );
Spellput ("Weng",-12597 );
Spellput ("wo",-12594 );
Spellput ("Wu",-1, 12585 );
Spellput ("Xi",-12556 );
Spellput ("xia",-12359 );
Spellput ("Xian",-12346 );
Spellput ("Xiang",-12320 );
Spellput ("Xiao",-12300 );
Spellput ("xie",-12120 );
Spellput ("Xin",-12099 );
Spellput ("Xing",-12089 );
Spellput ("Xiong",-12074 );
Spellput ("Xiu",-12067 );
Spellput ("Xu",-12058 );
Spellput ("Xuan",-12039 );
Spellput ("Xue",-11867 );
Spellput ("Xun",-11861 );
Spellput ("ya",-11847 );
Spellput ("Yan",-11831 );
Spellput ("yang",-1, 11798 );
Spellput ("Yao",-11781 );
Spellput ("ye",-11604 );
Spellput ("Yi",-11589 );
Spellput ("Yin",-11536 );
Spellput ("ying",-11358 );
Spellput ("yo",-11340 );
Spellput ("Yong",-11339 );
Spellput ("you",-11324 );
Spellput ("yu",-11303 );
Spellput ("Yuan",-11097 );
Spellput ("Yue",-11077 );
Spellput ("Yun",-11067 );
Spellput ("za",-11055 );
Spellput ("Zai",-11052 );
Spellput ("Zan",-11045 );
Spellput ("Zang",-11041 );
Spellput ("Zao",-11038 );
Spellput ("ze",-11024 );
Spellput ("Zei",-11020 );
Spellput ("Zen",-11019 );
Spellput ("Zeng",-11018 );
Spellput ("Zha",-11014 );
Spellput ("Zhai",-10838 );
Spellput ("Zhan",-10832 );
Spellput ("Zhang",-10815 );
Spellput ("Zhao",-10800 );
Spellput ("zhe",-10790 );
Spellput ("Zhen",-10780 );
Spellput ("zheng",-10764 );
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 );
}

Public static void main (string [] ARGs ){
String STR = NULL;
STR = "Xie Hai 101 General Xiyu ";
System. Out. println (STR );
System. Out. println ("spell =" + cntospell. getfullspell (STR ));

STR = "Dancing Claw"., ";
System. Out. println (STR );
System. Out. println ("spell =" + cntospell. getfullspell (STR ));

STR = "La la, shame. ";
System. Out. println (STR );
System. Out. println ("spell =" + cntospell. getfullspell (STR ));

STR = "lard, pig. ";
System. Out. println (STR );
System. Out. println ("spell =" + cntospell. getfullspell (STR ));
}

Private Static void spellput (string spell, int ASCII ){
Spellmap. Put (spell, new INTEGER (ASCII ));
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.