PinYinUtils.java 4.2 KB

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