[Java代码] Java用pinyin4j根据汉语获取各种格式和需求的拼音

 

 

 



pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j。
下面介绍用pinyin4j来做的一个根据汉语获取各种格式和需求的拼音demo
需要pinyin4j.jar自己去官网下载去吧http://pinyin4j.sourceforge.net/


  1. import java.util.HashSet;
  2. import java.util.Set;
  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.HanyuPinyinVCharType;
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  9. /**
  10. * 
  11. * @author  : Robert robert@xiaobei668.com
  12. * @version : 1.00
  13. * Create Time : 2011-3-22-下午07:04:30
  14. * Description : 
  15. *              处理汉字和对应拼音转换的工具类 
  16. * History:
  17. *  Editor       version      Time               Operation    Description*
  18. *  
  19. *
  20. */
  21. public class PinYinUtil {
  22.         
  23.         /**
  24.          * 
  25.          * @param src
  26.          * @return
  27.          * author  : Robert
  28.          * about version :1.00
  29.          * create time   : 2011-3-22-下午07:04:27
  30.          * Description :
  31.          *             传入汉字字符串,拼接成对应的拼音,返回拼音的集合
  32.          */
  33.         public static Set<String> getPinYinSet(String src){
  34.                 Set<String> lstResult = new HashSet<String>();
  35.                 char[] t1 = null;  //字符串转换成char数组
  36.                 t1 = src.toCharArray();
  37.                 
  38.                 //①迭代汉字
  39.                 for(char ch : t1){  
  40.                         String s[] = getPinYin(ch);  
  41.                         Set<String> lstNew = new HashSet<String>();
  42.                         //②迭代每个汉字的拼音数组
  43.                         for(String str : s){
  44.                                 if(lstResult.size()==0){
  45.                                         lstNew.add(str);
  46.                                 }else{
  47.                                         for(String ss : lstResult){
  48.                                                 ss += str;
  49.                                                 lstNew.add(ss);
  50.                                         }
  51.                                 }
  52.                         }
  53.                         lstResult.clear();
  54.                         lstResult = lstNew;
  55.                 }
  56.                 return lstResult;
  57.         }
  58.         
  59.         public static void main(String[] args) {
  60.                 Set<String> lst = PinYinUtil.getPinYinSet("迭代每个汉字的拼音数组,该分享来自程序员之家");
  61.                 for (String string : lst) {
  62.                         System.out.println(string);
  63.                 }
  64.         }
  65.         
  66.         /**
  67.          * 
  68.          * @param src
  69.          * @return
  70.          * author  : Robert
  71.          * about version :1.00
  72.          * create time   : 2011-3-22-下午02:21:42
  73.          * Description :
  74.          *             传入中文汉字,转换出对应拼音
  75.          *             注:出现同音字,默认选择汉字全拼的第一种读音
  76.          */
  77.         public static String getPinYin(String src) {
  78.                 char[] t1 = null;
  79.                 t1 = src.toCharArray();
  80.                 String[] t2 = new String[t1.length];
  81.                 // 设置汉字拼音输出的格式
  82.                 HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  83.                 t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  84.                 t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  85.                 t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  86.                 String t4 = "";
  87.                 int t0 = t1.length;
  88.                 try {
  89.                         for (int i = 0; i < t0; i++) {
  90.                                 // 判断能否为汉字字符
  91.                                 // System.out.println(t1[i]);
  92.                                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
  93.                                         t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
  94.                                         t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
  95.                                 } else {
  96.                                         // 如果不是汉字字符,间接取出字符并连接到字符串t4后
  97.                                         t4 += Character.toString(t1[i]);
  98.                                 }
  99.                         }
  100.                 } catch (BadHanyuPinyinOutputFormatCombination e) {
  101.                         e.printStackTrace();
  102.                 }
  103.                 return t4;
  104.         }
  105.         
  106.         /**
  107.          * @param src
  108.          * @return
  109.          * author  : Robert
  110.          * about version :1.00
  111.          * create time   : 2011-3-22-下午02:52:35
  112.          * Description :
  113.          *             将单个汉字转换成汉语拼音,考虑到同音字问题,返回字符串数组的形式
  114.          */
  115.         public static String[] getPinYin(char src){
  116.                 char[] t1 = {src};
  117.                 String[] t2 = new String[t1.length];
  118.                 
  119.                 // 设置汉字拼音输出的格式
  120.                 HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  121.                 t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  122.                 t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  123.                 t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  124.                 
  125.                 // 判断能否为汉字字符
  126.                 if (Character.toString(t1[0]).matches("[\\u4E00-\\u9FA5]+")) {
  127.                         try {
  128.                                 // 将汉字的几种全拼都存到t2数组中
  129.                                 t2 = PinyinHelper.toHanyuPinyinStringArray(t1[0], t3);
  130.                         } catch (BadHanyuPinyinOutputFormatCombination e) {
  131.                                 e.printStackTrace();
  132.                         }
  133.                 } else {
  134.                         // 如果不是汉字字符,则把字符直接放入t2数组中
  135.                         t2[0] = String.valueOf(src);
  136.                 }
  137.                 return t2;
  138.         }
  139.         
  140.         /**
  141.          * 
  142.          * @param src
  143.          * @return
  144.          * author  : Robert
  145.          * about version :1.00
  146.          * create time   : 2011-3-22-下午03:03:02
  147.          * Description :
  148.          *             传入没有多音字的中文汉字,转换出对应拼音
  149.          *             注:如果传入的中文中有任一同音字都会返回字符串信息:false
  150.          */
  151.         public static String getNoPolyphone(String src){
  152.                 char[] t1 = null;
  153.                 t1 = src.toCharArray();
  154.                 String[] t2 = new String[t1.length];
  155.                 // 设置汉字拼音输出的格式
  156.                 HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
  157.                 t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  158.                 t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  159.                 t3.setVCharType(HanyuPinyinVCharType.WITH_V);
  160.                 String t4 = "";
  161.                 int t0 = t1.length;
  162.                 try {
  163.                         for (int i = 0; i < t0; i++) {
  164.                                 // 判断能否为汉字字符
  165.                                 // System.out.println(t1[i]);
  166.                                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
  167.                                         t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
  168.                                         if(t2.length>1){
  169.                                                 return "false";
  170.                                         }else{
  171.                                                 t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
  172.                                         }
  173.                                 } else {
  174.                                         // 如果不是汉字字符,间接取出字符并连接到字符串t4后
  175.                                         t4 += Character.toString(t1[i]);
  176.                                 }
  177.                         }
  178.                 } catch (BadHanyuPinyinOutputFormatCombination e) {
  179.                         e.printStackTrace();
  180.                 }
  181.                 return t4;
  182.         }
  183.         
  184.         
  185. }
复制代码

推荐文章:Java根据汉字字符串检索出字符首字母

转载自:http://bbs.it-home.org/thread-3093-1-1.html

posted @ 2017-06-30 11:31  jack_ou  阅读(688)  评论(0)    收藏  举报