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 ". The pinyin4j library can solve this problem well when Java obtains Chinese pinyin characters.: Http://sourceforge.net/projects/pinyin4j/ download unzip, there is a pinyin4j-2.5.0.jar, you can use this library. Conversion of Chinese characters to pinyin: String [] pinyin = PinyinHelper. toHanyuPinyinStringArray ('weight'); The above line of code refers to converting a single Chinese character to pinyin, for example, "heavy". This method returns an array of the String type: "zhong4" "chong2" weight "is a Polyphonic word. The returned array of this method contains all the phonetic alphabet of this word. The last digit in each pronunciation is the tone (the first, the second, the third, and the fourth ). The preceding method is the simplest way to obtain a single Chinese character. You can also use HanyuPinyinOutputFormat to format and return the pinyin format. [Java] 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: v represents u (nv) // WITH_U_AND_COLON: u: "represents u (n U :) // WITH_U_UNICODE: directly use u (Nu) www.2cto. comformat. setVCharType (HanyuPinyinVCharType. WITH_U_UNICODE); String [] pinyin = PinyinHelper. toHanyuPinyinStringArray ('weight', format); toHanyuPinyinStringArray returns null if the input character is not a Chinese character and cannot be converted to pinyin. 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.