`

JAVA将汉字转化成拼音的方法

阅读更多
/** *//**
#############################################################################
# DESCRIBE 将汉字转化成拼音
# 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() ...{
 }


 
/** *//**
  * 获得单个汉字的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;
   
return ascii;
  }

  
return 0//错误
 }


 
/** *//**
  * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
  * 
@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();
 }


 
/** *//**
  * 根据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;

 }


 
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"
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics