PinYinUtils.java 4.2 KB

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