一般用法
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);
但該方法會有異常拋出,注意處理。
示例
1 import net.sourceforge.pinyin4j.PinyinHelper; 2 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 3 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 4 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 5 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; 6 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 7 public class Test { 8 public static void main(String[] args) { 9 import net.sourceforge.pinyin4j.PinyinHelper; 10 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 11 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 12 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 13 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; 14 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 15 public class Test { 16 public static void main(String[] args) { 17 HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat(); 18 outputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); 19 outputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); 20 outputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE); 21 try { 22 System.out.println(concatPinyinStringArray(PinyinHelper.toHanyuPinyinStringArray('劉',outputFormat))); 23 } catch (BadHanyuPinyinOutputFormatCombination e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 } 28 29 private static String concatPinyinStringArray(String[] pinyinArray) 30 { 31 StringBuffer pinyinStrBuf = new StringBuffer(); 32 if ((null != pinyinArray) && (pinyinArray.length > 0)) 33 { 34 for (int i = 0; i < pinyinArray.length; i++) 35 { 36 pinyinStrBuf.append(pinyinArray[i]); 37 pinyinStrBuf.append(System.getProperty("line.separator")); 38 } 39 } 40 String outputString = pinyinStrBuf.toString(); 41 return outputString; 42 } 43 } 44 輸出結果為:LIÚ
其他
l PinyinHelper還有其他的靜態方法,但示例和講解中使用的方法是常見的拼音格式,因此其他靜態方法我沒有調研其含義。
l 貌似支持生僻字,我試過很怪異的字,都可以將其讀音拼寫出來,因此這個工具包還是很強的。
拼音工具
1 package cn.itcast.bos.utils; 2 3 import java.util.Arrays; 4 5 import net.sourceforge.pinyin4j.PinyinHelper; 6 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; 7 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; 8 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; 9 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; 10 11 public class PinYin4jUtils { 12 /** 13 * 將字符串轉換成拼音數組 14 * 15 * @param src 16 * @return 17 */ 18 public static String[] stringToPinyin(String src) { 19 return stringToPinyin(src, false, null); 20 } 21 22 /** 23 * 將字符串轉換成拼音數組 24 * 25 * @param src 26 * @return 27 */ 28 public static String[] stringToPinyin(String src, String separator) { 29 30 return stringToPinyin(src, true, separator); 31 } 32 33 /** 34 * 將字符串轉換成拼音數組 35 * 36 * @param src 37 * @param isPolyphone 38 * 是否查出多音字的所有拼音 39 * @param separator 40 * 多音字拼音之間的分隔符 41 * @return 42 */ 43 public static String[] stringToPinyin(String src, boolean isPolyphone, 44 String separator) { 45 // 判斷字符串是否為空 46 if ("".equals(src) || null == src) { 47 return null; 48 } 49 char[] srcChar = src.toCharArray(); 50 int srcCount = srcChar.length; 51 String[] srcStr = new String[srcCount]; 52 53 for (int i = 0; i < srcCount; i++) { 54 srcStr[i] = charToPinyin(srcChar[i], isPolyphone, separator); 55 } 56 return srcStr; 57 } 58 59 /** 60 * 將單個字符轉換成拼音 61 * 62 * @param src 63 * @return 64 */ 65 public static String charToPinyin(char src, boolean isPolyphone, 66 String separator) { 67 // 創建漢語拼音處理類 68 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 69 // 輸出設置,大小寫,音標方式 70 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 71 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 72 73 StringBuffer tempPinying = new StringBuffer(); 74 75 // 如果是中文 76 if (src > 128) { 77 try { 78 // 轉換得出結果 79 String[] strs = PinyinHelper.toHanyuPinyinStringArray(src, 80 defaultFormat); 81 82 // 是否查出多音字,默認是查出多音字的第一個字符 83 if (isPolyphone && null != separator) { 84 for (int i = 0; i < strs.length; i++) { 85 tempPinying.append(strs[i]); 86 if (strs.length != (i + 1)) { 87 // 多音字之間用特殊符號間隔起來 88 tempPinying.append(separator); 89 } 90 } 91 } else { 92 tempPinying.append(strs[0]); 93 } 94 95 } catch (BadHanyuPinyinOutputFormatCombination e) { 96 e.printStackTrace(); 97 } 98 } else { 99 tempPinying.append(src); 100 } 101 102 return tempPinying.toString(); 103 104 } 105 106 public static String hanziToPinyin(String hanzi) { 107 return hanziToPinyin(hanzi, " "); 108 } 109 110 /** 111 * 將漢字轉換成拼音 112 * 113 * @param hanzi 114 * @param separator 115 * @return 116 */ 117 public static String hanziToPinyin(String hanzi, String separator) { 118 119 // 創建漢語拼音處理類 120 HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 121 // 輸出設置,大小寫,音標方式 122 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 123 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 124 125 String pinyingStr = ""; 126 try { 127 pinyingStr = PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat, 128 separator); 129 } catch (BadHanyuPinyinOutputFormatCombination e) { 130 // TODO Auto-generated catch block 131 e.printStackTrace(); 132 } 133 return pinyingStr; 134 } 135 136 /** 137 * 將字符串數組轉換成字符串 138 * 139 * @param str 140 * @param separator 141 * 各個字符串之間的分隔符 142 * @return 143 */ 144 public static String stringArrayToString(String[] str, String separator) { 145 StringBuffer sb = new StringBuffer(); 146 for (int i = 0; i < str.length; i++) { 147 sb.append(str[i]); 148 if (str.length != (i + 1)) { 149 sb.append(separator); 150 } 151 } 152 return sb.toString(); 153 } 154 155 /** 156 * 簡單的將各個字符數組之間連接起來 157 * 158 * @param str 159 * @return 160 */ 161 public static String stringArrayToString(String[] str) { 162 return stringArrayToString(str, ""); 163 } 164 165 /** 166 * 將字符數組轉換成字符串 167 * 168 * @param str 169 * @param separator 170 * 各個字符串之間的分隔符 171 * @return 172 */ 173 public static String charArrayToString(char[] ch, String separator) { 174 StringBuffer sb = new StringBuffer(); 175 for (int i = 0; i < ch.length; i++) { 176 sb.append(ch[i]); 177 if (ch.length != (i + 1)) { 178 sb.append(separator); 179 } 180 } 181 return sb.toString(); 182 } 183 184 /** 185 * 將字符數組轉換成字符串 186 * 187 * @param str 188 * @return 189 */ 190 public static String charArrayToString(char[] ch) { 191 return charArrayToString(ch, " "); 192 } 193 194 /** 195 * 取漢字的首字母 196 * 197 * @param src 198 * @param isCapital 199 * 是否是大寫 200 * @return 201 */ 202 public static char[] getHeadByChar(char src, boolean isCapital) { 203 // 如果不是漢字直接返回 204 if (src <= 128) { 205 return new char[] { src }; 206 } 207 // 獲取所有的拼音 208 String[] pinyingStr = PinyinHelper.toHanyuPinyinStringArray(src); 209 210 // 創建返回對象 211 int polyphoneSize = pinyingStr.length; 212 char[] headChars = new char[polyphoneSize]; 213 int i = 0; 214 // 截取首字符 215 for (String s : pinyingStr) { 216 char headChar = s.charAt(0); 217 // 首字母是否大寫,默認是小寫 218 if (isCapital) { 219 headChars[i] = Character.toUpperCase(headChar); 220 } else { 221 headChars[i] = headChar; 222 } 223 i++; 224 } 225 226 return headChars; 227 } 228 229 /** 230 * 取漢字的首字母(默認是大寫) 231 * 232 * @param src 233 * @return 234 */ 235 public static char[] getHeadByChar(char src) { 236 return getHeadByChar(src, true); 237 } 238 239 /** 240 * 查找字符串首字母 241 * 242 * @param src 243 * @return 244 */ 245 public static String[] getHeadByString(String src) { 246 return getHeadByString(src, true); 247 } 248 249 /** 250 * 查找字符串首字母 251 * 252 * @param src 253 * @param isCapital 254 * 是否大寫 255 * @return 256 */ 257 public static String[] getHeadByString(String src, boolean isCapital) { 258 return getHeadByString(src, isCapital, null); 259 } 260 261 /** 262 * 查找字符串首字母 263 * 264 * @param src 265 * @param isCapital 266 * 是否大寫 267 * @param separator 268 * 分隔符 269 * @return 270 */ 271 public static String[] getHeadByString(String src, boolean isCapital, 272 String separator) { 273 char[] chars = src.toCharArray(); 274 String[] headString = new String[chars.length]; 275 int i = 0; 276 for (char ch : chars) { 277 278 char[] chs = getHeadByChar(ch, isCapital); 279 StringBuffer sb = new StringBuffer(); 280 if (null != separator) { 281 int j = 1; 282 283 for (char ch1 : chs) { 284 sb.append(ch1); 285 if (j != chs.length) { 286 sb.append(separator); 287 } 288 j++; 289 } 290 } else { 291 sb.append(chs[0]); 292 } 293 headString[i] = sb.toString(); 294 i++; 295 } 296 return headString; 297 } 298 299 public static void main(String[] args) { 300 // pin4j 簡碼 和 城市編碼 301 String s1 = "中華人民共和國"; 302 String[] headArray = getHeadByString(s1); // 獲得每個漢字拼音首字母 303 System.out.println(Arrays.toString(headArray)); 304 305 String s2 ="長城" ; 306 System.out.println(Arrays.toString(stringToPinyin(s2,true,","))); 307 308 String s3 ="長"; 309 System.out.println(Arrays.toString(stringToPinyin(s3,true,","))); 310 } 311 }
