当前位置:编程学习 > JAVA >>

Java实现汉字转换为拼音

本文的核心代码取自easydozer的blog:http://blog.csdn.net/easydozer/
代码说明:
Java实现汉字转换为拼音的GUI版本。

GUI代码部分:
/**
* @(#)CnToSpellGUI.java
* kindani
* 2004-10-25??
* */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
*





*

JDK版本
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("To:"));
getContentPane().add(to);
// action handle
b.addActionListener(new Cn2PinyinActionListener(from,to));
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 to) {
this.from = from;
this.to = to;
}
public void actionPerformed(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();
}
}


核心代码部分:
引用自:easydozer的blog:http://blog.csdn.net/easydozer/
http://blog.csdn.net/easydozer/archive/2004/10/20/Chinese2FullSpell.aspx



/**
* @(#)CnToSpell.java
* 版权声明 Easydozer 版权所有 违者必究
*
* 修订记录:
* 1)更改者:Easydozer
* 时 间:2004-10-20 
* 描 述:创建
*/
package com.easydozer.commons.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

/**
*

汉字转化为全拼


*

JDK版本:
1.4


* @author 谢计生
* @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("ai", -20317);
spellPut("an", -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", -20036);
spellPut("beng", -20032);
spellPut("bi", -20026);
spellPut("bian", -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", -19763);
spellPut("cang", -19756);
spellPut("cao", -19751);
spellPut("ce", -19746);
spellPut("ceng", -19741);
spellPut("cha", -19739);
spellPut("chai", -19728);
spellPut("chan", -19725);
spellPut("chang", -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", -19018);
spellPut("de", -19006);
spellPut("deng", -19003);
spellPut("di", -18996);
spellPut("dian", -18977);
spellPut("diao", -18961);
spellPut("die", -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", -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("hen", -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", -15920);
spellPut("lin", -15915);
spellPut("ling", -15903);
spellPut("liu", -15889);
spellPut("long", -15878);
spellPut("lou", -

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,