pinyin4j的主页:http://pinyin4j.sourceforge.net/
pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制。
pinyin4j是一个支持将中文转换到拼音的Java开源类库。

  1. 支持简体中文和繁体中文字符;
  2. 支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字;
  3. 支持多音字,即可以获取一个中文字符的多种发音;
  4. 支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 "ˉ",阳平"ˊ",上声"ˇ",去声"ˋ")的输出。

示例代码:

[java] view plain copy
  1. public class Pinyin4jUtil {
  2. /**
  3. * 汉字转换位汉语拼音首字母,英文字符不变,特殊字符丢失 支持多音字,生成方式如(长沙市长:cssc,zssz,zssc,cssz)
  4. *
  5. * @param chines
  6. *            汉字
  7. * @return 拼音
  8. */
  9. public static String converterToFirstSpell(String chines) {
  10. StringBuffer pinyinName = new StringBuffer();
  11. char[] nameChar = chines.toCharArray();
  12. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  13. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  14. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  15. for (int i = 0; i < nameChar.length; i++) {
  16. if (nameChar[i] > 128) {
  17. try {
  18. // 取得当前汉字的所有全拼
  19. String[] strs = PinyinHelper.toHanyuPinyinStringArray(
  20. nameChar[i], defaultFormat);
  21. if (strs != null) {
  22. for (int j = 0; j < strs.length; j++) {
  23. // 取首字母
  24. pinyinName.append(strs[j].charAt(0));
  25. if (j != strs.length - 1) {
  26. pinyinName.append(",");
  27. }
  28. }
  29. }
  30. // else {
  31. // pinyinName.append(nameChar[i]);
  32. // }
  33. } catch (BadHanyuPinyinOutputFormatCombination e) {
  34. e.printStackTrace();
  35. }
  36. } else {
  37. pinyinName.append(nameChar[i]);
  38. }
  39. pinyinName.append(" ");
  40. }
  41. // return pinyinName.toString();
  42. return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
  43. }
  44. /**
  45. * 汉字转换位汉语全拼,英文字符不变,特殊字符丢失
  46. * 支持多音字,生成方式如(重当参:zhongdangcen,zhongdangcan,chongdangcen
  47. * ,chongdangshen,zhongdangshen,chongdangcan)
  48. *
  49. * @param chines
  50. *            汉字
  51. * @return 拼音
  52. */
  53. public static String converterToSpell(String chines) {
  54. StringBuffer pinyinName = new StringBuffer();
  55. char[] nameChar = chines.toCharArray();
  56. HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
  57. defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  58. defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  59. for (int i = 0; i < nameChar.length; i++) {
  60. if (nameChar[i] > 128) {
  61. try {
  62. // 取得当前汉字的所有全拼
  63. String[] strs = PinyinHelper.toHanyuPinyinStringArray(
  64. nameChar[i], defaultFormat);
  65. if (strs != null) {
  66. for (int j = 0; j < strs.length; j++) {
  67. pinyinName.append(strs[j]);
  68. if (j != strs.length - 1) {
  69. pinyinName.append(",");
  70. }
  71. }
  72. }
  73. } catch (BadHanyuPinyinOutputFormatCombination e) {
  74. e.printStackTrace();
  75. }
  76. } else {
  77. pinyinName.append(nameChar[i]);
  78. }
  79. pinyinName.append(" ");
  80. }
  81. // return pinyinName.toString();
  82. return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
  83. }
  84. /**
  85. * 去除多音字重复数据
  86. *
  87. * @param theStr
  88. * @return
  89. */
  90. private static List<Map<String, Integer>> discountTheChinese(String theStr) {
  91. // 去除重复拼音后的拼音列表
  92. List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>();
  93. // 用于处理每个字的多音字,去掉重复
  94. Map<String, Integer> onlyOne = null;
  95. String[] firsts = theStr.split(" ");
  96. // 读出每个汉字的拼音
  97. for (String str : firsts) {
  98. onlyOne = new Hashtable<String, Integer>();
  99. String[] china = str.split(",");
  100. // 多音字处理
  101. for (String s : china) {
  102. Integer count = onlyOne.get(s);
  103. if (count == null) {
  104. onlyOne.put(s, new Integer(1));
  105. } else {
  106. onlyOne.remove(s);
  107. count++;
  108. onlyOne.put(s, count);
  109. }
  110. }
  111. mapList.add(onlyOne);
  112. }
  113. return mapList;
  114. }
  115. /**
  116. * 解析并组合拼音,对象合并方案(推荐使用)
  117. *
  118. * @return
  119. */
  120. private static String parseTheChineseByObject(
  121. List<Map<String, Integer>> list) {
  122. Map<String, Integer> first = null; // 用于统计每一次,集合组合数据
  123. // 遍历每一组集合
  124. for (int i = 0; i < list.size(); i++) {
  125. // 每一组集合与上一次组合的Map
  126. Map<String, Integer> temp = new Hashtable<String, Integer>();
  127. // 第一次循环,first为空
  128. if (first != null) {
  129. // 取出上次组合与此次集合的字符,并保存
  130. for (String s : first.keySet()) {
  131. for (String s1 : list.get(i).keySet()) {
  132. String str = s + s1;
  133. temp.put(str, 1);
  134. }
  135. }
  136. // 清理上一次组合数据
  137. if (temp != null && temp.size() > 0) {
  138. first.clear();
  139. }
  140. } else {
  141. for (String s : list.get(i).keySet()) {
  142. String str = s;
  143. temp.put(str, 1);
  144. }
  145. }
  146. // 保存组合数据以便下次循环使用
  147. if (temp != null && temp.size() > 0) {
  148. first = temp;
  149. }
  150. }
  151. String returnStr = "";
  152. if (first != null) {
  153. // 遍历取出组合字符串
  154. for (String str : first.keySet()) {
  155. returnStr += (str + ",");
  156. }
  157. }
  158. if (returnStr.length() > 0) {
  159. returnStr = returnStr.substring(0, returnStr.length() - 1);
  160. }
  161. return returnStr;
  162. }
  163. }

测试代码

[java] view plain copy
  1. <span style="white-space:pre">  </span>    String str = "长沙市长";
  2. String pinyin = Pinyin4jUtil.converterToSpell(str);
  3. System.out.println(str+" pin yin :"+pinyin);
  4. pinyin = Pinyin4jUtil.converterToFirstSpell(str);
  5. System.out.println(str+" short pin yin :"+pinyin); <p style="margin-top:0px; margin-bottom:25px; line-height:28.8px; color:rgb(64,64,64); font-family:"Microsoft YaHei",Verdana,sans-serif,SimSun; font-size:16px"><span style="line-height:26px; font-family:Arial; font-size:14px">运行结果:</span></p><p style="margin-top:0px; margin-bottom:25px; line-height:28.8px; color:rgb(64,64,64); font-family:"Microsoft YaHei",Verdana,sans-serif,SimSun; font-size:16px"><span style="line-height:26px; font-family:Arial; font-size:14px">长沙市长 pin yin :zhangshashichang,changshashichang,zhangshashizhang,changshashizhang<br style="line-height:10px">长沙市长 short pin yin :cssc,zssz,zssc,cssz<br style="line-height:10px"></span></p><div><span style="line-height:26px; font-family:Arial; font-size:14px">
  6. </span></div>

pinyin4j使用示例(支持多音字)相关推荐

  1. pinyin4j使用示例

    pinyin4j的主页:http://pinyin4j.sourceforge.net/ pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制 pinyin4j是一个支持将中文转换 ...

  2. java pinyin4j 官网_Java中文转汉语拼音类库pinyin4j使用示例

    pinyin4j的java开源类库,提供中文转汉语拼音(并且支持多音字) pinyin4j官方网址:http://pinyin4j.sourceforge.net/ import java.util. ...

  3. android T9 搜索联系人分析与实现(支持多音字)

    最近在android项目开发过程中需要实现类似电话拨号功能,这里涉及到T9键盘搜索联系人,于是研究了一番,将思路和心得记录于此,方便自己与他人. 相信大家对T9输入法并不陌生,这里并不涉及到T9输入法 ...

  4. AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字)

    AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...

  5. 汉字转拼音,一二级词库,不支持多音字

    GB2312标准共收录6763个汉字,其中一级汉字3755个,二级汉字3008个. 分区表示  GB 2312中对所收汉字进行了"分区"处理,每区含有94个汉字/符号.这种表示方式 ...

  6. android Qwerty 键盘搜索联系人分析与实现(支持多音字)

    最近在项目开发的过程中不仅仅涉及到T9搜索联系人,后来发现还需要支持Qwerty键盘搜索联系人.上次我已经写了一篇关于T9搜索的博客<android T9 搜索联系人分析与实现(支持多音字)&g ...

  7. ElasticSearch pinyin分词支持多音字

    ElasticSearch pinyin分词支持多音字 背景 我们有一个搜索业务底层采用ElasticSearch作为搜索引擎,在索引的过程中,使用了ik中文分词.拼音分词.同义词等多种分词器.ES和 ...

  8. thinkphp5整合系列之汉字转拼音完美支持多音字

    thinkphp5整合系列之汉字转拼音完美支持多音字 1.进入thinkphp5项目的根目录,用composer 安装扩展 2.接下来需要做的事就是在控制器中引入 /vendor/overtrue/p ...

  9. android 拼音搜索联系人分析与实现(支持多音字,支持T9搜索,支持Qwerty搜索)

    最近的项目开发过程中涉及到了联系人T9键盘搜索和Qwerty键盘搜索,也分别写了相关的博客: <android T9 搜索联系人分析与实现(支持多音字)> <android Qwer ...

最新文章

  1. 分布式锁的实现方式——ACID数据库、缓存或者是zk
  2. 3.4 参数展开-机器学习笔记-斯坦福吴恩达教授
  3. java.lang包中的常用类
  4. 网络编程 数据的封装与解封装过程
  5. JS快速获取图片宽高的方法
  6. python selenium 判断元素是否可见
  7. 一步步编写操作系统 25 cpu的保护模式
  8. onclick 源码_精读:手写React框架 解析Hooks源码
  9. Leetcode 206.反转链表(双指针迭代法和递归操作)
  10. Java开发的可扩展-高性能-响应式的API网关-soul v2.2.1
  11. python天气查询_Python学习笔记——天气查询代码
  12. 面试题--------5、==与equals的区别
  13. 关闭计算机主机还亮着,计算机关闭后,主机电源指示灯仍然亮起
  14. PPT——背景图格式(排版)和字体排版
  15. 我希望进入大学时就能知道的一些事儿 -----作者:瞬息之间
  16. pytorch实现LeNet5手写数字识别+各层特征图可视化
  17. word中“项目符号”和后面的文字间隔太远
  18. 【知识图谱】Neo4j 导入数据构建知识图谱的三种方法
  19. 关于数据仓库中变化历史数据处理方式:全量表、快照表、拉链表
  20. html table边框加粗,table加边框记录

热门文章

  1. Excel调用MD5加密
  2. 国产热门操作系统Deepin试用感受
  3. classD类和classAB类功放区别
  4. IPEndPoint 和 EndPoint
  5. linux 动态库文件stripped属性理解
  6. 诊断和响应故障_验证数据库文件和备份
  7. 全球与中国的前50家最佳网站
  8. 这款免费开源的数据库工具,支持所有主流数据库!
  9. matplotlib和seaborn中的颜色图(colormap)和调色板(color palette)
  10. Python「可视化编程插件」让编程更easy