Usage of converting Chinese characters from Java to PinYin pinyin4j

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.