您的位置:首页 > 其它

探索PinYin4j.jar将汉字转换为拼音的基本用法

2016-11-29 11:23 260 查看
将汉字转换为拼音在Android开发中是个很常见的问题。例如:在Android手机应用开发中,要查询联系人的姓名,通常都是用拼音进行查询的。

Pinyin4j是一个功能强悍的汉语拼音工具包,是sourceforge.net上的一个开源项目。

主要的功能有:

- 支持同一汉字有多个发音

- 支持拼音的格式化输出,比如第几声之类的

- 支持简体中文、繁体中文转换为拼音

首先,在Android Studio中的使用应该将pinyin4j的包放入到libs文件夹下,然后在需要使用到pinyin4j时就在类中import要使用的相应类即可。



检查在build.gradle中是否已经导入PinYin4j.jar的存放路径



1.基本用法(单个汉字转拼音):

PinyinHelper类中的静态方法toHanyuPinyinStringArray返回的数据类型是一个String数组,它用来接收一个汉字的多个发音,如果toHanyuPinyinStringArray中的参数不是汉字,那么它会返回null。
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray('行');
for (int i = 0; i < pinyinArray.length; ++i)
{
Log.d("TAG", pinyinArray[i]);
}
1
2
3
4
5
1
2
3
4
5
[/code]

结果输出:



可以看到“行”字有这么5种发音,后面的数字代表第几声。

2.格式支持

Pinyin4j支持拼音输出的格式化,比如,“行”字出了像前面输出“xing2”,也可以输出为“xing”或者“xíng”之类的其他格式。
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
String[] pinyinArray = null;
try {
pinyinArray = PinyinHelper.toHanyuPinyinStringArray('行', format);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
for (int i = 0; i < pinyinArray.length; ++i) {
Log.d("TAG", pinyinArray[i]);
}
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
[/code]

效果图如下:



其中使用HanyuPinyinOutputFormat来格式化返回拼音的格式还有例如以下几种:

- WITH_V:用v表示ü (nv)

- WITH_U_AND_COLON:用”u:”表示ü (nu:)

- WITH_U_UNICODE:直接用ü (nü)

–>使用:format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

- UPPERCASE:大写 (XING)

- LOWERCASE:小写 (xing)

–>使用:format.setCaseType(HanyuPinyinCaseType.LOWERCASE);

- WITHOUT_TONE:无音标 (xing)

- WITH_TONE_NUMBER:1-4数字表示英标 (xing2)

- WITH_TONE_MARK:直接用音标符

–>使用:format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);

虽然pinyin4j很好用,但是还是有局限的。以上代码只能获取单个汉字的拼音,但是不能获取一个包含多音字的词的拼音。例如“重庆”,无法判断到底是“chongqing”还是“zhongqing”,pinyin4j不能通过上下文来判断多音字的读音。

  所以,在获取一个包含多音字的词语的读音,可以返回一个列表,正确的读音只能是人工判断选择。

3.示例代码
package com.adan.pinyindome;

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;

/**
* @author: xiaolijuan
* @description:
* @projectName: PinyinDome
* @date: 2016-02-18
* @time: 10:13
*/
public class PinyinUtils {

/**
* 获得汉语拼音首字母
*
* @param chines 汉字
* @return
*/
public static String getAlpha(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}

/**
* 将字符串中的中文转化为拼音,英文字符不变
*
* @param inputString 汉字
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
String output = "";
if (inputString != null && inputString.length() > 0
&& !"null".equals(inputString)) {
char[] input = inputString.trim().toCharArray();
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
input[i], format);
output += temp[0];
} else
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
return "*";
}
return output;
}

/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines 汉字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}

}
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
99
100
101
102
103
104
105
106
107
108
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
99
100
101
102
103
104
105
106
107
108
[/code]

在实际开发中如果我们要将一段文字中的汉字全部转换成不带音调的拼音输出,而这段文字中又可能包含阿拉伯数字、英文、标点符号等等。如果完全靠自己写代码进行转换,那是非常麻烦的,其中一个首先就要区别,这段文字中那些是汉字,那些是非汉字。有了Pinyin4j,这个问题就不再困难了
String strs = PinyinUtils.getPingYin("新年好!Hello,新年大家都过得开心吧?哈哈,我是做Android开发哒,what's this?");
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(strs);
Log.d("TAG", strs);
1
2
3
4


1
2
3
4
[/code]

结果输出:

xinnianhao!Hello,xinniandajiadouguodekaixinba?haha,woshizuoAndroidkaifada,what’s this?

Dome下载

下载pinyin4j-2.5.0.jar

转载博客原文:http://blog.csdn.net/qq_20785431/article/details/50730342
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: