|
<p >GUI代码部分:<BR>/**<BR> * @(#)CnToSpellGUI.java<BR> * kindani<BR> * 2004-10-25??<BR> * */<BR><BR> import java.awt.*;<BR> import java.awt.event.*;<BR> import javax.swing.*;<BR> import javax.swing.event.*;<BR><BR>/**<BR> * <pre></pre><BR> * <BR><DL><DT><B>JDK版本</B></DT><BR><DD>1.4</DD></DL><BR> * @author KIN<BR> * @version 1.0<BR> * @see<BR> * @since 1.0<BR> */<BR>public class CnToSpell2GUI extends JFrame {<BR><BR> private CnToSpell2GUI c = null;<BR><BR> public CnToSpell2GUI () {<BR> super("Cn to Spell");<BR> setSize(800,100);<BR> getContentPane().setLayout(new FlowLayout());<BR> // component layout<BR> JTextArea from = new JTextArea(5,20);<BR> JTextArea to = new JTextArea(5,20);<BR> JButton b = new JButton("cn to pinyin");<BR> getContentPane().add(new JLabel("From:"));<BR> getContentPane().add(from);<BR> getContentPane().add(b);<BR> getContentPane().add(new JLabel("To:"));<BR> getContentPane().add(to);<BR> // action handle<BR> b.addActionListener(new Cn2PinyinActionListener(from,to));<BR> setVisible(true);<BR> // set this for pack<BR> c = this;<BR> }<BR><BR> /**button action listener to convert text to pinyin from one textbox to another textbox*/<BR> class Cn2PinyinActionListener implements ActionListener{<BR><BR> private JTextArea from = null;<BR> private JTextArea to = null;<BR> public Cn2PinyinActionListener(JTextArea from, JTextArea to) {<BR> this.from = from;<BR> this.to = to;<BR> }<BR> public void actionPerformed(ActionEvent e) {<BR> if (from.getText().length() == 0) {<BR> JOptionPane.showMessageDialog(from,"From text is empty!","Warning",JOptionPane.WARNING_MESSAGE);<BR> }<BR> String text = from.getText();<BR> to.setText(CnToSpell.getFullSpell(text));<BR> c.pack();<BR> }<BR> }<BR><BR> public static void main(String [] args) {<BR> CnToSpell2GUI g = new CnToSpell2GUI();<BR> }<BR>}<BR><BR><BR><BR>------------------------------------------------------------------------<BR>/**<BR> * @(#)CnToSpell.java <BR> * 版权声明 Easydozer 版权所有 违者必究 <BR> *<BR> * 修订记录:<BR> * 1)更改者:Easydozer<BR> * 时 间:2004-10-20 <BR> * 描 述:创建<BR> */<BR>package com.easydozer.commons.util;<BR><BR>import java.util.Iterator;<BR>import java.util.LinkedHashMap;<BR>import java.util.Set;<BR><BR>/**<BR> * <pre>汉字转化为全拼</pre><BR> * <BR><DL><DT><B>JDK版本:</B></DT><BR><DD>1.4</DD></DL><BR> * @author 谢计生<BR> * @version 1.0<BR> * @see <BR> * @since 1.0<BR> */<BR>public class CnToSpell<BR>{<BR> private static LinkedHashMap spellMap = null;<BR><BR> static<BR> {<BR> if(spellMap == null){<BR> spellMap = new LinkedHashMap(400);<BR> }<BR> initialize();<BR> System.out.println("Chinese transfer Spell Done.");<BR> }<BR><BR> private CnToSpell()<BR> {<BR> }<BR><BR> private static void spellPut(String spell,int ascii)<BR> {<BR> spellMap.put(spell,new Integer(ascii));<BR> }<BR><BR> private static void initialize()<BR> {<BR> spellPut("a", -20319);<BR> spellPut("ai", -20317);<BR> spellPut("an", -20304);<BR> spellPut("ang", -20295);<BR> spellPut("ao", -20292);<BR> spellPut("ba", -20283);<BR> spellPut("bai", -20265);<BR> spellPut("ban", -20257);<BR> spellPut("bang", -20242);<BR> spellPut("bao", -20230);<BR> spellPut("bei", -20051);<BR> spellPut("ben", -20036);<BR> spellPut("beng", -20032);<BR> spellPut("bi", -20026);<BR> spellPut("bian", -20002);<BR> spellPut("biao", -19990);<BR> spellPut("bie", -19986);<BR> spellPut("bin", -19982);<BR> spellPut("bing", -19976);<BR> spellPut("bo", -19805);<BR> spellPut("bu", -19784);<BR> spellPut("ca", -19775);<BR> spellPut("cai", -19774);<BR> spellPut("can", -19763);<BR> spellPut("cang", -19756);<BR> spellPut("cao", -19751);<BR> spellPut("ce", -19746);<BR> spellPut("ceng", -19741);<BR> spellPut("cha", -19739);<BR> spellPut("chai", -19728);<BR> spellPut("chan", -19725);<BR> spellPut("chang", -19715);<BR> spellPut("chao", -19540);<BR> spellPut("che", -19531);<BR> spellPut("chen", -19525);<BR> spellPut("cheng", -19515);<BR> spellPut("chi", -19500);<BR> spellPut("chong", -19484);<BR> spellPut("chou", -19479);<BR> spellPut("chu", -19467);<BR> spellPut("chuai", -19289);<BR> spellPut("chuan", -19288);<BR> spellPut("chuang", -19281);<BR> spellPut("chui", -19275);<BR> spellPut("chun", -19270);<BR> spellPut("chuo", -19263);<BR> spellPut("ci", -19261);<BR> spellPut("cong", -19249);<BR> spellPut("cou", -19243);<BR> spellPut("cu", -19242);<BR> spellPut("cuan", -19238);<BR> spellPut("cui", -19235);<BR> spellPut("cun", -19227);<BR> spellPut("cuo", -19224);<BR> spellPut("da", -19218);<BR> spellPut("dai", -19212);<BR> spellPut("dan", -19038);<BR> spellPut("dang", -19023);<BR> spellPut("dao", -19018);<BR> spellPut("de", -19006);<BR> spellPut("deng", -19003);<BR> spellPut("di", -18996);<BR> spellPut("dian", -18977);<BR> spellPut("diao", -18961);<BR> spellPut("die", -18952);<BR> spellPut("ding", -18783);<BR> spellPut("diu", -18774);<BR> spellPut("dong", -18773);<BR> spellPut("dou", -18763);<BR> spellPut("du", -18756);<BR> spellPut("duan", -18741);<BR> spellPut("dui", -18735);<BR> spellPut("dun", -18731);<BR> spellPut("duo", -18722);<BR> spellPut("e", -18710);<BR> spellPut("en", -18697);<BR> spellPut("er", -18696);<BR> spellPut("fa", -18526);<BR> spellPut("fan", -18518);<BR> spellPut("fang", -18501);<BR> spellPut("fei", -18490);<BR> spellPut("fen", -18478);<BR> spellPut("feng", -18463);<BR> spellPut("fo", -18448);<BR> spellPut("fou", -18447);<BR> spellPut("fu", -18446);<BR> spellPut("ga", -18239);<BR> spellPut("gai", -18237);<BR> spellPut("gan", -18231);<BR> spellPut("gang", -18220);<BR> spellPut("gao", -18211);<BR> spellPut("ge", -18201);<BR> spellPut("gei", -18184);<BR> spellPut("gen", -18183);<BR> spellPut("geng", -18181);<BR> spellPut("gong", -18012);<BR> spellPut("gou", -17997);<BR> spellPut("gu", -17988);<BR> spellPut("gua", -17970);<BR> spellPut("guai", -17964);<BR> spellPut("guan", -17961);<BR> spellPut("guang", -17950);<BR> spellPut("gui", -17947);<BR> spellPut("gun", -17931);<BR> spellPut("guo", -17928);<BR> spellPut("ha", -17922);<BR> spellPut("hai", -17759);<BR> spellPut("han", -17752);<BR> spellPut("hang", -17733);<BR> spellPut("hao", -17730);<BR> spellPut("he", -17721);<BR> spellPut("hei", -17703);<BR> spellPut("hen", -17701);<BR> spellPut("heng", -17697);<BR> spellPut("hong", -17692);<BR> spellPut("hou", -17683);<BR> spellPut("hu", -17676);<BR> spellPut("hua", -17496);<BR> spellPut("huai", -17487);<BR> spellPut("huan", -17482);<BR> spellPut("huang", -17468);<BR> spellPut("hui", -17454);<BR> spellPut("hun", -17433);<BR> spellPut("huo", -17427);<BR> spellPut("ji", -17417);<BR> spellPut("jia", -17202);<BR> spellPut("jian", -17185);<BR> spellPut("jiang", -16983);<BR> spellPut("jiao", -16970);<BR> spellPut("jie", -16942);<BR> spellPut("jin", -16915);<BR> spellPut("jing", -16733);<BR> spellPut("jiong", -16708);<BR> spellPut("jiu", -16706);<BR> spellPut("ju", -16689);<BR> spellPut("juan", -16664);<BR> spellPut("jue", -16657);<BR> spellPut("jun", -16647);<BR> spellPut("ka", -16474);<BR> spellPut("kai", -16470);<BR> spellPut("kan", -16465);<BR> spellPut("kang", -16459);<BR> spellPut("kao", -16452);<BR> spellPut("ke", -16448);<BR> spellPut("ken", -16433);<BR> spellPut("keng", -16429);<BR> spellPut("kong", -16427);<BR> spellPut("kou", -16423);<BR> spellPut("ku", -16419);<BR> spellPut("kua", -16412);<BR> spellPut("kuai", -16407);<BR> spellPut("kuan", -16403);<BR> spellPut("kuang", -16401);<BR> spellPut("kui", -16393);<BR> spellPut("kun", -16220);<BR> spellPut("kuo", -16216);<BR> spellPut("la", -16212);<BR> spellPut("lai", -16205);<BR> spellPut("lan", -16202);<BR> spellPut("lang", -16187);<BR> spellPut("lao", -16180);<BR> spellPut("le", -16171);<BR> spellPut("lei", -16169);<BR> spellPut("leng", -16158);<BR> spellPut("li", -16155);<BR> spellPut("lia", -15959);<BR> spellPut("lian", -15958);<BR> spellPut("liang", -15944);<BR> spellPut("liao", -15933);<BR> spellPut("lie", -15920);<BR> spellPut("lin", -15915);<BR> spellPut("ling", -15903);<BR> spellPut("liu", -15889);<BR> spellPut("long", -15878);<BR> spellPut("lou", -15707);<BR> spellPut("lu", -15701);<BR> spellPut("lv", -15681);<BR> spellPut("luan", -15667);<BR> spellPut("lue", -15661);<BR> spellPut("lun", -15659);<BR> spellPut("luo", -15652);<BR> spellPut("ma", -15640);<BR> spellPut("mai", -15631);<BR> spellPut("man", -15625);<BR> spellPut("mang", -15454);<BR> spellPut("mao", -15448);<BR> spellPut("me", -15436);<BR> spellPut("mei", -15435);<BR> spellPut("men", -15419);<BR> spellPut("meng", -15416);<BR> spellPut("mi", -15408);<BR> spellPut("mian", -15394);<BR> spellPut("miao", -15385);<BR> spellPut("mie", -15377);<BR> spellPut("min", -15375);<BR> spellPut("ming", -15369);<BR> spellPut("miu", -15363);<BR> spellPut("mo", -15362);<BR> spellPut("mou", -15183);<BR> spellPut("mu", -15180);<BR> spellPut("na", -15165);<BR> spellPut("nai", -15158);<BR> spellPut("nan", -15153);<BR> spellPut("nang", -15150);<BR> spellPut("nao", -15149);<BR> spellPut("ne", -15144);<BR> spellPut("nei", -15143);<BR> spellPut("nen", -15141);<BR> spellPut("neng", -15140);<BR> spellPut("ni", -15139);<BR> spellPut("nian", -15128);<BR> spellPut("niang", -15121);<BR> spellPut("niao", -15119);<BR> spellPut("nie", -15117);<BR> spellPut("nin", -15110);<BR> spellPut("ning", -15109);<BR> spellPut("niu", -14941);<BR> spellPut("nong", -14937);<BR> spellPut("nu", -14933);<BR> spellPut("nv", -14930);<BR> spellPut("nuan", -14929);<BR> spellPut("nue", -14928);<BR> spellPut("nuo", -14926);<BR> spellPut("o", -14922);<BR> spellPut("ou", -14921);<BR> spellPut("pa", -14914);<BR> spellPut("pai", -14908);<BR> spellPut("pan", -14902);<BR> spellPut("pang", -14894);<BR> spellPut("pao", -14889);<BR> spellPut("pei", -14882);<BR> spellPut("pen", -14873);<BR> spellPut("peng", -14871);<BR> spellPut("pi", -14857);<BR> spellPut("pian", -14678);<BR> spellPut("piao", -14674);<BR> spellPut("pie", -14670);<BR> spellPut("pin", -14668);<BR> spellPut("ping", -14663);<BR> spellPut("po", -14654);<BR> spellPut("pu", -14645);<BR> spellPut("qi", -14630);<BR> spellPut("qia", -14594);<BR> spellPut("qian", -14429);<BR> spellPut("qiang", -14407);<BR> spellPut("qiao", -14399);<BR> spellPut("qie", -14384);<BR> spellPut("qin", -14379);<BR> spellPut("qing", -14368);<BR> spellPut("qiong", -14355);<BR> spellPut("qiu", -14353);<BR> spellPut("qu", -14345);<BR> spellPut("quan", -14170);<BR> spellPut("que", -14159);<BR> spellPut("qun", -14151);<BR> spellPut("ran", -14149);<BR> spellPut("rang", -14145);<BR> spellPut("rao", -14140);<BR> spellPut("re", -14137);<BR> spellPut("ren", -14135);<BR> spellPut("reng", -14125);<BR> spellPut("ri", -14123);<BR> spellPut("rong", -14122);<BR> spellPut("rou", -14112);<BR> spellPut("ru", -14109);<BR> spellPut("ruan", -14099);<BR> spellPut("rui", -14097);<BR> spellPut("run", -14094);<BR> spellPut("ruo", -14092);<BR> spellPut("sa", -14090);<BR> spellPut("sai", -14087);<BR> spellPut("san", -14083);<BR> spellPut("sang", -13917);<BR> spellPut("sao", -13914);<BR> spellPut("se", -13910);<BR> spellPut("sen", -13907);<BR> spellPut("seng", -13906);<BR> spellPut("sha", -13905);<BR> spellPut("shai", -13896);<BR> spellPut("shan", -13894);<BR> spellPut("shang", -13878);<BR> spellPut("shao", -13870);<BR> spellPut("she", -13859);<BR> spellPut("shen", -13847);<BR> spellPut("sheng", -13831);<BR> spellPut("shi", -13658);<BR> spellPut("shou", -13611);<BR> spellPut("shu", -13601);<BR> spellPut("shua", -13406);<BR> spellPut("shuai", -13404);<BR> spellPut("shuan", -13400);<BR> spellPut("shuang", -13398);<BR> spellPut("shui", -13395);<BR> spellPut("shun", -13391);<BR> spellPut("shuo", -13387);<BR> spellPut("si", -13383);<BR> spellPut("song", -13367);<BR> spellPut("sou", -13359);<BR> spellPut("su", -13356);<BR> spellPut("suan", -13343);<BR> spellPut("sui", -13340);<BR> spellPut("sun", -13329);<BR> spellPut("suo", -13326);<BR> spellPut("ta", -13318);<BR> spellPut("tai", -13147);<BR> spellPut("tan", -13138);<BR> spellPut("tang", -13120);<BR> spellPut("tao", -13107);<BR> spellPut("te", -13096);<BR> spellPut("teng", -13095);<BR> spellPut("ti", -13091);<BR> spellPut("tian", -13076);<BR> spellPut("tiao", -13068);<BR> spellPut("tie", -13063);<BR> spellPut("ting", -13060);<BR> spellPut("tong", -12888);<BR> spellPut("tou", -12875);<BR> spellPut("tu", -12871);<BR> spellPut("tuan", -12860);<BR> spellPut("tui", -12858);<BR> spellPut("tun", -12852);<BR> spellPut("tuo", -12849);<BR> spellPut("wa", -12838);<BR> spellPut("wai", -12831);<BR> spellPut("wan", -12829);<BR> spellPut("wang", -12812);<BR> spellPut("wei", -12802);<BR> spellPut("wen", -12607);<BR> spellPut("weng", -12597);<BR> spellPut("wo", -12594);<BR> spellPut("wu", -12585);<BR> spellPut("xi", -12556);<BR> spellPut("xia", -12359);<BR> spellPut("xian", -12346);<BR> spellPut("xiang", -12320);<BR> spellPut("xiao", -12300);<BR> spellPut("xie", -12120);<BR> spellPut("xin", -12099);<BR> spellPut("xing", -12089);<BR> spellPut("xiong", -12074);<BR> spellPut("xiu", -12067);<BR> spellPut("xu", -12058);<BR> spellPut("xuan", -12039);<BR> spellPut("xue", -11867);<BR> spellPut("xun", -11861);<BR> spellPut("ya", -11847);<BR> spellPut("yan", -11831);<BR> spellPut("yang", -11798);<BR> spellPut("yao", -11781);<BR> spellPut("ye", -11604);<BR> spellPut("yi", -11589);<BR> spellPut("yin", -11536);<BR> spellPut("ying", -11358);<BR> spellPut("yo", -11340);<BR> spellPut("yong", -11339);<BR> spellPut("you", -11324);<BR> spellPut("yu", -11303);<BR> spellPut("yuan", -11097);<BR> spellPut("yue", -11077);<BR> spellPut("yun", -11067);<BR> spellPut("za", -11055);<BR> spellPut("zai", -11052);<BR> spellPut("zan", -11045);<BR> spellPut("zang", -11041);<BR> spellPut("zao", -11038);<BR> spellPut("ze", -11024);<BR> spellPut("zei", -11020);<BR> spellPut("zen", -11019);<BR> spellPut("zeng", -11018);<BR> spellPut("zha", -11014);<BR> spellPut("zhai", -10838);<BR> spellPut("zhan", -10832);<BR> spellPut("zhang", -10815);<BR> spellPut("zhao", -10800);<BR> spellPut("zhe", -10790);<BR> spellPut("zhen", -10780);<BR> spellPut("zheng", -10764);<BR> spellPut("zhi", -10587);<BR> spellPut("zhong", -10544);<BR> spellPut("zhou", -10533);<BR> spellPut("zhu", -10519);<BR> spellPut("zhua", -10331);<BR> spellPut("zhuai", -10329);<BR> spellPut("zhuan", -10328);<BR> spellPut("zhuang", -10322);<BR> spellPut("zhui", -10315);<BR> spellPut("zhun", -10309);<BR> spellPut("zhuo", -10307);<BR> spellPut("zi", -10296);<BR> spellPut("zong", -10281);<BR> spellPut("zou", -10274);<BR> spellPut("zu", -10270);<BR> spellPut("zuan", -10262);<BR> spellPut("zui", -10260);<BR> spellPut("zun", -10256);<BR> spellPut("zuo", -10254);<BR> }<BR><BR> /**<BR> * 获得单个汉字的Ascii.<BR> * @param cn char<BR> * 汉字字符<BR> * @return int<BR> * 错误返回 0,否则返回ascii<BR> */<BR> public static int getCnAscii(char cn)<BR> {<BR> byte[] bytes = (String.valueOf(cn)).getBytes();<BR> if(bytes == null || bytes.length > 2 || bytes.length <= 0){ //错误<BR> return 0;<BR> }<BR> if(bytes.length == 1){ //英文字符<BR> return bytes[0];<BR> }<BR> if(bytes.length == 2){ //中文字符<BR> int hightByte = 256 + bytes[0];<BR> int lowByte = 256 + bytes[1];<BR><BR> int ascii = (256 * hightByte + lowByte) - 256 * 256;<BR><BR> //System.out.println("ASCII=" + ascii);<BR><BR> return ascii;<BR> }<BR><BR> return 0; //错误<BR> }<BR><BR> /**<BR> * 根据ASCII码到SpellMap中查找对应的拼音<BR> * @param ascii int<BR> * 字符对应的ASCII<BR> * @return String<BR> * 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,<BR> * <BR>否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.<BR> */<BR> public static String getSpellByAscii(int ascii)<BR> {<BR> if(ascii > 0 && ascii < 160){ //单字符<BR> return String.valueOf((char)ascii);<BR> }<BR><BR> if(ascii < -20319 || ascii > -10247){ //不知道的字符<BR> return null;<BR> }<BR><BR> Set keySet = spellMap.keySet();<BR> Iterator it = keySet.iterator();<BR><BR> String spell0 = null;;<BR> String spell = null;<BR><BR> int asciiRang0 = -20319;<BR> int asciiRang;<BR> while(it.hasNext()){<BR><BR> spell = (String)it.next();<BR> Object valObj = spellMap.get(spell);<BR> if(valObj instanceof Integer){<BR> asciiRang = ((Integer)valObj).intValue();<BR><BR> if(ascii >= asciiRang0 && ascii < asciiRang){ //区间找到<BR> return(spell0 == null) ? spell : spell0;<BR> }<BR> else{<BR> spell0 = spell;<BR> asciiRang0 = asciiRang;<BR> }<BR> }<BR> }<BR><BR> return null;<BR><BR> }<BR><BR> /**<BR> * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换<BR> * @param cnStr String<BR> * 字符串<BR> * @return String<BR> * 转换成全拼后的字符串<BR> */<BR> public static String getFullSpell(String cnStr)<BR> {<BR> if(null == cnStr || "".equals(cnStr.trim())){<BR> return cnStr;<BR> }<BR><BR> char[] chars = cnStr.toCharArray();<BR> StringBuffer retuBuf = new StringBuffer();<BR> for(int i = 0,Len = chars.length;i < Len;i++){<BR> int ascii = getCnAscii(chars);<BR> if(ascii == 0){ //取ascii时出错<BR> retuBuf.append(chars);<BR> }<BR> else{<BR> String spell = getSpellByAscii(ascii);<BR> if(spell == null){<BR> retuBuf.append(chars);<BR> }<BR> else{<BR> retuBuf.append(spell);<BR> } // end of if spell == null<BR> } // end of if ascii <= -20400<BR> } // end of for<BR><BR> return retuBuf.toString();<BR> }<BR><BR> public static String getFirstSpell(String cnStr)<BR> {<BR> return null;<BR> }<BR><BR> public static void main(String[] args)<BR> {<BR> String str = null;<BR> str = "谢海101普降喜雨";<BR> System.out.println("Spell=" + CnToSpell.getFullSpell(str));<BR><BR> str = "张牙舞爪》。,";<BR> System.out.println("Spell=" + CnToSpell.getFullSpell(str));<BR><BR> str = "李鹏,可耻下场。";<BR> System.out.println("Spell=" + CnToSpell.getFullSpell(str));<BR><BR> str = "猪油,猪八戒。";<BR> System.out.println("Spell=" + CnToSpell.getFullSpell(str));<BR> }<BR>}<BR><FONT face="Times New Roman"> </FONT><p align="center"></p></p> |
|