PinYinUtils.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.yihu.jw.utils;
  2. import net.sourceforge.pinyin4j.PinyinHelper;
  3. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  7. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  8. import org.apache.commons.lang3.StringUtils;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * @author huangwenjie
  13. * @date 2020/3/13 09:32
  14. */
  15. public class PinYinUtils {
  16. public static final Map<String, String> multiToneMap = new HashMap<>();
  17. static {
  18. multiToneMap.put("重庆", "C");
  19. multiToneMap.put("广东", "G");
  20. multiToneMap.put("广西", "G");
  21. multiToneMap.put("厦门", "X");
  22. }
  23. //将中文转换为英文
  24. public static String getEname(String name) throws Exception
  25. {
  26. HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
  27. pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  28. pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  29. pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
  30. return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
  31. }
  32. //姓、名的第一个字母需要为大写
  33. public static String getUpEname(String name) throws Exception {
  34. char[] strs = name.toCharArray();
  35. String newname = null;
  36. //名字的长度
  37. if (strs.length == 2) {
  38. newname = toUpCase(getEname("" + strs[0])) + " "
  39. + toUpCase(getEname("" + strs[1]));
  40. } else if (strs.length == 3)
  41. {
  42. newname = toUpCase(getEname("" + strs[0])) + " "
  43. + toUpCase(getEname("" + strs[1] + strs[2]));
  44. }
  45. else if (strs.length == 4)
  46. {
  47. newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
  48. + toUpCase(getEname("" + strs[2] + strs[3]));
  49. } else
  50. {
  51. newname = toUpCase(getEname(name));
  52. }
  53. return newname;
  54. }
  55. //首字母大写
  56. private static String toUpCase(String str) {
  57. StringBuffer newstr = new StringBuffer();
  58. newstr.append((str.substring(0, 1)).toUpperCase()).append(
  59. str.substring(1, str.length()));
  60. return newstr.toString();
  61. }
  62. /**
  63. * 获取汉字串拼音首字母,英文字符不变
  64. *
  65. * @param chinese 汉字串
  66. * @return 汉语拼音首字母
  67. */
  68. public static String getFirstSpell(String chinese) {
  69. StringBuffer pybf = new StringBuffer();
  70. char[] arr = chinese.toCharArray();
  71. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  72. defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
  73. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  74. //只取第一个汉字的拼音
  75. if (arr[0] > 128) {
  76. try {
  77. String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[0], defaultFormat);
  78. // 长度大于1,表明是多音字,往后多取一个字或两个字,取词组,取匹配多音字属性文件确定最终读音
  79. if (temp != null && temp.length > 1 && !temp[0].equalsIgnoreCase(temp[1])) {
  80. String finalLetter = getOrgNameMultiTone(chinese);
  81. if(StringUtils.isEmpty(finalLetter)){
  82. System.out.print(arr[0]);
  83. System.out.print(arr[1]+":[");
  84. for(int i=0; i< temp.length-1; i++)
  85. System.out.print(temp[i]+", ");
  86. System.out.println(temp[temp.length-1]+"]");
  87. pybf.append(temp[0].charAt(0));
  88. }else{
  89. pybf.append(finalLetter.charAt(0));
  90. }
  91. } else {
  92. pybf.append(temp[0].charAt(0));
  93. }
  94. } catch (BadHanyuPinyinOutputFormatCombination e) {
  95. e.printStackTrace();
  96. }
  97. } else {
  98. pybf.append(arr[0]);
  99. }
  100. return pybf.toString().replaceAll("\\W", "").trim();
  101. }
  102. /**
  103. * 获取医院多音字的读音,取词组,最多取三个
  104. *
  105. * @param chinesePhrase 中文词组
  106. * @return
  107. */
  108. public static String getOrgNameMultiTone(String chinesePhrase) {
  109. try{
  110. if (multiToneMap.containsKey(chinesePhrase.substring(0, 1))) {
  111. return multiToneMap.get(chinesePhrase.substring(0, 1));
  112. } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 2))) {
  113. return multiToneMap.get(chinesePhrase.substring(0, 2));
  114. } else if (multiToneMap.containsKey(chinesePhrase.substring(0, 3))) {
  115. return multiToneMap.get(chinesePhrase.substring(0, 3));
  116. }
  117. }catch (Exception e){
  118. }
  119. return null;
  120. }
  121. public static void main(String[] args) throws Exception{
  122. System.out.println(getFirstSpell("广西"));
  123. }
  124. }