|  | @ -6,118 +6,176 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
 | 
												
													
														
															|  | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 |  | import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 | 
												
													
														
															|  | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 |  | import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 | 
												
													
														
															|  | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 |  | import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 | 
												
													
														
															|  | 
 |  | import org.apache.commons.lang3.StringUtils;
 | 
												
													
														
															|  | 
 |  | import org.springframework.core.io.ClassPathResource;
 | 
												
													
														
															|  | 
 |  | import org.springframework.core.io.Resource;
 | 
												
													
														
															|  | 
 |  | import org.springframework.util.Assert;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | import java.util.ArrayList;
 |  | 
 | 
												
													
														
															|  | import java.util.LinkedHashMap;
 |  | 
 | 
												
													
														
															|  | import java.util.List;
 |  | 
 | 
												
													
														
															|  | import java.util.Map;
 |  | 
 | 
												
													
														
															|  | 
 |  | import java.io.BufferedReader;
 | 
												
													
														
															|  | 
 |  | import java.io.IOException;
 | 
												
													
														
															|  | 
 |  | import java.io.InputStreamReader;
 | 
												
													
														
															|  | 
 |  | import java.util.*;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | public class PinYinUtil {
 |  | public class PinYinUtil {
 | 
												
													
														
															|  | 	/** 
 |  | 
 | 
												
													
														
															|  |      * 获取汉字串拼音首字母,英文字符不变 
 |  | 
 | 
												
													
														
															|  |      * @param chinese 汉字串 
 |  | 
 | 
												
													
														
															|  |      * @return 汉语拼音首字母 
 |  | 
 | 
												
													
														
															|  |      */ 
 |  | 
 | 
												
													
														
															|  |     public static String getFirstSpell(String chinese) { 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     public static final Map<String, String> multiToneMap = new HashMap<>();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     static {
 | 
												
													
														
															|  | 
 |  |         Resource resource = new ClassPathResource("multitone.properties");
 | 
												
													
														
															|  | 
 |  |         BufferedReader br = null;
 | 
												
													
														
															|  | 
 |  |         try {
 | 
												
													
														
															|  | 
 |  |             br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "GBK"));
 | 
												
													
														
															|  | 
 |  |             String line = null;
 | 
												
													
														
															|  | 
 |  |             while (null != (line = br.readLine())) {
 | 
												
													
														
															|  | 
 |  |                 if (!StringUtils.startsWith(line, "#")) {
 | 
												
													
														
															|  | 
 |  |                     String oneLine = line;
 | 
												
													
														
															|  | 
 |  |                     String[] lineArr = oneLine.split("=");
 | 
												
													
														
															|  | 
 |  |                     for (String key : lineArr[1].split("/")) {
 | 
												
													
														
															|  | 
 |  |                         multiToneMap.put(key, lineArr[0]);
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         } catch (IOException e) {
 | 
												
													
														
															|  | 
 |  |             e.printStackTrace();
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     public static void main(String[] args) {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         System.out.println(StringUtils.startsWith("######### S ##########", "#"));
 | 
												
													
														
															|  | 
 |  |         System.out.println(multiToneMap);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取汉字串拼音首字母,英文字符不变
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @param chinese 汉字串
 | 
												
													
														
															|  | 
 |  |      * @return 汉语拼音首字母
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public static String getFirstSpell(String chinese) {
 | 
												
													
														
															|  |         StringBuffer pybf = new StringBuffer();
 |  |         StringBuffer pybf = new StringBuffer();
 | 
												
													
														
															|  |         char[] arr = chinese.toCharArray();
 |  |         char[] arr = chinese.toCharArray();
 | 
												
													
														
															|  |         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 |  |         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 | 
												
													
														
															|  |         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 |  | 
 | 
												
													
														
															|  | 
 |  |         defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
 | 
												
													
														
															|  |         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 |  |         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 | 
												
													
														
															|  |             for (int i = 0; i < arr.length; i++) {
 |  | 
 | 
												
													
														
															|  |                     if (arr[i] > 128) {
 |  | 
 | 
												
													
														
															|  |                             try {
 |  | 
 | 
												
													
														
															|  |                                     String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
 |  | 
 | 
												
													
														
															|  |                                     if (temp != null) {
 |  | 
 | 
												
													
														
															|  |                                             pybf.append(temp[0].charAt(0));
 |  | 
 | 
												
													
														
															|  |                                     }
 |  | 
 | 
												
													
														
															|  |                             } catch (BadHanyuPinyinOutputFormatCombination e) {
 |  | 
 | 
												
													
														
															|  |                                     e.printStackTrace();
 |  | 
 | 
												
													
														
															|  |                             }
 |  | 
 | 
												
													
														
															|  |                     } else {
 |  | 
 | 
												
													
														
															|  |                             pybf.append(arr[i]);
 |  | 
 | 
												
													
														
															|  |                     }
 |  | 
 | 
												
													
														
															|  | 
 |  |         //只取第一个汉字的拼音
 | 
												
													
														
															|  | 
 |  |         if (arr[0] > 128) {
 | 
												
													
														
															|  | 
 |  |             try {
 | 
												
													
														
															|  | 
 |  |                 String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[0], defaultFormat);
 | 
												
													
														
															|  | 
 |  |                 // 长度大于1,表明是多音字,往后多取一个字或两个字,取词组,取匹配多音字属性文件确定最终读音
 | 
												
													
														
															|  | 
 |  |                 if (temp != null && temp.length > 1) {
 | 
												
													
														
															|  | 
 |  |                     pybf.append(getOrgNameMultiTone(chinese).charAt(0));
 | 
												
													
														
															|  | 
 |  |                 } else {
 | 
												
													
														
															|  | 
 |  |                     pybf.append(temp[0].charAt(0));
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             } catch (BadHanyuPinyinOutputFormatCombination e) {
 | 
												
													
														
															|  | 
 |  |                 e.printStackTrace();
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  |             return pybf.toString().replaceAll("\\W", "").trim();
 |  | 
 | 
												
													
														
															|  |     } 
 |  | 
 | 
												
													
														
															|  | 
 |  |         } else {
 | 
												
													
														
															|  | 
 |  |             pybf.append(arr[0]);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         return pybf.toString().replaceAll("\\W", "").trim();
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /** 
 |  | 
 | 
												
													
														
															|  |      * 获取汉字串拼音,英文字符不变 
 |  | 
 | 
												
													
														
															|  |      * @param chinese 汉字串 
 |  | 
 | 
												
													
														
															|  |      * @return 汉语拼音 
 |  | 
 | 
												
													
														
															|  |      */ 
 |  | 
 | 
												
													
														
															|  |     public static String getFullSpell(String chinese) { 
 |  | 
 | 
												
													
														
															|  |             StringBuffer pybf = new StringBuffer(); 
 |  | 
 | 
												
													
														
															|  |             char[] arr = chinese.toCharArray(); 
 |  | 
 | 
												
													
														
															|  |             HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
 |  | 
 | 
												
													
														
															|  |             defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
 |  | 
 | 
												
													
														
															|  |             defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
 |  | 
 | 
												
													
														
															|  |             for (int i = 0; i < arr.length; i++) { 
 |  | 
 | 
												
													
														
															|  |                     if (arr[i] > 128) { 
 |  | 
 | 
												
													
														
															|  |                             try { 
 |  | 
 | 
												
													
														
															|  |                                     pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); 
 |  | 
 | 
												
													
														
															|  |                             } catch (BadHanyuPinyinOutputFormatCombination e) { 
 |  | 
 | 
												
													
														
															|  |                                     e.printStackTrace(); 
 |  | 
 | 
												
													
														
															|  |                             } 
 |  | 
 | 
												
													
														
															|  |                     } else { 
 |  | 
 | 
												
													
														
															|  |                             pybf.append(arr[i]); 
 |  | 
 | 
												
													
														
															|  |                     } 
 |  | 
 | 
												
													
														
															|  |             } 
 |  | 
 | 
												
													
														
															|  |             return pybf.toString(); 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取汉字串拼音,英文字符不变
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @param chinese 汉字串
 | 
												
													
														
															|  | 
 |  |      * @return 汉语拼音
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public static String getFullSpell(String chinese) {
 | 
												
													
														
															|  | 
 |  |         StringBuffer pybf = new StringBuffer();
 | 
												
													
														
															|  | 
 |  |         char[] arr = chinese.toCharArray();
 | 
												
													
														
															|  | 
 |  |         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 | 
												
													
														
															|  | 
 |  |         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 | 
												
													
														
															|  | 
 |  |         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 | 
												
													
														
															|  | 
 |  |         for (int i = 0; i < arr.length; i++) {
 | 
												
													
														
															|  | 
 |  |             if (arr[i] > 128) {
 | 
												
													
														
															|  | 
 |  |                 try {
 | 
												
													
														
															|  | 
 |  |                     pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
 | 
												
													
														
															|  | 
 |  |                 } catch (BadHanyuPinyinOutputFormatCombination e) {
 | 
												
													
														
															|  | 
 |  |                     e.printStackTrace();
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             } else {
 | 
												
													
														
															|  | 
 |  |                 pybf.append(arr[i]);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         return pybf.toString();
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |      * 字符串按拼音排序
 |  |      * 字符串按拼音排序
 | 
												
													
														
															|  |      * @param list
 |  | 
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @param
 | 
												
													
														
															|  |      * @return
 |  |      * @return
 | 
												
													
														
															|  |      */
 |  |      */
 | 
												
													
														
															|  |     public static Map<String,List<Object>> pingYinSort(List<String> list){
 |  | 
 | 
												
													
														
															|  |             Map<String,List<Object>>  map =  new LinkedHashMap<>();
 |  | 
 | 
												
													
														
															|  |             String[] alphatables = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
 |  | 
 | 
												
													
														
															|  |                     "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
 |  | 
 | 
												
													
														
															|  |             for (String alpha : alphatables){
 |  | 
 | 
												
													
														
															|  |                     for(String one:list){
 |  | 
 | 
												
													
														
															|  |                             String firstSpell = PinYinUtil.getFirstSpell(one).substring(0,1);
 |  | 
 | 
												
													
														
															|  |                             if (alpha.equals(firstSpell)){
 |  | 
 | 
												
													
														
															|  |                                     List<Object> resultList = null;
 |  | 
 | 
												
													
														
															|  |                                     if(map.get(alpha)==null){
 |  | 
 | 
												
													
														
															|  |                                             resultList = new ArrayList();
 |  | 
 | 
												
													
														
															|  |                                     }else{
 |  | 
 | 
												
													
														
															|  |                                             resultList = map.get(alpha);
 |  | 
 | 
												
													
														
															|  |                                     }
 |  | 
 | 
												
													
														
															|  |                                     resultList.add(one);
 |  | 
 | 
												
													
														
															|  |                                     map.put(alpha,resultList);
 |  | 
 | 
												
													
														
															|  |                             }
 |  | 
 | 
												
													
														
															|  | 
 |  |     public static Map<String, Object> pingYinSort(Map<String, Map<String, Object>> result) {
 | 
												
													
														
															|  | 
 |  |         Map<String, Object> map = new LinkedHashMap<>();
 | 
												
													
														
															|  | 
 |  |         String[] alphatables = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
 | 
												
													
														
															|  | 
 |  |                 "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
 | 
												
													
														
															|  | 
 |  |         if (result.size() > 0) {
 | 
												
													
														
															|  | 
 |  |             List<Map<String, Object>> list = Lists.newArrayList(result.values());
 | 
												
													
														
															|  | 
 |  |             for (String alpha : alphatables) {
 | 
												
													
														
															|  | 
 |  |                 List<Map<String, Object>> resultList = new ArrayList<>();
 | 
												
													
														
															|  | 
 |  |                 for (Map<String, Object> one : list) {
 | 
												
													
														
															|  | 
 |  |                     String name = String.valueOf(one.get("name"));
 | 
												
													
														
															|  | 
 |  |                     String firstSpell = PinYinUtil.getFirstSpell(name).substring(0, 1);
 | 
												
													
														
															|  | 
 |  |                     if (alpha.equals(firstSpell)) {
 | 
												
													
														
															|  | 
 |  |                         resultList.add(one);
 | 
												
													
														
															|  | 
 |  |                         map.put(alpha, resultList);
 | 
												
													
														
															|  |                     }
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  |             return map;
 |  |             return map;
 | 
												
													
														
															|  | 
 |  |         } else {
 | 
												
													
														
															|  | 
 |  |             return map;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |     * 字符串按拼音排序
 |  | 
 | 
												
													
														
															|  |     * @param
 |  | 
 | 
												
													
														
															|  |     * @return
 |  | 
 | 
												
													
														
															|  |     */
 |  | 
 | 
												
													
														
															|  |     public static Map<String,Object> pingYinSort(Map<String, Map<String,Object>> result){
 |  | 
 | 
												
													
														
															|  |             Map<String,Object>  map =  new LinkedHashMap <>();
 |  | 
 | 
												
													
														
															|  |             String[] alphatables = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
 |  | 
 | 
												
													
														
															|  |                     "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
 |  | 
 | 
												
													
														
															|  |             if (result.size() > 0) {
 |  | 
 | 
												
													
														
															|  |                     List<Map<String,Object>> list = Lists.newArrayList(result.values());
 |  | 
 | 
												
													
														
															|  |                     for (String alpha : alphatables){
 |  | 
 | 
												
													
														
															|  |                             List<Map<String,Object>> resultList = new ArrayList<>();
 |  | 
 | 
												
													
														
															|  |                             for (Map<String,Object> one : list){
 |  | 
 | 
												
													
														
															|  |                                     String name = String.valueOf(one.get("name"));
 |  | 
 | 
												
													
														
															|  |                                     String firstSpell = PinYinUtil.getFirstSpell(name).substring(0,1);
 |  | 
 | 
												
													
														
															|  |                                     if (alpha.equals(firstSpell)){
 |  | 
 | 
												
													
														
															|  |                                             resultList.add(one);
 |  | 
 | 
												
													
														
															|  |                                             map.put(alpha,resultList);
 |  | 
 | 
												
													
														
															|  |                                     }
 |  | 
 | 
												
													
														
															|  |                             }
 |  | 
 | 
												
													
														
															|  | 
 |  |      * 字符串按拼音排序
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @param
 | 
												
													
														
															|  | 
 |  |      * @return
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public static Map<String, Object> pingYinSort(List<Map<String, Object>> result) {
 | 
												
													
														
															|  | 
 |  |         Map<String, Object> map = new LinkedHashMap<>();
 | 
												
													
														
															|  | 
 |  |         String[] alphatables = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
 | 
												
													
														
															|  | 
 |  |                 "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
 | 
												
													
														
															|  | 
 |  |         if (result.size() > 0) {
 | 
												
													
														
															|  | 
 |  |             for (String alpha : alphatables) {
 | 
												
													
														
															|  | 
 |  |                 List<Map<String, Object>> resultList = new ArrayList<>();
 | 
												
													
														
															|  | 
 |  |                 for (Map<String, Object> one : result) {
 | 
												
													
														
															|  | 
 |  |                     String name = String.valueOf(one.get("name"));
 | 
												
													
														
															|  | 
 |  |                     String firstSpell = getFirstSpell(name).substring(0, 1);
 | 
												
													
														
															|  | 
 |  |                     if (alpha.equals(firstSpell)) {
 | 
												
													
														
															|  | 
 |  |                         resultList.add(one);
 | 
												
													
														
															|  | 
 |  |                         map.put(alpha, resultList);
 | 
												
													
														
															|  |                     }
 |  |                     }
 | 
												
													
														
															|  |                     return map;
 |  | 
 | 
												
													
														
															|  |             } else {
 |  | 
 | 
												
													
														
															|  |                     return map;
 |  | 
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  |             }
 |  |             }
 | 
												
													
														
															|  | 
 |  |             return map;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         return map;
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 获取医院多音字的读音,取词组,最多取三个
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @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;
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | }
 |  | }
 |