PinYinUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.yihu.jw.utils;
  2. import com.alibaba.fastjson.JSONArray;
  3. import net.sourceforge.pinyin4j.PinyinHelper;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  9. import org.apache.commons.lang3.StringUtils;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * @author huangwenjie
  14. * @date 2020/3/13 09:32
  15. */
  16. public class PinYinUtils {
  17. public static final Map<String, String> multiToneMap = new HashMap<>();
  18. static {
  19. multiToneMap.put("重庆", "C");
  20. multiToneMap.put("广东", "G");
  21. multiToneMap.put("广西", "G");
  22. multiToneMap.put("厦门", "X");
  23. }
  24. //将中文转换为英文
  25. public static String getEname(String name) throws Exception
  26. {
  27. HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
  28. pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  29. pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  30. pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
  31. return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
  32. }
  33. //姓、名的第一个字母需要为大写
  34. public static String getUpEname(String name) throws Exception {
  35. char[] strs = name.toCharArray();
  36. String newname = null;
  37. //名字的长度
  38. if (strs.length == 2) {
  39. newname = toUpCase(getEname("" + strs[0])) + " "
  40. + toUpCase(getEname("" + strs[1]));
  41. } else if (strs.length == 3)
  42. {
  43. newname = toUpCase(getEname("" + strs[0])) + " "
  44. + toUpCase(getEname("" + strs[1] + strs[2]));
  45. }
  46. else if (strs.length == 4)
  47. {
  48. newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
  49. + toUpCase(getEname("" + strs[2] + strs[3]));
  50. } else
  51. {
  52. newname = toUpCase(getEname(name));
  53. }
  54. return newname;
  55. }
  56. //首字母大写
  57. private static String toUpCase(String str) {
  58. StringBuffer newstr = new StringBuffer();
  59. newstr.append((str.substring(0, 1)).toUpperCase()).append(
  60. str.substring(1, str.length()));
  61. return newstr.toString();
  62. }
  63. /**
  64. * 获取汉字串拼音首字母,英文字符不变
  65. *
  66. * @param chinese 汉字串
  67. * @return 汉语拼音首字母
  68. */
  69. public static String getFirstSpell(String chinese) {
  70. StringBuffer pybf = new StringBuffer();
  71. char[] arr = chinese.toCharArray();
  72. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  73. defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  74. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  75. //只取第一个汉字的拼音
  76. if (arr[0] > 128) {
  77. try {
  78. String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[0], defaultFormat);
  79. // 长度大于1,表明是多音字,往后多取一个字或两个字,取词组,取匹配多音字属性文件确定最终读音
  80. if (temp != null && temp.length > 1 && !temp[0].equalsIgnoreCase(temp[1])) {
  81. String finalLetter = getOrgNameMultiTone(chinese);
  82. if(StringUtils.isEmpty(finalLetter)){
  83. System.out.print(arr[0]);
  84. System.out.print(arr[1]+":[");
  85. for(int i=0; i< temp.length-1; i++)
  86. System.out.print(temp[i]+", ");
  87. System.out.println(temp[temp.length-1]+"]");
  88. pybf.append(temp[0].charAt(0));
  89. }else{
  90. pybf.append(finalLetter.charAt(0));
  91. }
  92. } else {
  93. pybf.append(temp[0].charAt(0));
  94. }
  95. } catch (BadHanyuPinyinOutputFormatCombination e) {
  96. e.printStackTrace();
  97. }
  98. } else {
  99. pybf.append(arr[0]);
  100. }
  101. return pybf.toString().replaceAll("\\W", "").trim();
  102. }
  103. /**
  104. * 获取医院多音字的读音,取词组,最多取三个
  105. *
  106. * @param chinesePhrase 中文词组
  107. * @return
  108. */
  109. public static String getOrgNameMultiTone(String chinesePhrase) {
  110. try{
  111. if (multiToneMap.containsKey(chinesePhrase.substring(0, 1))) {
  112. return multiToneMap.get(chinesePhrase.substring(0, 1));
  113. } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 2))) {
  114. return multiToneMap.get(chinesePhrase.substring(0, 2));
  115. } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 3))) {
  116. return multiToneMap.get(chinesePhrase.substring(0, 3));
  117. }
  118. }catch (Exception e){
  119. }
  120. return null;
  121. }
  122. public static void main(String[] args) throws Exception{
  123. /*String r = "[\"钟公庙街道\",\n" +
  124. "\"中河街道\",\n" +
  125. "\"章水镇\",\n" +
  126. "\"云龙镇\",\n" +
  127. "\"鄞江镇\",\n" +
  128. "\"曕岐镇\",\n" +
  129. "\"咸祥镇\",\n" +
  130. "\"下应街道\",\n" +
  131. "\"五乡镇\",\n" +
  132. "\"塘溪镇\",\n" +
  133. "\"首南街道\",\n" +
  134. "\"石碶\",\n" +
  135. "\"邱隘镇\",\n" +
  136. "\"梅墟\",\n" +
  137. "\"龙观乡\",\n" +
  138. "\"姜山镇\",\n" +
  139. "\"集士港镇\",\n" +
  140. "\"横溪镇\",\n" +
  141. "\"横街镇\",\n" +
  142. "\"古林镇\",\n" +
  143. "\"高桥镇\",\n" +
  144. "\"洞桥镇\",\n" +
  145. "\"东吴镇\",\n" +
  146. "\"东钱湖镇\"]";
  147. JSONArray array = JSONArray.parseArray(r);
  148. for (int i=0;i<array.size();i++){
  149. System.out.println(getFirstSpell(array.getString(i)));
  150. }
  151. */
  152. System.out.print(getFirstSpell("曕岐镇"));
  153. }
  154. }