一、問題描述
漢字轉化為對應的拼音或者獲取漢字拼音的首字母,這些都是在開發(fā)中經(jīng)常遇到的問題,在獲取漢字的拼音或者拼音的首字母之后,我們在推薦或者搜索部門可以很大程度提高用戶的體驗,比如用戶輸入“NH”,我們就可以聯(lián)想出“你好”、“你會”、“年后”、“內(nèi)涵”等詞語。在Java中,pinyin4j.jar這個工具很好實現(xiàn)了將漢字轉化為對應的拼音,下面我們就介紹下如何使用這個jar包。
二、資源下載
下載之后解壓,直接使用文件中的pinyin4j-2.5.0.jar即可。
三、提供方法
我們可以使用HanyuPinyinOutputFormat類來設置拼音的返回方式,比如設置拼音的大小寫、音標方式以及拼音ü的顯示形式,具體如下圖:
直接使用PinyinHelper中的方法來對漢字做對應的轉化,具體有如下三種,三種效果如何自己做下測試即可:
四、編寫代碼
針對我們平常可能用到的功能,我做了如下的封裝,提供的功能還有具體的實現(xiàn)步驟參照代碼中的注釋:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package com.lulei.util; import java.util.ArrayList; import java.util.List; 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; public class PinYinUtil { private static HanyuPinyinOutputFormat format = null ; static { format = new HanyuPinyinOutputFormat(); //拼音小寫 format.setCaseType(HanyuPinyinCaseType.LOWERCASE); //無音標方式;WITH_TONE_NUMBER:1-4數(shù)字表示英標;WITH_TONE_MARK:直接用音標符(必須WITH_U_UNICODE否則異常 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); //用v表示ü format.setVCharType(HanyuPinyinVCharType.WITH_V); } /** * @param str * @return * @Description: 返回字符串的拼音 */ public static String[] getCharPinYinString(String str) { if (str == null || str.length() < 1 ) { return null ; } List<String> result = new ArrayList<String>(); //對字符串中的記錄逐個分析 for ( int i = 0 ; i < str.length(); i++) { result = getCharPinYinString(str.charAt(i), result); } return result.toArray( new String[result.size()]); } /** * @param c * @param list * @return * @Description: 將字符c的拼音拼接到list中的記錄中 */ private static List<String> getCharPinYinString( char c, List<String> list) { String[] strs = getCharPinYinString(c); List<String> result = new ArrayList<String>(); //如果解析出的拼音為空,判斷字符C是否為英文字母,如果是英文字母則添加值拼音結果中 if (strs == null ) { if ((c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' )) { c = c <= 91 ? ( char )(c + 32 ) : c; if (list == null || list.size() == 0 ) { result.add(c + "" ); } else { for (String s : list) { result.add(s + c); } } return result; } return list; } //將字符C的拼音首和已存在的拼音首組合成新的記錄 for (String str : strs) { if (list == null || list.size() == 0 ) { result.add(str); } else { for (String s : list) { result.add(s + str); } } } return result; } /** * @param c * @return * @Description: 返回漢字的拼音 */ public static String[] getCharPinYinString( char c) { try { //返回字符C的拼音 return PinyinHelper.toHanyuPinyinStringArray(c, format); } catch (Exception e) { e.printStackTrace(); } return null ; } /** * @param str * @return * @Description: 返回字符串的拼音的首字母 */ public static String[] getCharPinYinChar(String str) { if (str == null || str.length() < 1 ) { return null ; } List<String> result = new ArrayList<String>(); //對字符串中的記錄逐個分析 for ( int i = 0 ; i < str.length(); i++) { result = getCharPinYinChar(str.charAt(i), result); } return result.toArray( new String[result.size()]); } /** * @param c * @param list * @return * @Description: 將字符c的拼音首字母拼接到list中的記錄中 */ private static List<String> getCharPinYinChar( char c, List<String> list) { char [] chars = getCharPinYinChar(c); List<String> result = new ArrayList<String>(); //如果解析出的拼音為空,判斷字符C是否為英文字母,如果是英文字母則添加值拼音結果中 if (chars == null ) { if ((c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' )) { c = c < 91 ? ( char )(c + 32 ) : c; if (list == null || list.size() == 0 ) { result.add(c + "" ); } else { for (String s : list) { result.add(s + c); } } return result; } return list; } //將字符C的拼音首字母和已存在的拼音首字母組合成新的記錄 for ( char ch : chars) { if (list == null || list.size() == 0 ) { result.add(ch + "" ); } else { for (String s : list) { result.add(s + ch); } } } return result; } /** * @param c * @return * @Description:返回漢字拼音首字母 */ public static char [] getCharPinYinChar( char c) { //字符C的拼音 String[] strs = getCharPinYinString(c); if (strs != null ) { //截取拼音的首字母 char [] chars = new char [strs.length]; for ( int i = 0 ; i <chars.length; i++) { chars[i] = strs[i].charAt( 0 ); } return chars; } return null ; } public static void main(String[] args) { // TODO Auto-generated method stub char c = "重慶" .charAt( 0 ); String[] str = PinYinUtil.getCharPinYinString(c); for (String s : str) { System.out.println(s); } char [] chars = PinYinUtil.getCharPinYinChar(c); for ( char c1 : chars) { System.out.println(c1); } str = PinYinUtil.getCharPinYinString( "重慶c" ); for (String s : str) { System.out.println(s); } str = PinYinUtil.getCharPinYinChar( "重慶a" ); for (String s : str) { System.out.println(s); } } } |
五、輸出結果
以上就是java實現(xiàn)漢字轉拼音的全部內(nèi)容,希望對大家的學習有所幫助。