汉字转拼音的使用手册
来源:互联网 发布:淘宝会员名取不了 编辑:程序博客网 时间:2024/04/29 00:17
Pinyin4j使用手册
一般用法
pinyin4j的使用很方便,一般转换只需要使用PinyinHelper类的静态工具方法即可:
String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(‘刘’);
//该类还有其他的拼音转换形式,但是基本上用不到,就不介绍了
返回的数组即是该字符的拼音,如上例就是pinyin[0]=liu2,后面的数字代表声调,声调为5表示轻读,无声调。之所谓返回数组,是因为被判定的汉字有可能有多个读音。如果输入的参数不是汉字,则返回null。
拼音格式化
如果对于拼音转换后的结果有一些特定的格式要求目前pinyin4j支持:
l 声调格式化。例如:“刘”字的格式化后为“liu2”或“liu”或“liú”
l 对特殊拼音ü的的显示格式。例如“u:”或“v”或“ü”
l 大小写的转换。例如:“liu2”或“LIU2”
以上这些格式可以混合使用,下面就来介绍具体的使用方法,首先需要创建格式化对象HanyuPinyinOutputFormat,例如:
HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
然后分别调用outputFormat的set方法设置上述一些格式要求:
设置声调格式:
outputFormat.setToneType(HanyuPinyinToneType);
方法参数HanyuPinyinToneType有以下常量对象:
HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调,例如:liu2
HanyuPinyinToneType.WITHOUT_TONE 无声调表示,例如:liu
HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示,例如:liú
设置特殊拼音ü的显示格式:
outputFormat.setVCharType(HanyuPinyinVCharType);
方法参数HanyuPinyinVCharType有以下常量对象:
HanyuPinyinVCharType.WITH_U_AND_COLON 以U和一个冒号表示该拼音,例如:lu:
HanyuPinyinVCharType.WITH_V 以V表示该字符,例如:lv
HanyuPinyinVCharType.WITH_U_UNICODE 以ü表示
设置大小写格式
outputFormat.setCaseType(HanyuPinyinCaseType);
HanyuPinyinCaseType.LOWERCASE 转换后以全小写方式输出
HanyuPinyinCaseType.UPPERCASE 转换后以全大写方式输出
设置好格式对象后还是利用上述的工具类方法进行拼音转换,只不过需要将格式化对象当成方法参数传入转换方法,告知要转换的格式要求:
String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(‘刘’, outputFormat);
但该方法会有异常抛出,注意处理。
示例
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;
publicclass Test {
publicstaticvoid main(String[] args) {
HanyuPinyinOutputFormat outputFormat =new HanyuPinyinOutputFormat();
outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
try {
System.out.println(concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray('刘',outputFormat)));
} catch (BadHanyuPinyinOutputFormatCombination e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
privatestatic String concatPinyinStringArray(String[] pinyinArray)
{
StringBuffer pinyinStrBuf =new StringBuffer();
if ((null != pinyinArray) && (pinyinArray.length > 0))
{
for (int i = 0; i < pinyinArray.length; i++)
{
pinyinStrBuf.append(pinyinArray[i]);
pinyinStrBuf.append(System.getProperty("line.separator"));
}
}
String outputString = pinyinStrBuf.toString();
return outputString;
}
}
输出结果为:LIÚ
其他
l PinyinHelper还有其他的静态方法,但示例和讲解中使用的方法是常见的拼音格式,因此其他静态方法我没有调研其含义。
l 貌似支持生僻字,我试过很怪异的字,都可以将其读音拼写出来,因此这个工具包还是很强的。
- 汉字转拼音的使用手册
- c# 汉字转拼音(完整的拼音)
- c#的汉字转拼音
- c#的汉字转拼音
- 汉字转拼音的类
- 汉字转拼音的类
- 汉字转拼音的类
- 汉字转拼音的代码
- 汉字转拼音的类
- 汉字转拼音的实现
- 转一个汉字转拼音的类
- 汉字转拼音缩写的函数(VB)
- 汉字转拼音缩写的函数(VB)
- 汉字转拼音缩写的代码
- 汉字转拼音的静态类
- 汉字转拼音缩写的函数(C#)
- 汉字转拼音缩写的函数(VB)
- 汉字转拼音缩写的函数(C#)
- 在tableview中选种一行,当失去焦点的时候选种行的颜色自动消失
- Android 3.0 r1中文API文档(103) —— InputMethodManager [输入法]
- 设置MySql允许外部连接的方法
- pop-click噪声
- 更改cell选中后的颜色
- 汉字转拼音的使用手册
- python对文件加载到内存进行索引缓存
- 让label自适应里面的文字,自动调整宽度和高度。
- 帮父母揉肩捶背,轻松脊椎
- android 软键盘 InputMethodManager
- 算法系列15天速成——第十一天 树操作(上)
- Handler用法
- 无法定位程序输入点_except_handler4_common于动态链接库msvcrt.dll上
- 解决eclipse svn插件不能识别自己svn服务器中项目的问题(点team没有相关功能)