汉字转拼音类库(pinyin4j的使用)

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

 

转载于:https://www.cnblogs.com/lfm1996/p/6906095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值