zoukankan      html  css  js  c++  java
  • 汉字转拼音(汉语拼音)util

    请注意: 该util目前只是将汉字转为拼音,并无特殊功能,例如将"银行"是转为"yinxing",而不是"yinhang"

    使用pinyin4j-2.5.1.jar  commons-lang3-3.7.jar   

    maven依赖为

    <dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
    </dependency>


    代码如下:
    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;
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

    /**
    * @author zad
    * @date 2020/2/14 17:10
    * @description
    */
    public class PinYinUtil {

    private static final HanyuPinyinOutputFormat PINYIN_OUTPUT_FORMAT = new HanyuPinyinOutputFormat();
    private static final String EMPTY = "";

    static {
    PINYIN_OUTPUT_FORMAT.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    PINYIN_OUTPUT_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    PINYIN_OUTPUT_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_V);
    }

    private PinYinUtil() {
    }

    public static String getPinYin(String chineseCharacter) {
    return getPinYin(chineseCharacter, false);
    }


    public static String getPinYinHeadChar(String chineseCharacter) {
    return getPinYin(chineseCharacter, true);
    }

    private static String getPinYin(String raw, boolean onlyHeader) {
    if (StringUtil.isBlank(raw)) {
    return EMPTY;
    }
    StringBuilder sb = new StringBuilder();
    for (char cc : raw.toCharArray()) {
    if (addPinYin(sb, cc, onlyHeader)) {
    //
    } else {
    addLetterOrNumber(sb, cc);
    }
    }
    return sb.toString();
    }


    private static boolean addLetterOrNumber(StringBuilder sb, char raw) {
    if (isCharacterOrDigits(raw)) {
    sb.append(raw);
    return true;
    }
    return false;
    }

    private static boolean addPinYin(StringBuilder sb, char c, boolean onlyHeader) {
    if (isChineseCharacter(c)) {
    if (onlyHeader) {
    sb.append(getPinYinHeader(c));
    } else {
    sb.append(getPinYin(c));
    }
    return true;
    }
    return false;
    }


    private static char getPinYinHeader(char raw) {
    String res = doGetPinYinString(raw);
    if (StringUtil.isNotEmpty(res)) {
    return res.charAt(0);
    }
    return Character.MIN_VALUE;
    }

    private static String getPinYin(char raw) {
    return StringUtil.defaultIfEmpty(doGetPinYinString(raw), EMPTY);
    }

    private static String doGetPinYinString(char cc) {
    try {
    String[] res = PinyinHelper.toHanyuPinyinStringArray(cc, PINYIN_OUTPUT_FORMAT);
    return res[0];
    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
    // do nothing
    }
    return EMPTY;
    }

    private static boolean isChineseCharacter(char c) {
    return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
    }

    private static boolean isCharacterOrDigits(char c) {
    return Character.isLetterOrDigit(c);
    }

    }
     
  • 相关阅读:
    IIS打开本地站点时,无法访问本地json文件的解决办法
    几种流行的前端框架(BootStrap、Layui、Element-UI、Mint UI、Angular、Vue.js、React)
    六大排序算法:插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序
    SpringBoot框架开发的优秀的项目「值得收藏学习」
    jmeter接口之json提取器应用
    【设计模式(23)】行为型模式之访问者模式
    【设计模式(22)】行为型模式之模板模式
    【设计模式(21)】行为型模式之策略模式
    HTML回忆笔记,给那些忘了但又没完全忘的人准备的
    vscode创建html文件使用"!+tab"不起作用的解决方法
  • 原文地址:https://www.cnblogs.com/zad27/p/9900007.html
Copyright © 2011-2022 走看看