java姓名转拼音(多音字处理)

1.下载jar包

点击下载地址
下载成功后添加到项目lib中
image

2.编写工具类

import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
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 liukuiyong
 * 2018-05-23
 *
 */
public class PinyinUtils {
        
    private static String path= "/pinyin.properties";

    /**
     * 将字符串中的中文转化为拼音,英文字符不变
     *
     * @param inputString 患者姓名
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String output = "";
        if (inputString != null && inputString.length() > 0
                && !"null".equals(inputString)) {
            String  substring = diffPro(inputString);
            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);
                        output += temp[0]+" ";
                    } else{
                        output += java.lang.Character.toString(input[i]);
                    }
                    output = output;
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            
            
        } else {
            return "";
        }
        return output;
    }

    /**
     * 汉字转换位汉语拼音首字母,英文字符不变
     *
     * @param chines 汉字
     * @return 拼音首字母
     */
    public static String converterToFirstSpell(String chines) {
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(
                            nameChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }
    
    /**
     * 与pinyin.properties中多音字的姓名对比
     * @param inputString 中文姓名
     * @return 百家姓的拼音读法 
     */
    private static String diffPro(String inputString){
        Properties properties = new Properties();
        try {
             InputStreamReader reader =new InputStreamReader(PinyinUtils.class.getResourceAsStream(path),"utf-8");
            properties.load(reader);
            reader.close();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
           Set keyValue = properties.keySet();
           for (Iterator it = keyValue.iterator(); it.hasNext();){
               String key = (String) it.next();
    
               if(inputString.substring(0, key.length()).equals(key)){
                  String value =  inputString.substring(key.length(),inputString.length());
                   return properties.getProperty(key)+" "+value;
               }
          }
        return inputString;
    }
}

3.配置文件

image.png
配置文件如果放到其他目录中需要在PinyinUtils中修改路径(path)
如果有多音字姓名可以在配置文件中按照下面的格式追加
区=OU
缪=MIAO
晟=CHENG
乐=YUE
员=YUN
贠=YUN
黑=HE
重=CHONG
仇=QIU
秘=BI
冼=XIAN
解=XIE
折=SHE
单=SHAN
朴=PIAO
翟=ZHA
查=ZHA
盖=GE
万俟=MO QI
尉迟=YU CHI

4.测试

public class Test {

    public static void main(String[] args) {
            String strs = PinyinUtils.getPingYin("单雄信");
            String strs1 = PinyinUtils.getPingYin("王单");
            System.out.println("输出结果:"+strs+"\n输出结果:"+strs1);
    }
}

5.结果

输出结果:SHAN XIONG XIN 
输出结果:WANG DAN 

有问题请留言,欢迎大家一起探讨,谢谢

你可能感兴趣的:(java姓名转拼音(多音字处理))