IT前沿技术
    IT名企招聘
    IT技术网站
首页 >> IT沙龙 >> IT前沿技术

JAVA 实现把汉字转化成拼音

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();

}

}

------------------------------------------------------------------------

/**

* @(#)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()

{

/**

* 获得单个汉字的 Ascii.

* @param cn char

* 汉字字符

* @return int

* 错误返回 0, 否则返回 ascii

*/

public static int getCnAscii( char cn)

{

byte [] bytes = (String.valueOf(cn)).getBytes();

if (bytes == null || bytes.length > 2 || bytes.length <= 0){ // 错误

 

return 0;

}

if (bytes.length == 1){ // 英文字符

 

return bytes[0];

}

if (bytes.length == 2){ // 中文字符

 

int hightByte = 256 + bytes[0];

int lowByte = 256 + bytes[1];

int ascii = (256 * hightByte + lowByte) - 256 * 256;

//System.out.println("ASCII=" + ascii);

 

return ascii;

}

return 0; // 错误

 

}

/**

* 根据 ASCII 码到 SpellMap 中查找对应的拼音

* @param ascii int

* 字符对应的 ASCII

* @return String

* 拼音 , 首先判断 ASCII 是否 >0&<160, 如果是返回对应的字符 ,

*

  否则到 SpellMap 中查找 , 如果没有找到拼音 , 则返回 null, 如果找到则返回拼音 .

*/

public static String getSpellByAscii( int ascii)

{

if (ascii > 0 && ascii < 160){ // 单字符

 

return String.valueOf(( char )ascii);

}

if (ascii < -20319 || ascii > -10247){ // 不知道的字符

 

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){ // 区间找到

 

return (spell0 == null ) ? spell : spell0;

}

else {

spell0 = spell;

asciiRang0 = asciiRang;

}

}

}

return null ;

}

/**

* 返回字符串的全拼 , 是汉字转化为全拼 , 其它字符不进行转换

* @param cnStr String

* 字符串

* @return 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){ // 取 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 for

 

return retuBuf.toString();

}

public static String getFirstSpell(String cnStr)

{

return null ;

}

public static void main(String[] args)

{

String str = null ;

str = " 谢海 101 普降喜雨 ";

System. out .println("Spell=" + CnToSpell.getFullSpell(str));

str = " 张牙舞爪》。, ";

System. out .println("Spell=" + CnToSpell.getFullSpell(str));

str = " 可耻下场。 ";

System. out .println("Spell=" + CnToSpell.getFullSpell(str));

str = " 猪油,猪八戒。 ";

System. out .println("Spell=" + CnToSpell.getFullSpell(str));

}

}