• 汉字转拼音(汉语拼音)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);
    }

    }
     
  • 相关阅读:
    taro clock组件
    创建taro项目
    ts声明各种变量类型
    ts的数组/元组/type/interface
    使用styled-components初始化css
    Oracle profile含义、修改、新增
    JDK bin指令
    Nginx 设置忽略favicon.ico文件的错误日志
    nginx: [error] CreateFile() "D: ginx-1.14.2/logs/nginx.pid" failed 解决办法
    Nginx Windows详细安装部署教程
  • 原文地址:https://www.cnblogs.com/zad27/p/9900007.html
Copyright © 2020-2023  润新知