• java pinyin4j


    pom:

    <dependency>
        <groupId>com.belerweb</groupId>
        <artifactId>pinyin4j</artifactId>
        <version>2.5.1</version>
    </dependency>

    demo:

        public static String getAllPinyin(String hanzi) {
            //输出格式设置
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            /**
             * 输出大小写设置
             * LOWERCASE:输出小写
             * UPPERCASE:输出大写
             */
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    
            /**
             * 输出音标设置
             * WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常)
             * WITH_TONE_NUMBER:1-4数字表示音标
             * WITHOUT_TONE:没有音标
             */
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    
            /**
             * 特殊音标ü设置
             *
             * WITH_V:用v表示ü
             * WITH_U_AND_COLON:用"u:"表示ü
             * WITH_U_UNICODE:直接用ü
             */
            format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    
            char[] hanYuArr = hanzi.trim().toCharArray();
            StringBuilder pinYin = new StringBuilder();
    
            try {
                for (int i = 0, len = hanYuArr.length; i < len; i++) {
                    //匹配是否是汉字
                    if (Character.toString(hanYuArr[i]).matches("[\u4E00-\u9FA5]+")) {
                        //如果是多音字,返回多个拼音,这里只取第一个
                        String[] pys = PinyinHelper.toHanyuPinyinStringArray(hanYuArr[i], format);
                        pinYin.append(pys[0]).append(" ");
                    } else {
                        pinYin.append(hanYuArr[i]).append(" ");
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            return pinYin.toString();
        }
    
        public static String getFirstPinYin(String hanyu) {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    
            StringBuilder firstPinyin = new StringBuilder();
            char[] hanyuArr = hanyu.trim().toCharArray();
            try {
                for (int i = 0, len = hanyuArr.length; i < len; i++) {
                    if (Character.toString(hanyuArr[i]).matches("[\u4E00-\u9FA5]+")) {
                        String[] pys = PinyinHelper.toHanyuPinyinStringArray(hanyuArr[i], format);
                        firstPinyin.append(pys[0].charAt(0));
                    } else {
                        firstPinyin.append(hanyuArr[i]);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            return firstPinyin.toString();
        }
  • 相关阅读:
    SPI masterslave驱动框架分析
    linux内存分配方法总结
    C#图片适应PictureBox大小显示
    .NET下的AO对象的安全释放
    .net C# PropertyGrid 显示下拉列表
    Oracle事务细节问题
    C#中Remoting的IPC通信之Winform与Windows服务通信错误问题及解决
    windows服务的安装与卸载
    windows环境下Oracle数据库冷备份和恢复全过程
    OGR连接数据源读取矢量数据图层(C#)
  • 原文地址:https://www.cnblogs.com/dhName/p/14560471.html
Copyright © 2020-2023  润新知