java之——汉字转换拼音大小写

pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务,主要有一下方法:

[java]  view plain  copy
  1. static public String[] toHanyuPinyinStringArray(char ch)  
将char(必须为汉字单字)转化为拼音,实用的是通用的格式,如果ch为非汉字,返回null。
输入:重 输出:[zhong4, chong2]
说明重字有两个读音,拼音后面的1,2,3,4 代表的是读音
[java]  view plain  copy
  1. static public String[] toHanyuPinyinStringArray(char ch,HanyuPinyinOutputFormat outputFormat)  
同上,但是这个方法可以设置输出的格式。HanyuPinyinOutputFormat   可以设置拼音大小写、是否后面加读音数字、特殊读音的显示方式,定义如下:
[java]  view plain  copy
  1. /** 
  2.  * The option indicates that the output of 'ü' is "u:" 
  3.  */  
  4. public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");  
  5. /** 
  6.  * The option indicates that the output of 'ü' is "v" 
  7.  */  
  8. public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")  
  9. /** 
  10.  * The option indicates that the output of 'ü' is "ü" in Unicode form 
  11.  */  
  12. public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");  
[java]  view plain  copy
  1. static public String[] toTongyongPinyinStringArray(char ch)     //转换为通用拼音  
  2. static public String[] toWadeGilesPinyinStringArray(char ch)    //转换为威妥玛拼音  
  3. static public String[] toMPS2PinyinStringArray(char ch)         //转换为注音符号拼音  
  4. static public String[] toYalePinyinStringArray(char ch)         //转换为耶魯拼音  
  5. static public String[] toGwoyeuRomatzyhStringArray(char ch)     //转换为国语罗马字  
对于”重“的拼音转换,以上方法分别得到的结果是:
[plain]  view plain  copy
  1. 汉语拼音:[zhong4, chong2]  
  2. 通用拼音:[jhong4, chong2]  
  3. 威妥玛拼音:[chung4, ch`ung2]  
  4. 注音符号拼音:[jung4, chung2]  
  5. 耶魯拼音:[jung4, chung2]  
  6. 国语罗马字:[jonq, chorng]  
好了,有了上面的基础,我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。
首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:
[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>com.belerweb</groupId>  
  3.     <artifactId>pinyin4j</artifactId>  
  4.     <version>2.5.0</version>  
  5. </dependency>  
非maven的可以直接将下载好的jar包放入classpath。
然后编写工具类 PinyinTool.java:
[java]  view plain  copy
  1. package com.lyz.pingyin;  
  2.   
  3. import net.sourceforge.pinyin4j.PinyinHelper;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  7. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  8.   
  9.   
  10. /** 
  11.  * 汉字转化为拼音的工具类 
  12.  * @author liuyazhuang 
  13.  * 
  14.  */  
  15. public class PinyinTool {  
  16.     HanyuPinyinOutputFormat format = null;  
  17.     public static enum Type {  
  18.         UPPERCASE,              //全部大写  
  19.         LOWERCASE,              //全部小写  
  20.         FIRSTUPPER              //首字母大写  
  21.     }  
  22.   
  23.     public PinyinTool(){  
  24.         format = new HanyuPinyinOutputFormat();  
  25.         format.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
  26.         format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  27.     }  
  28.   
  29.     public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{  
  30.         return toPinYin(str, "", Type.UPPERCASE);  
  31.     }  
  32.   
  33.     public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
  34.         return toPinYin(str, spera, Type.UPPERCASE);  
  35.     }  
  36.   
  37.     /** 
  38.      * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换 
  39.      * 如: 明天 转换成 MINGTIAN 
  40.      * @param str:要转化的汉字 
  41.      * @param spera:转化结果的分割符 
  42.      * @return 
  43.      * @throws BadHanyuPinyinOutputFormatCombination 
  44.      */  
  45.     public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {  
  46.         if(str == null || str.trim().length()==0)  
  47.             return "";  
  48.         if(type == Type.UPPERCASE)  
  49.             format.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
  50.         else  
  51.             format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  52.   
  53.         String py = "";  
  54.         String temp = "";  
  55.         String[] t;  
  56.         for(int i=0;i<str.length();i++){  
  57.             char c = str.charAt(i);  
  58.             if((int)c <= 128)  
  59.                 py += c;  
  60.             else{  
  61.                 t = PinyinHelper.toHanyuPinyinStringArray(c, format);  
  62.                 if(t == null)  
  63.                     py += c;  
  64.                 else{  
  65.                     temp = t[0];  
  66.                     if(type == Type.FIRSTUPPER)  
  67.                         temp = t[0].toUpperCase().charAt(0)+temp.substring(1);  
  68.                     py += temp+(i==str.length()-1?"":spera);  
  69.                 }  
  70.             }  
  71.         }  
  72.         return py.trim();  
  73.     }  
  74. }  
编写测试类PingyinToolTest

[java]  view plain  copy
  1. package com.lyz.pingyin.test;  
  2.   
  3. import com.lyz.pingyin.PinyinTool;  
  4. import com.lyz.pingyin.PinyinTool.Type;  
  5.   
  6. /** 
  7.  * 测试拼音转化结果 
  8.  * @author liuyazhuang 
  9.  * 
  10.  */  
  11. public class PingyinToolTest {  
  12.     public static void main(String[] args) throws Exception{  
  13.         PinyinTool tool = new PinyinTool();  
  14.         System.out.println("刘亚壮的运行测试结果为====" + tool.toPinYin("刘亚壮""", Type.LOWERCASE));  
  15.     }  
  16. }  

http://blog.csdn.net/l1028386804/article/details/55505397


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值