The core code of this article is taken from easydozer's blog: http://blog.csdn.net/easydozer/
Code Description:
Java implements the GUI version for converting Chinese characters to pinyin.
GUI code:
/**
* @ (#) CnToSpellGUI. java
* Kindani
* 2004-10-25 ??
**/
Import java. awt .*;
Import java. awt. event .*;
Import javax. swing .*;
Import javax. swing. event .*;
/**
*
*
-
JDK version
-
1.4
* @ Author KIN
* @ Version 1.0
* @ See
* @ Since 1.0
*/
Public class CnToSpell2GUI extends JFrame {
Private CnToSpell2GUI c = null;
Public CnToSpell2GUI (){
Super ("Cn to Spell ");
SetSize (800,100 );
GetContentPane (). setLayout (new FlowLayout ());
// Component layout
JTextArea from = new JTextArea (5, 20 );
JTextArea to = new JTextArea (5, 20 );
JButton B = new JButton ("cn to pinyin ");
GetContentPane (). add (new JLabel ("From :"));
GetContentPane (). add (from );
GetContentPane (). add (B );
GetContentPane (). add (new JLabel (":"));
GetContentPane (). add ();
// Action handle
B. addActionListener (new Cn2PinyinActionListener (from, ));
SetVisible (true );
// Set this for pack
C = this;
}
/** Button action listener to convert text to pinyin from one textbox to another textbox */
Class Cn2PinyinActionListener implements ActionListener {
Private JTextArea from = null;
Private JTextArea to = null;
Public Cn2PinyinActionListener (JTextArea from, JTextArea ){
This. from = from;
This. to =;
}
Public void actionreceivmed (ActionEvent e ){
If (from. getText (). length () = 0 ){
JOptionPane. showMessageDialog (from, "From text is empty! "," Warning ", JOptionPane. WARNING_MESSAGE );
}
String text = from. getText ();
To. setText (CnToSpell. getFullSpell (text ));
C. pack ();
}
}
Public static void main (String [] args ){
CnToSpell2GUI g = new CnToSpell2GUI ();
}
}
Core code:
Reference from: easydozer blog: http://blog.csdn.net/easydozer/
Http://blog.csdn.net/easydozer/archive/2004/10/20/Chinese2FullSpell.aspx
/**
* @ (#) CnToSpell. java
* Copyright statement Easydozer All Rights Reserved
*
* Revision record:
* 1) modifier: Easydozer
* Time: 2004-10-20
* Description: Create
*/
Package com. easydozer. commons. util;
Import java. util. Iterator;
Import java. util. LinkedHashMap;
Import java. util. Set;
/**
*
Convert Chinese characters to full spelling
*
-
JDK version:
-
1.4
* @ Author Xie Zhisheng
* @ Version 1.0
* @ See
* @ Since 1.0
*/
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 ()
{
}
Private static void spellPut (String spell, int ascii)
{
SpellMap. put (spell, new Integer (ascii ));
}
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 ",-