使用的库: pinyin4j

 

使用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: 暑假来波机器学习入门!!   因暑假有机会参与项目,去实习了两个月,

 

posted on 2017-05-25 21:37  小明在努力  阅读(3804)  评论(0编辑  收藏  举报