汉字转拼音,pinyin4j使用详细介绍

汉字转拼音,pinyin4j使用详细介绍

首先,在pom.xml文件中导入依赖。

<!-- pinyin4j -->
<dependency>
     <groupId>com.belerweb</groupId>
     <artifactId>pinyin4j</artifactId>
     <version>2.5.0</version>
 </dependency>

输出格式设置:

大小写,音标,特殊音标ü

大小写:

  • LOWERCASE: 输出小写

  • UPPERCASE:输出大写

输出音标:

  • WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常) 例如huáng

  • WITH_TONE_NUMBER:1-5数字表示音标,5表示轻声。 例如:huang2

  • WITHOUT_TONE:没有音标 例如:huang

特殊音标ü设置:

  • WITH_V:用v表示ü
  • WITH_U_AND_COLON:用"u:"表示ü
  • WITH_U_UNICODE:直接用ü

在这里插入图片描述
创建工具类

扫描二维码关注公众号,回复: 11625995 查看本文章
public final class WordToPinYinUtil {
/**
     * @Description: 识别多音字   汉字转换为拼音  声调为数字 例如:huang2
     */
    public static String ToPinyinNumber(String src){
        char[] hz = null;
        hz = src.toCharArray();//该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符
        String[] py = new String[hz.length];//该数组用来存储
        //设置汉子拼音输出的格式
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
       
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

        String pys = ""; //存放拼音字符串
        int len = hz.length;

        try {
            for (int i = 0; i < len ; i++ ){
                //先判断是否为汉字字符
                if(Character.toString(hz[i]).matches("[\\u4E00-\\u9FA5]+")){
                    //将汉字的几种全拼都存到py数组中
                    py = PinyinHelper.toHanyuPinyinStringArray(hz[i],format);
                    for (int j = 0; j < py.length ; j++) {
                        if ("".equals(pys)) {
                            pys = py[j];
                        } else {
                            pys = pys + "," + py[j];
                        }
                    }
                    //取出改汉字全拼的第一种读音,并存放到字符串pys后
                    //pys += py[0];
                }else{
                    //如果不是汉字字符,间接取出字符并连接到 pys 后
                    pys += Character.toString(hz[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e){
            e.printStackTrace();
        }
        return pys;
    }
   /**
    * @Description:识别多音字 用声调符号标识 例如huáng
    */
    public static String ToPinyinMark(String src){
        char[] hz = null;
        hz = src.toCharArray();//该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符
        String[] py = new String[hz.length];//该数组用来存储
        //设置汉子拼音输出的格式
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

        String pys = ""; //存放拼音字符串
        int len = hz.length;

        try {
            for (int i = 0; i < len ; i++ ){
                //先判断是否为汉字字符
                if(Character.toString(hz[i]).matches("[\\u4E00-\\u9FA5]+")){
                    //将汉字的几种全拼都存到py数组中
                    py = PinyinHelper.toHanyuPinyinStringArray(hz[i],format);
                    for (int j = 0; j < py.length ; j++) {
                        if ("".equals(pys)) {
                            pys = py[j];
                        } else {
                            pys = pys + "," + py[j];
                        }
                    }
                    //取出改汉字全拼的第一种读音,并存放到字符串pys后
                    //pys += py[0];
                }else{
                    //如果不是汉字字符,间接取出字符并连接到 pys 后
                    pys += Character.toString(hz[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e){
            e.printStackTrace();
        }
        return pys;
    }

    /**
     * @Description:多音字取第一个音标  无声调表示,例如:huang
     */
    public static String ToPinyinNone(String src){
        char[] hz = null;
        hz = src.toCharArray();//该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符
        String[] py = new String[hz.length];//该数组用来存储
        //设置汉子拼音输出的格式
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        String pys = ""; //存放拼音字符串
        int len = hz.length;

        try {
            for (int i = 0; i < len ; i++ ){
                //先判断是否为汉字字符
                if(Character.toString(hz[i]).matches("[\\u4E00-\\u9FA5]+")){
                    //将汉字的几种全拼都存到py数组中
                    py = PinyinHelper.toHanyuPinyinStringArray(hz[i],format);
//                    // 全部拼音 , 分割
//                    for (int j = 0; j < py.length ; j++) {
//                        if ("".equals(pys)) {
//                            pys = py[j];
//                        } else {
//                            pys = pys + "," + py[j];
//                        }
//                    }
                    //取出该汉字全拼的第一种读音,并存放到字符串pys后
                    pys += py[0];
                }else{
                    //如果不是汉字字符,间接取出字符并连接到 pys 后
                    pys += Character.toString(hz[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e){
            e.printStackTrace();
        }
        return pys;
    }

    /**
     * @Description:    汉字转换为拼音首字母
     */
    public static String ToFirstChar(String str){
        String convert = "";
        for (int j = 0; j <  str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
}

案列:
在这里插入图片描述
运行结果:
注:前两个方法取多音字全部拼音。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sxsssss/article/details/103527892