This article illustrates the usage of Chinese pinyin pinyin4j in Java. Share to everyone for your reference, specific as follows:
Chinese pinyin conversion is a common problem in the daily development. For example, our great 12306, in the place name input "WH", will appear "Wuhan" "Wuhu" "Weihai" and other place names, input "Wuhu" will appear "Wuhu".
Java to get the pinyin of Chinese characters, pinyin4j This library can be a good solution to this problem.
Download Address: http://sourceforge.net/projects/pinyin4j/
Download decompression, there is a pinyin4j-2.5.0.jar, use this library can be.
Chinese Pinyin:
Copy Code code as follows:
string[] Pinyin = Pinyinhelper.tohanyupinyinstringarray (' heavy ');
The above line of code is a single kanji, such as the word "heavy," which returns an array of type string:
"Zhong4"
"Chong2"
"Heavy" is a pronunciation, and the return array of the method contains the phonetic alphabet of all pronunciations of the word. At the end of each pronunciation there is a number that is the tone (the first second, the third sound, no explanation).
Above is the simplest way to get a single Chinese character, and you can use Hanyupinyinoutputformat to format the returned phonetic alphabet.
Hanyupinyinoutputformat format = new Hanyupinyinoutputformat ();
Uppercase: Uppercase (ZHONG)
//lowercase: lowercase (ZHONG)
format.setcasetype (hanyupinyincasetype.lowercase);
Without_tone: No phonetic transcription (zhong)
//with_tone_number:1-4 Digital Representation (ZHONG4)
//With_tone_mark: Directly with the phonetic symbol (must be with_u_ Unicode otherwise abnormal) (Zhòng)
format.settonetype (Hanyupinyintonetype.with_tone_mark);
With_v: V for U (NV)
//With_u_and_colon: With "u:" Means U (nu:)
//With_u_unicode: Direct with U (nü)
Format.setvchartype (Hanyupinyinvchartype.with_u_unicode);
string[] Pinyin = Pinyinhelper.tohanyupinyinstringarray (' heavy ', format);
Tohanyupinyinstringarray returns null if the passed-in character is not a kanji and cannot be converted to pinyin.
Although the pinyin4j is very useful, but there are limitations. The above code can only get the pinyin of a single Chinese character, but cannot get a phonetic word that contains pronunciation. For example, "Chongqing", can not determine in the end is "Chongqing" or "zhongqing", pinyin4j can not be judged by the context of the pronunciation of pronunciation.
So, in getting a word that contains pronunciation, you can return a list, the correct pronunciation can only be artificial judgment choice.
I hope this article will help you with Java programming.