中文转换拼音的实现思路以及实现
??? ?在项目中一位大师兄写的中文转换拼音代码:自己整理一下,在此表示感谢!!^_^
?
?? 首先熟悉一下基础:LinkedHashMap
??? J2SE 1.4 为 Java Collections Framework 引入了两个新实现, LinkedHashSet
和 LinkedHashMap
。添加这两个新实现的好处是散列集合现在可以维护贯穿其元素的两条路径。除标准的散列关系之外,现在还有一个可遍历整个集合的链表。正常情况下,这个新的第二路径会遵循插入顺序,这意味着集合的迭代器将按照元素的插入顺序返回元素(而不按它们的散列码将其组合成一个集合的顺序),但 LinkedHashMap
支持第二种排序选项:按存取顺序而非插入顺序维护链表。
?
???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) {
??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.charAt(0));
????} // end of if spell == null
???} // end of if ascii <= -20400
??} // end of for
??return retuBuf.toString();
?}
?public static void main(String[] args) {
??String str = null;
??str = "谢海101普降喜雨";
??System.out.println("Spell=" + CnToSpellUtils.getFullSpell(str));
??str = "张牙舞爪》。,";
??System.out.println("Spell=" + CnToSpellUtils.getFullSpell(str));
??str = "猪油戒。";
??System.out.println("Spell=" + CnToSpellUtils.getFullSpell(str));
??str = "澳门";
??System.out.println("Spell=" + CnToSpellUtils.getFirstSpell(str));
?}
}
?
1 楼 Hojave 2010-04-19 楼主试试这几个字: