posts - 2,  comments - 2,  trackbacks - 0
      昨天发现一号玩的jar包(pinyin4j-2.5.0.jar),可以把汉字转为拼音。可在http://pinyin4j.sourceforge.net/下载。下面是我做的一个Demo

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class HanZiToPinYin {


    
public static  String toPinYin(String hanzhis){
        CharSequence s
= hanzhis;
        
        
char [] hanzhi=new char[s.length()];
        
for(int i=0;i<s.length();i++){
            hanzhi[i]
=s.charAt(i);
        }

        
        
char [] t1 =hanzhi; 
        String[] t2 
= new String[s.length()];
        
/**
         * 设置输出格式
         
*/

        net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 
= new
        HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        
        
int t0=t1.length;
        String py 
= "";
        
try {
            
for (int i=0;i<t0;i++)
            
{
                t2 
= PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                py
=py+t2[0].toString();
                    }

        }

        
catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }

        
        
return py.trim();
        }

    
    
    
    
    
public static void main(String[] args) {
        
        System.err.println(ToPinYin.toPinYing(
"汉字转拼音"));
    }

 
}


输出结果:hanzizhuanpinyin
        转换的正确率,还不错。
详细请查看官方文档!
posted on 2008-11-20 09:56 R_XiaoGuang 阅读(3002) 评论(2)  编辑  收藏 所属分类: opensource

FeedBack:
# re: 汉字转拼音(PinYin4j)
2011-10-12 13:16 | MMMM
谢谢分享,的确挺好玩!不过第一句话有个错别字,嘻嘻........
<昨天发现一号玩的jar包>  回复  更多评论
  
# re: 汉字转拼音(PinYin4j)
2012-06-19 10:21 | 张志杰
谢谢你,亲,啊哈哈~  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2012年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

没有什么不可以承受,只是没有落到自己身上。

没有什么不可以成功,只是没有真正的努力过。

 

常用链接

留言簿(2)

随笔分类

随笔档案

最新随笔

搜索

  •  

积分与排名

  • 积分 - 7240
  • 排名 - 2699

最新评论

阅读排行榜

评论排行榜