A case study of Chinese pinyin pinyin4j usage in Java _java

Source: Internet
Author: User

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.

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.