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

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

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

原文地址:https://www.cnblogs.com/langtianya/p/7098089.html