汉字转换成拼音的工具----pinyin4j
今天在无意中发现了pinyin4j这个强大的工具,它能帮助我们完成将汉字转换成拼音的工作,这给我们的开发带来了巨大的便利,意识到这一点,我果断的学习了。在开始记录我的学习成果前,我必须先感谢pinyin4j的作者Li Min
(作者详细信息不详),感谢你为开发者提供了这个工具。
先来看看pinyin4j有什么功能吧。
Pinyin4j是sourceforge.net上的一个开源项目,支持同一汉字有多个发音,还支持拼音的格式化输出,比如第几声之类的,同时支持简体中文、繁体中文转换为拼音。下面是其官方网址,其中提供了下载:http://pinyin4j.sourceforge.net/
下面就是我的学习成果演示了,可能存在诸多错误,希望大家能不吝指正,帮助我进步。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import net.sourceforge.pinyin4j.*;
public class Chinese2Pinyin {
/**
* @param args
* @throws BadHanyuPinyinOutputFormatCombination
*/
public static void main(String[] args)
throws BadHanyuPinyinOutputFormatCombination {
// TODO Auto-generated method stub
Chinese chinese = new Chinese();
String sentence = "自己选择的路,跪着也要走完" ;
char word = '中' ;
// 不使用格式化
String pinyins[] = chinese.getSingleWord(word);
System.out.println( "不使用格式化的单字测试:" );
for (String pinyin : pinyins) {
System.out.println(pinyin);
}
System.out.println( "不使用格式化的语句测试:" );
String result = chinese.getSentence(sentence);
System.out.println(result);
// 使用格式化
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
System.out.println( "使用格式化的单字测试:" );
String pinyins2[] = chinese.getSingleWord(word, format);
for (String pinyin : pinyins2) {
System.out.println(pinyin);
}
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
System.out.println( "使用格式化的语句测试:" );
String result2 = chinese.getSentence(sentence, format);
System.out.println(result2);
}
}
class Chinese {
public Chinese() {
}
// 测试单字,不使用格式化方式
public String[] getSingleWord( char word) {
return PinyinHelper.toHanyuPinyinStringArray(word);
}
// 测试单字,使用格式化方式
public String[] getSingleWord( char word, HanyuPinyinOutputFormat format)
throws BadHanyuPinyinOutputFormatCombination {
return PinyinHelper.toHanyuPinyinStringArray(word, format);
}
// 测试语句,不使用格式方式
public String getSentence(String sentence) {
StringBuilder sb = new StringBuilder();
for ( int i = 0 , len = sentence.length(); i < len; i++) {
char ch = sentence.charAt(i);
String[] temp = getSingleWord(ch);
if (temp == null ) {
sb.append(ch + ' ' );
} else {
sb.append(temp[ 0 ] + ' ' );
}
}
return sb.toString();
}
// 测试语句,使用格式化方式
public String getSentence(String sentence, HanyuPinyinOutputFormat format)
throws BadHanyuPinyinOutputFormatCombination {
StringBuilder sb = new StringBuilder();
for ( int i = 0 , len = sentence.length(); i < len; i++) {
char ch = sentence.charAt(i);
String[] temp = getSingleWord(ch, format);
if (temp == null ) {
sb.append(ch + ' ' );
} else {
sb.append(temp[ 0 ] + ' ' );
}
}
return sb.toString();
}
}
|
测试结果如下:
OK,上面是我做的简单的测试程序,在做程序的时候我想到一个问题,如何来判断文字在语句中的读音该是怎样的呢?我认为这是这个工具需要再添加的功能,如果有了这个功能,那么我们就可以很方便的将汉语文章用拼音注解,这也就为小孩子提供了更多的阅读物了。
package com.bq.action;import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public class PinyinMain {public static void main(String[] args) {System.out.println(stringToPinYin("大修包"));}public static String charToPinYin(char c) {HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();format.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 设置大小写format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不输出音调format.setVCharType(HanyuPinyinVCharType.WITH_V); // 拼音中的u输出为v 例如lvtry {// 返回汉字的拼音,如果为多音字则返回所有读音String[] result = PinyinHelper.toHanyuPinyinStringArray(c, format);if (result == null) {return null; // 如果传入的不是汉字,例如A,则返回数组为null} else {return result[0]; // 返回汉字的第一个读音}} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();return null;}}public static String stringToPinYin(String str) {StringBuilder sb = new StringBuilder();for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (charToPinYin(c) == null) {sb.append(c);} else {sb.append(charToPinYin(c));}}return sb.toString();}
}
汉字转换成拼音的工具----pinyin4j相关推荐
- hive 转拼音udf_自定义UDF函数:将汉字转换成拼音
工作需求要讲汉字转换成拼音,自定义UDF函数 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- 自定义UDF函数:将汉字转换成拼音
工作需求要讲汉字转换成拼音,自定义UDF函数 import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j ...
- C#下汉字转换成拼音
因为一个开发需求需要把汉字转换成拼音,没想到还真的可以! 找了一个类,是别人整理的,默认情况下第一个字母是大写的.我这边需要都是小写的,可以按照自己情况进行实际的需求进行修改,比如有些时候只要首字母就 ...
- 将中文汉字转换成拼音(全拼)
using System.Globalization; using System.Text; using System.Text.RegularExpressions;/// <summary& ...
- 关于xamarin汉字转换成拼音
c# 中关于汉字转换成拼音 现在虽然是xamarin.from 但是大多是代码还是c#的.在点击title排序时候发现汉字不能正确排序.最后我是把汉字取首字母排序的. 发现一篇很有用的文章:http: ...
- java如何把汉字转换成拼音
如何把汉字转换成拼音,听上去好像有难度,但是网上搜了一下,发现有这方面的工具包,叫做pin4j,怎么用呢? /*** * @param zhongwen 传入的中文字符串* @return 返回转换成 ...
- javascript汉字转换成拼音
javascript汉字转换成拼音 分类 JavaScript 2007-9-27 7:16:38 浏览 13791 回复 0 隐藏侧边栏 转自 www.div-css.com . 阅读 编辑 运行 ...
- Android汉字转换成拼音
今天介绍一个好用的类,把中文转换成拼音,具体代码如下: /**** An object to convert Chinese character to its corresponding pinyin ...
- 汉字转拼音 python_python把汉字转换成拼音实现程序
python把汉字转换成拼音实现程序 文章一个简洁干的汉字转拼音 程序,复制下载就可以直接使用,有需要的同学可以参考一下下. #coding:utf-8 #基于python2.6 table = 'a ...
最新文章
- 在网页中画Icon小图标
- Android Studio3.0中dependencies依赖由compile变为implementation的区别
- 基于torch.nn.functional.conv2d实现CNN
- 获取行信息_论文推荐 | 周乐韬,黄丁发,袁林果,等:基于状态和残差的北斗基准站观测数据表达与信息分级...
- mysql sql in or 替换_sql IN 的用法一例--替换 mysql longtext字段中某些内容的用法
- 机器学习速成课程 | 练习 | Google Development——编程练习:使用 TensorFlow 的起始步骤
- python建立一个字符串_python字符串基本方法
- 波士顿学院计算机科学专业,波士顿学院专业有哪些?
- Android做的第一个小程序
- python从键盘输入一个数n、输出大于n且不能整除3_python基础练习题
- android 滑动tabhost,tabhost左右滑动按钮
- ShadowGun Billboard Blinking God Rays
- 惠普m128fn中文说明书_惠普M128fp中文说明书
- 【IT软技能】如何使用中文维基百科
- 移动互联网感言(董烨/Joven.Dong)
- “大数据”加盟“网格化”管理
- decodeURIComponent() 方法
- 魔兽世界9.5人口最多服务器,魔兽世界最新全球服务器人口普查,国服早已不是世界第一人口大服...
- JIRA软件使用入门
- Python+Django毕业设计学校旧书交易网站(程序+LW+部署)