java 汉字转化为全拼

先根据[url]http://ash.jp/code/cn/gb2312tbl.htm [/url]表将所有的汉字编码转成十进制,如:有对应是:"129-74", "you",把所有的全放到一个map中.下面是代码:

	private static LinkedHashMap spellMap = null;

	static {
		if (spellMap == null) {
			spellMap = new LinkedHashMap(20901);
		}
		initialize();
	}

	private static void initialize() {
         spellMap.put("129-74", "you");
         spellMap.put("129-99", "dou");
}


先判断是不是GBK,接着 int hightByte = 256 + bytes[0],找一个汉字对应的Ascii,最后和上面的我们准备好的map的key进行比较.

/**
	 * 获得单个汉字的Ascii,并用"-"连接成一个字符串
	 * 
	 * @param cn char 汉字字符
	 * @return string 错误返回 空字符串,否则返回ascii
	 */
	public static String getCnAscii(char cn) {
		byte[] bytes = null;
		try {
			bytes = (String.valueOf(cn)).getBytes("GBK");
		} catch (Exception ex) {
			bytes = (String.valueOf(cn)).getBytes();
		}
		
		if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误
			return "";
		}
		if (bytes.length == 1) { // 英文字符
			return new String(bytes);
		}
		if (bytes.length == 2) { // 中文字符
			int hightByte = 256 + bytes[0];
			int lowByte = 256 + bytes[1];

			String ascii = hightByte + "-" + lowByte;

			return ascii;
		}

		return ""; // 错误
	}

	/**
	 * 根据ASCII码连接成的字符串到SpellMap中查找对应的拼音
	 * 
	 * @param ascii 字符对应的ASCII连接的字符串
	 * @return String 拼音,首先判断是否是中文如果是英文直接返回字符,如果是中文返回拼音,
	 * 
	 * 否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
	 */
	public static String getSpellByAscii(String ascii) {
		if (ascii.indexOf("-") > -1)
		{
			return (String)spellMap.get(ascii);
		} else {
			return ascii;
		}
	}

	/**
	 * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
	 * 
	 * @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++) {
			String ascii = getCnAscii(chars[i]);
			if(log.isDebugEnabled()){
				log.debug("cnToSpell:"+chars[i]+":"+ascii);
			}
			if (ascii.length() == 0) { // 取ascii时出错
				retuBuf.append(chars[i]);
			} else {
				String spell = getSpellByAscii(ascii);
				if(log.isDebugEnabled()){
					log.debug("cnToSpell:"+ascii+":"+spell);
				}
				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();
	}

	/**
	 * 获取汉语字符串的声母组合,每个汉字取拼音的第一个字符组成的一个字符串
	 * @param cnStr 汉字的字符串
	 * @return 每个汉字拼音的第一个字母所组成的汉字
	 */
	public static String getFirstSpell(String cnStr)
	{
		if (null == cnStr || "".equals(cnStr.trim())) {
			return cnStr;
		}

		char[] chars = cnStr.toCharArray();
		StringBuffer retuBuf = new StringBuffer();

		String ascii = getCnAscii(chars[0]);
		if (ascii.length() == 1) { // 取ascii时出错
			retuBuf.append(chars[0]);
		} else {
			
			String spell = getSpellByAscii(ascii).substring(0, 1);
			if (spell == null) {
				retuBuf.append(chars[0]);
			} else {
				retuBuf.append(spell);
			} // end of if spell == null
		} // end of if ascii <= -20400 
		
		return retuBuf.toString();
	}


你可能感兴趣的:(java)