Chinese character conversion Pinyin is a common problem in daily development. For example, if we enter "Wh" in the place name of "Great 12306", "Wuhan", "Wuhu", and "Weihai", and enter "Wuhu", the "Wuhu" will appear ".
Java obtains the Chinese pinyin,Pinyin4jThis library can solve this problem very well.
: Http://sourceforge.net/projects/pinyin4j/
Download unzip, there is a pinyin4j-2.5.0.jar inside, you can use this library.
Conversion of Chinese characters to PinYin:
String [] pinyin =Pinyinhelper. tohanyupinyinstringarray('Weight ');
The above line of code converts a single Chinese character to pinyin, for example, "heavy". This method returnsString Array:
"Zhong4"
"Chong2"
"Weight" is a Polyphonic word. The returned array of this method containsAll pronunciations of the wordPinyin. The last digit in each pronunciation is the tone (the first, the second, the third, and the fourth ).
The above is the simplest way to obtain a single Chinese character. You can also useHanyupinyinoutputformatReturns the pinyin format.
Hanyupinyinoutputformat format = new hanyupinyinoutputformat (); // uppercase: upper case (Zhong) // lowercase: lower case (Zhong) format. setcasetype (hanyupinyincasetype. lowercase); // without_tone: No phonetic symbol (Zhong) // with_tone_number: 1-4 digits to indicate the English logo (zhong4) // with_tone_mark: directly use the phonetic symbol (with_u_unicode is required otherwise abnormal) (zh ò NG) format. settonetype (hanyupinyintonetype. with_tone_mark); // with_v: Use V to represent U (NV) // with_u_and_colon: use U: "To represent U (Nu :) // with_u_unicode: use U (n u) directly) format. setvchartype (hanyupinyinvchartype. with_u_unicode); string [] pinyin = pinyinhelper. tohanyupinypolicingarray ('weight', format );
TohanyupinyinstringarrayIf the input characters are not Chinese characters and cannot be converted to pinyinReturns null..
Although pinyin4j is easy to use, it is still limited. The above code can only obtain the Pinyin of a single Chinese character, but cannot obtain the Pinyin of a word that contains multiple words. For example, "Chongqing" cannot determine whether it is "Chongqing" or "zhongqing", and pinyin4j cannot determine the pronunciation of polyphonic words through context.
Therefore, when you obtain the pronunciation of a word that contains multiple words, you can return a list. The correct pronunciation can only be selected manually.