123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package com.yihu.jw.utils;
- 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 org.apache.commons.lang3.StringUtils;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author huangwenjie
- * @date 2020/3/13 09:32
- */
- public class PinYinUtils {
- public static final Map<String, String> multiToneMap = new HashMap<>();
- static {
- multiToneMap.put("重庆", "C");
- multiToneMap.put("广东", "G");
- multiToneMap.put("广西", "G");
- multiToneMap.put("厦门", "X");
- }
- //将中文转换为英文
- public static String getEname(String name) throws Exception
- {
- HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
- pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
-
- return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
- }
-
- //姓、名的第一个字母需要为大写
- public static String getUpEname(String name) throws Exception {
- char[] strs = name.toCharArray();
- String newname = null;
-
- //名字的长度
- if (strs.length == 2) {
- newname = toUpCase(getEname("" + strs[0])) + " "
- + toUpCase(getEname("" + strs[1]));
- } else if (strs.length == 3)
- {
- newname = toUpCase(getEname("" + strs[0])) + " "
- + toUpCase(getEname("" + strs[1] + strs[2]));
- }
- else if (strs.length == 4)
- {
- newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
- + toUpCase(getEname("" + strs[2] + strs[3]));
- } else
- {
- newname = toUpCase(getEname(name));
- }
- return newname;
- }
-
- //首字母大写
- private static String toUpCase(String str) {
- StringBuffer newstr = new StringBuffer();
- newstr.append((str.substring(0, 1)).toUpperCase()).append(
- str.substring(1, str.length()));
-
- return newstr.toString();
- }
- /**
- * 获取汉字串拼音首字母,英文字符不变
- *
- * @param chinese 汉字串
- * @return 汉语拼音首字母
- */
- public static String getFirstSpell(String chinese) {
- StringBuffer pybf = new StringBuffer();
- char[] arr = chinese.toCharArray();
- HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
- defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
- defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- //只取第一个汉字的拼音
- if (arr[0] > 128) {
- try {
- String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[0], defaultFormat);
- // 长度大于1,表明是多音字,往后多取一个字或两个字,取词组,取匹配多音字属性文件确定最终读音
- if (temp != null && temp.length > 1 && !temp[0].equalsIgnoreCase(temp[1])) {
- String finalLetter = getOrgNameMultiTone(chinese);
- if(StringUtils.isEmpty(finalLetter)){
- System.out.print(arr[0]);
- System.out.print(arr[1]+":[");
- for(int i=0; i< temp.length-1; i++)
- System.out.print(temp[i]+", ");
- System.out.println(temp[temp.length-1]+"]");
- pybf.append(temp[0].charAt(0));
- }else{
- pybf.append(finalLetter.charAt(0));
- }
- } else {
- pybf.append(temp[0].charAt(0));
- }
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- e.printStackTrace();
- }
- } else {
- pybf.append(arr[0]);
- }
- return pybf.toString().replaceAll("\\W", "").trim();
- }
- /**
- * 获取医院多音字的读音,取词组,最多取三个
- *
- * @param chinesePhrase 中文词组
- * @return
- */
- public static String getOrgNameMultiTone(String chinesePhrase) {
- try{
- if (multiToneMap.containsKey(chinesePhrase.substring(0, 1))) {
- return multiToneMap.get(chinesePhrase.substring(0, 1));
- } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 2))) {
- return multiToneMap.get(chinesePhrase.substring(0, 2));
- } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 3))) {
- return multiToneMap.get(chinesePhrase.substring(0, 3));
- }
- }catch (Exception e){
- }
- return null;
- }
- public static void main(String[] args) throws Exception{
- System.out.println(getFirstSpell("广西"));
- }
- }
|