码迷,mamicode.com
首页 > 其他好文 > 详细

汉字转拼音

时间:2017-05-25 23:37:33      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:联系   div   技术分享   返回   深度学习   todo   exception   github   input   

使用的库: pinyin4j

链接:http://pan.baidu.com/s/1gf23Nkn 密码:b4sf

 

使用pinyin4j获取汉字的简拼/全拼示例:

 1 package prinyin4j;
 2 
 3 import java.util.Scanner;
 4 import net.sourceforge.pinyin4j.PinyinHelper;
 5 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
 6 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 7 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 8 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
 9 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;;
10 
11 public class Main {
12 
13     static Scanner input = new Scanner(System.in);
14 
15     public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
16         
17         HanyuPinyinOutputFormat hypyfm = new HanyuPinyinOutputFormat();
18         hypyfm.setCaseType(HanyuPinyinCaseType.LOWERCASE);
19         hypyfm.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
20         hypyfm.setVCharType(HanyuPinyinVCharType.WITH_V);
21         
22         System.out.println("请输入联系人的名字:");
23         String str = input.next();
24         char[] ch = str.toCharArray();
25         StringBuilder fullPrint = new StringBuilder();
26         StringBuilder simplePrint = new StringBuilder();
27         for (int i = 0; i < ch.length; ++i) {
28             String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch[i],hypyfm);
29             simplePrint.append(temp[0].charAt(0) );
30             fullPrint.append(temp[0]);
31         }
32         System.out.println(simplePrint);
33         System.out.println(fullPrint.toString());
34     }
35 
36 }

运行截图:

技术分享

 

不过还有个问题:

技术分享

厦门的简拼和全拼错了,厦(xia)被发音成厦(sha)!!!

事实上,只要是有两种以上发音的字,就存在这种风险.

 

ps: 更佳的汉字转拼音java开源类库 https://github.com/stuxuhai/jpinyin

技术分享

 

当然,JPinyin并没有修复多音字的缺陷,233...

 

pinyin4j对单个汉字进行处理,多音字会返回多个读音,必然产生这种风险

然而要根据语境进行准确发音,这太难了,即使对人类来说,这都不是件简单的事,

但总有办法,能让机器产生类似人类的选择行为 , 机器学习 or 深度学习 or AI !!!

 

TODO: 暑假来波机器学习入门!!

 

汉字转拼音

标签:联系   div   技术分享   返回   深度学习   todo   exception   github   input   

原文地址:http://www.cnblogs.com/lfm1996/p/6906095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!