当前位置: 移动技术网 > IT编程>开发语言>Java > java实现汉字转拼音

java实现汉字转拼音

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、问题描述
汉字转化为对应的拼音或者获取汉字拼音的首字母,这些都是在开发中经常遇到的问题,在获取汉字的拼音或者拼音的首字母之后,我们在推荐或者搜索部门可以很大程度提高用户的体验,比如用户输入“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实现汉字转拼音的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网