|  | @ -5,12 +5,21 @@ 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 org.springframework.util.Assert;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 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<>();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	 //将中文转换为英文
 | 
	
		
			
				|  |  | 	public static String getEname(String name) throws  Exception
 | 
	
		
			
				|  |  |     {
 | 
	
	
		
			
				|  | @ -55,6 +64,66 @@ public class PinYinUtils {
 | 
	
		
			
				|  |  | 		
 | 
	
		
			
				|  |  | 		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 = 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) {
 | 
	
		
			
				|  |  | 		Assert.notNull(chinesePhrase, "中文词组不可为空");
 | 
	
		
			
				|  |  | 		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));
 | 
	
		
			
				|  |  | 		}
 | 
	
		
			
				|  |  | 		return null;
 | 
	
		
			
				|  |  | 	}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 	public static void main(String[] args)  throws  Exception{
 | 
	
		
			
				|  |  | 		System.out.println(getUpEname("李宇春"));
 | 
	
		
			
				|  |  | 		
 |