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 @ 2008-11-20 09:56 R_XiaoGuang 阅读(3002) | 评论 (2)编辑 收藏
     摘要:   阅读全文
posted @ 2008-09-20 07:31 R_XiaoGuang 阅读(125) | 评论 (0)编辑 收藏
仅列出标题  
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

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

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

 

常用链接

留言簿(2)

随笔分类

随笔档案

最新随笔

搜索

  •  

积分与排名

  • 积分 - 7240
  • 排名 - 2699

最新评论

阅读排行榜

评论排行榜