【Java】汉字转拼音

【Java】汉字转拼音
将汉字转成拼音及汉字首字母,可以使用jar包 pingyin4j,但是遇到多音节汉字就会有问题。如果使用大名鼎鼎的jpinyin,可以自动识别常见多音字,而且还支持简体转换为繁体,检查是简体还是繁体,是否为中文字符等功能。下面简单列举了汉字转换拼音、简体转换繁体和判断是否为中文字符串。

依赖包
       <dependency>
            <groupId>com.github.stuxuhai</groupId>
            <artifactId>jpinyin</artifactId>
            <version>1.1.8</version>
        </dependency>

示例
复制代码## 标题

import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ChangeToPinYinJP {

    private static String REGEX_CHINESE_CHARACTER = "[\\u4E00-\\u9FA5]+";
    public static void main(String[] args) {
        System.out.println(changeHanZi2TonePinYin("音乐,。 359 非常 Wiener 快乐"));
//        搞不定的示例
        printStr("人参 参与");

    }

    public static void printStr(String ret) {
        String result = changeToTonePinYin(ret);
        System.out.println("Oops! --》 " + result);
    }

    /**
     * 转换为有声调的拼音字符串
     *
     * @param givenHanZi 指定汉字
     * @return 有声调的拼音字符串
     */
    public static String changeToMarkPinYin(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = PinyinHelper.convertToPinyinString(givenHanZi, " ", PinyinFormat.WITH_TONE_MARK);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }
   /**
     * 转换为数字声调字符串
     *
     * @param givenHanZi 需转换的汉字
     * @return 转换完成的拼音字符串
     */
    public static String changeToNumberPinYin(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = PinyinHelper.convertToPinyinString(givenHanZi, " ", PinyinFormat.WITH_TONE_NUMBER);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }

    /**
     * 转换为不带音调的拼音字符串,只转换汉字部分,不转换数字、标点符号和英文字母
     * 自动识别常用多音字
     * @param givenHanZi 汉字字符串
     * @return 拼音字符串
     */
    public static String changeHanZi2TonePinYin(String givenHanZi) {
        if (StringUtils.isBlank(givenHanZi)) {
            return "";
        }
        String tempOutput = "";
        StringBuilder output = new StringBuilder();
        char[] input = givenHanZi.trim().toCharArray();

        try {
            for (int i = 0; i < input.length; i++) {
                if (Character.toString(input[i]).matches(REGEX_CHINESE_CHARACTER)) {
                    tempOutput += Character.toString(input[i]);
                } else {
                    tempOutput = changeToTonePinYin(tempOutput);
                    output = output.append(tempOutput).append(Character.toString(input[i]));
                    tempOutput = "";
                }
            }
            if (StringUtils.isNotBlank(tempOutput)) {
                output = output.append(changeToTonePinYin(tempOutput));
            }
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return output.toString();
    }

    /**
     * 转换为不带音调的拼音字符串
     *
     * @param givenHanZi 需转换的汉字
     * @return 拼音字符串
     */
    public static String changeToTonePinYin(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = PinyinHelper.convertToPinyinString(givenHanZi, " ", PinyinFormat.WITHOUT_TONE);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }

    /**
     * 转换为每个汉字对应拼音首字母字符串
     *
     * @param givenHanZi 需转换的汉字
     * @return 拼音字符串
     */
    public static String changeToGetShortPinYin(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = PinyinHelper.getShortPinyin(givenHanZi);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }

    /**
     * 检查汉字是否为多音字
     *
     * @param givenHanZi 需检查的汉字
     * @return true 多音字,false 不是多音字
     */
    public static boolean checkPinYin(char givenHanZi) {
        boolean check = false;
        try {
            check = PinyinHelper.hasMultiPinyin(givenHanZi);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return check;
    }

    /**
     * 简体转换为繁体
     *
     * @param givenHanZi
     * @return
     */
    public static String changeToTraditional(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = ChineseHelper.convertToTraditionalChinese(givenHanZi);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }
   /**
     * 繁体转换为简体
     *
     * @param givenHanZi
     * @return
     */
    public static String changeToSimplified(String givenHanZi) {
        String tempStr = null;
        try {
            tempStr = ChineseHelper.convertToSimplifiedChinese(givenHanZi);
        } catch (Exception e) {
            log.error( "转换异常信息:", e);
        }
        return tempStr;
    }

}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值