java实现汉字转拼音


一、问题描述
汉字转化为对应的拼音或者获取汉字拼音的首字母,这些都是在开发中经常遇到的问题,在获取汉字的拼音或者拼音的首字母之后,我们在推荐或者搜索部门可以很大程度提高用户的体验,比如用户输入“NH”,我们就可以联想出“你好”、“你会”、“年后”、“内涵”等词语。在Java中,pinyin4j.jar这个工具很好实现了将汉字转化为对应的拼音,下面我们就介绍下如何使用这个jar包。

二、资源下载
下载之后解压,直接使用文件中的pinyin4j-2.5.0.jar即可。

三、提供方法
我们可以使用HanyuPinyinOutputFormat类来设置拼音的返回方式,比如设置拼音的大小写、音标方式以及拼音ü的显示形式,具体如下图:

直接使用PinyinHelper中的方法来对汉字做对应的转化,具体有如下三种,三种效果如何自己做下测试即可:

四、编写代码
针对我们平常可能用到的功能,我做了如下的封装,提供的功能还有具体的实现步骤参照代码中的注释:

package com.lulei.util; 
import java.util.ArrayList; 
import java.util.List; 
 
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; 
  
public class PinYinUtil { 
  private static HanyuPinyinOutputFormat format = null; 
  static { 
    format = new HanyuPinyinOutputFormat(); 
    //拼音小写 
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
    //无音标方式;WITH_TONE_NUMBER:1-4数字表示英标;WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常 
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
    //用v表示ü 
    format.setVCharType(HanyuPinyinVCharType.WITH_V); 
  } 
   
  /** 
   * @param str 
   * @return 
   * @Description: 返回字符串的拼音 
   */ 
  public static String[] getCharPinYinString(String str) { 
    if (str == null || str.length() < 1) { 
      return null; 
    } 
    List<String> result = new ArrayList<String>(); 
    //对字符串中的记录逐个分析 
    for (int i = 0; i < str.length(); i++) { 
      result = getCharPinYinString(str.charAt(i), result); 
    } 
    return result.toArray(new String[result.size()]); 
  } 
   
  /** 
   * @param c 
   * @param list 
   * @return 
   * @Description: 将字符c的拼音拼接到list中的记录中 
   */ 
  private static List<String> getCharPinYinString(char c, List<String> list) { 
    String[] strs = getCharPinYinString(c); 
    List<String> result = new ArrayList<String>(); 
    //如果解析出的拼音为空,判断字符C是否为英文字母,如果是英文字母则添加值拼音结果中 
    if (strs == null) { 
      if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { 
        c = c <= 91 ? (char)(c + 32) : c; 
        if (list == null || list.size() == 0) { 
          result.add(c + ""); 
        } else { 
          for (String s : list) { 
            result.add(s + c); 
          } 
        } 
        return result; 
      } 
      return list; 
    } 
    //将字符C的拼音首和已存在的拼音首组合成新的记录 
    for (String str : strs) { 
      if (list == null || list.size() == 0) { 
        result.add(str); 
      } else { 
        for (String s : list) { 
          result.add(s + str); 
        } 
      } 
    } 
    return result; 
  } 
   
  /** 
   * @param c 
   * @return 
   * @Description: 返回汉字的拼音 
   */ 
  public static String[] getCharPinYinString(char c) { 
    try { 
      //返回字符C的拼音 
      return PinyinHelper.toHanyuPinyinStringArray(c, format); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 
   
  /** 
   * @param str 
   * @return 
   * @Description: 返回字符串的拼音的首字母 
   */ 
  public static String[] getCharPinYinChar(String str) { 
    if (str == null || str.length() < 1) { 
      return null; 
    } 
    List<String> result = new ArrayList<String>(); 
    //对字符串中的记录逐个分析 
    for (int i = 0; i < str.length(); i++) { 
      result = getCharPinYinChar(str.charAt(i), result); 
    } 
    return result.toArray(new String[result.size()]); 
  } 
   
  /** 
   * @param c 
   * @param list 
   * @return 
   * @Description: 将字符c的拼音首字母拼接到list中的记录中 
   */ 
  private static List<String> getCharPinYinChar(char c, List<String> list) { 
    char[] chars = getCharPinYinChar(c); 
    List<String> result = new ArrayList<String>(); 
    //如果解析出的拼音为空,判断字符C是否为英文字母,如果是英文字母则添加值拼音结果中 
    if (chars == null) { 
      if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { 
        c = c < 91 ? (char)(c + 32) : c; 
        if (list == null || list.size() == 0) { 
          result.add(c + ""); 
        } else { 
          for (String s : list) { 
            result.add(s + c); 
          } 
        } 
        return result; 
      } 
      return list; 
    } 
    //将字符C的拼音首字母和已存在的拼音首字母组合成新的记录 
    for (char ch : chars) { 
      if (list == null || list.size() == 0) { 
        result.add(ch + ""); 
      } else { 
        for (String s : list) { 
          result.add(s + ch); 
        } 
      } 
    } 
    return result; 
  } 
   
  /** 
   * @param c 
   * @return 
   * @Description:返回汉字拼音首字母 
   */ 
  public static char[] getCharPinYinChar(char c) { 
    //字符C的拼音 
    String[] strs = getCharPinYinString(c); 
    if (strs != null) { 
      //截取拼音的首字母 
      char[] chars = new char[strs.length]; 
      for(int i = 0; i <chars.length; i++) { 
        chars[i] = strs[i].charAt(0); 
      } 
      return chars; 
    } 
    return null; 
  } 
   
  public static void main(String[] args) { 
    // TODO Auto-generated method stub  
    char c = "重庆".charAt(0); 
    String[] str = PinYinUtil.getCharPinYinString(c); 
    for(String s : str) { 
      System.out.println(s); 
    } 
     
    char[] chars = PinYinUtil.getCharPinYinChar(c); 
    for(char c1 : chars) { 
      System.out.println(c1); 
    } 
     
    str = PinYinUtil.getCharPinYinString("重庆c"); 
    for(String s : str) { 
      System.out.println(s); 
    } 
     
    str = PinYinUtil.getCharPinYinChar("重庆a"); 
    for(String s : str) { 
      System.out.println(s); 
    } 
  } 
 
} 

五、输出结果

以上就是java实现汉字转拼音的全部内容,希望对大家的学习有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3