[Pinyin4j] converting Chinese characters into pinyin (case sensitive) in java, and pinyin4j in case sensitive

Source: Internet
Author: User

[Pinyin4j] converting Chinese characters into pinyin (case sensitive) in java, and pinyin4j in case sensitive

Pinyin4J is a lib that can convert Chinese characters into pinyin, which is very practical. Its maven address is: http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0.


Pinyin4J provides the static class PinyinHelper to provide the PinYin conversion service. The main methods are as follows:

Static public String [] toHanyuPinyinStringArray (char ch)

Convert char (which must be a single Chinese character) to pinyin. The common format is used. If ch is not a Chinese character, null is returned.

Input: duplicate output: [zhong4, chong2] indicates that duplicate words have two pronunciations. The values 1, 2, and 4 after pinyin indicate the pronunciations.


Static public String [] toHanyuPinyinStringArray (char ch, HanyuPinyinOutputFormat outputFormat)

Same as above, but this method can set the output format.HanyuPinyinOutputFormatYou can set the upper and lower case letters, whether to display the pronunciation numbers, and special pronunciations. The definition is as follows:

    /**     * The option indicates that the output of 'ü' is "u:"     */    public static final HanyuPinyinVCharType WITH_U_AND_COLON = new HanyuPinyinVCharType("WITH_U_AND_COLON");    /**     * The option indicates that the output of 'ü' is "v"     */    public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType("WITH_V")    /**     * The option indicates that the output of 'ü' is "ü" in Unicode form     */    public static final HanyuPinyinVCharType WITH_U_UNICODE = new HanyuPinyinVCharType("WITH_U_UNICODE");


Static public String [] toTongyongPinyinStringArray (char ch)

Convert to General pinyin. General pinyin introduction see: http://zh.wikipedia.org/zh-cn/%E9%80%9A%E7%94%A8%E6%8B%BC%E9%9F%B3


Static public String [] toWadeGilesPinyinStringArray (char ch)

Convert to rituma pinyin: http://zh.wikipedia.org/wiki/%E5%A8%81%E5%A6%A5%E7%91%AA%E6%8B%BC%E9%9F%B3


Static public String [] toMPS2PinyinStringArray (char ch)

Convert to Phonetic Notation pinyin: http://zh.wikipedia.org/zh-cn/%E6%B3%A8%E9%9F%B3%E7%AC%A6%E8%99%9F


Static public String [] toYalePinyinStringArray (char ch)

Convert to yeyun pinyin: http://zh.wikipedia.org/zh-cn/%E8%80%B6%E9%AD%AF%E6%8B%BC%E9%9F%B3


Static public String [] toGwoyeuRomatzyhStringArray (char ch)

Conversion to Mandarin Rome: http://zh.wikipedia.org/wiki/%E5%9C%8B%E8%AA%9E%E7%BE%85%E9%A6%AC%E5%AD%97


For the PinYin conversion of "weight", the results obtained by the above methods are:

Chinese pinyin: [zhong4, chong2] General pinyin: [jhong4, chong2] rittal pinyin: [chung4, ch'ung2] phonetic alphabet: [jung4, chung2] Yeo pinyin: [jung4, chung2] Mandarin Roman character: [jonq, chorng]

Well, with the above foundation, we can encapsulate a tool class to convert Chinese characters into pinyin. Here we only use Chinese Characters in pinyin.

First, add pinyin4j to the project. For a maven project, you can add references:

<Span style = "white-space: pre"> </span> <! -- Added pinyin4j --> <dependency> <groupId> com. belerweb </groupId> <artifactId> pinyin4j </artifactId> <version> 2.5.0 </version> </dependency>
For non-maven users, you can directly put the downloaded jar package into classpath.

Then write the tool class pinyuncol. java:

Package org. nerve. d3lesson. common. tools; import net. sourceforge. pinyin4j. pinyinHelper; import net. sourceforge. pinyin4j. format. hanyuPinyinCaseType; import net. sourceforge. pinyin4j. format. hanyuPinyinOutputFormat; import net. sourceforge. pinyin4j. format. hanyuPinyinToneType; import net. sourceforge. pinyin4j. format. exception. badHanyuPinyinOutputFormatCombination; import java. util. arrays;/***** Created by zengx M on 2014/12/4. */public class pinypolicol {HanyuPinyinOutputFormat format = null; public static enum Type {UPPERCASE, // all upper case LOWERCASE, // all lower case FIRSTUPPER // upper case} public pinypolicol () {format = new HanyuPinyinOutputFormat (); format. setCaseType (HanyuPinyinCaseType. UPPERCASE); format. setToneType (HanyuPinyinToneType. WITHOUT_TONE);} public String toPinYin (String str) throws BadHanyuPinyinOutputForm AtCombination {return toPinYin (str, "", Type. UPPERCASE);} public String toPinYin (String str, String spera) throws BadHanyuPinyinOutputFormatCombination {return toPinYin (str, spera, Type. UPPERCASE);}/*** converts str to pinyin. If it is not a Chinese character or there is no corresponding pinyin, It is not converted * For example: convert it to MINGTIAN * @ param str * @ param spera * @ return * @ throws BadHanyuPinyinOutputFormatCombination */public String toPinYin (String str, String spera, Typ E type) throws BadHanyuPinyinOutputFormatCombination {if (str = null | str. trim (). length () = 0) return ""; if (type = Type. UPPERCASE) format. setCaseType (HanyuPinyinCaseType. UPPERCASE); else format. setCaseType (HanyuPinyinCaseType. LOWERCASE); String py = ""; String temp = ""; String [] t; for (int I = 0; I <str. length (); I ++) {char c = str. charAt (I); if (int) c <= 128) py + = c; else {t = PinyinHelper. toHan YuPinyinStringArray (c, format); if (t = null) py + = c; else {temp = t [0]; if (type = Type. FIRSTUPPER) temp = t [0]. toUpperCase (). charAt (0) + temp. substring (1); py + = temp + (I = str. length ()-1? "": Spera) ;}}return py. trim ();}}

Write a test case to see the result:




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.