JAVA 中文名字转拼音

1. maven 添加依赖

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

2. java 工具类

package com.taichu.util;


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;
import java.util.HashMap;


public class PinyinUtils {
    private static final String WORD_SEPARATOR = "";
    private static final String SURNAME_NAME_SEPARATOR = "/";

    private static HashMap<String, String> map = new HashMap<>();
    private static final String COMPLEX_SURNAME = "欧阳,太史,端木,上官,司马,东方,独孤,南宫,万俟,闻人,夏侯,诸葛,尉迟," +
            "公羊,赫连,澹台,皇甫,宗政,濮阳,公冶,太叔,申屠,公孙,慕容,仲孙,钟离,长孙,宇文,司徒,鲜于,司空,闾丘,子车,亓官,司寇,巫马,公西,颛孙," +
            "壤驷,公良,漆雕,乐正,宰父,谷梁,拓跋,夹谷,轩辕,令狐,段干,百里,呼延,东郭,南门,羊舌,微生,公户,公玉,公仪,梁丘,公仲,公上,公门,公山," +
            "公坚,左丘,公伯,西门,公祖,第五,公乘,贯丘,公皙,南荣,东里,东宫,仲长,子书,子桑,即墨,达奚,褚师,吴铭,";

    static {
        map.put("秘", "BI");
        map.put("晟", "CHENG");
        map.put("重", "CHONG");
        map.put("都", "DU");
        map.put("阿", "E");
        map.put("盖", "GE");
        map.put("句", "GOU");
        map.put("黑", "HE");
        map.put("郇", "HUAN");
        map.put("缪", "MIAO");
        map.put("能", "NAI");
        map.put("区", "OU");
        map.put("朴", "PIAO");
        map.put("繁", "PO");
        map.put("覃", "QIN");
        map.put("仇", "QIU");
        map.put("瞿", "QU");
        map.put("单", "SHAN");
        map.put("折", "SHE");
        map.put("冼", "XIAN");
        map.put("解", "XIE");
        map.put("乐", "YUE");
        map.put("员", "YUN");
        map.put("贠", "YUN");
        map.put("曾", "ZENG");
        map.put("查", "ZHA");
        map.put("翟", "ZHAI");
        map.put("万俟", "MOQI");
        map.put("尉迟", "YUCHI");
    }

    public static String getPinyinName(String name) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        if (name == null || name.length() == 0)
        {
            return "";
        }
        String ret = "";
        String  substring = getNameAndSurname(name);
        char[] input = substring.trim().toCharArray();
        try {
            for (int i = 0; i < input.length; i++) {
                if (java.lang.Character.toString(input[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                            input[i], format);
                    ret += temp[0] + WORD_SEPARATOR;
                } else{
                    ret += java.lang.Character.toString(input[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return ret;
    }


    private static String getNameAndSurname(String name){
        if (name == null || name.length() < 2){
            return name;
        }
        String key = name.substring(0,2);
        int index = COMPLEX_SURNAME.contains(key + ",")? 2 :1;
        key = name.substring(0,index);
        String content = name.substring(index);
        if (map.containsKey(key)){
            return map.get(key) + SURNAME_NAME_SEPARATOR + content;
        }
        return key + SURNAME_NAME_SEPARATOR + content;
    }
}

3. 测试方法

public class PinyinMain {
    public static void main(String[] args){
        String src = "单田芳";
        String des = PinyinUtils.getPinyinName(src);
        System.out.println(des);


        src = "尉迟敬德";
        des = PinyinUtils.getPinyinName(src);
        System.out.println(des);

        src = "张三";
        des = PinyinUtils.getPinyinName(src);
        System.out.println(des);

        src = "司徒雪";
        des = PinyinUtils.getPinyinName(src);
        System.out.println(des);
    }
}

4. 测试结果

SHAN/TIANFANG
YUCHI/JINGDE
ZHANG/SAN
SITU/XUE

猜你喜欢

转载自blog.csdn.net/qsyjrz206/article/details/130531740