[pinyin4j] Java edition Kanji conversion Pinyin (case)

Source: Internet
Author: User

Pinyin4j is a lib that converts Chinese characters into Pinyin, which is very practical, and its MAVEN address is: http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0


PINYIN4J provides pinyinhelper this static class to provide pinyin conversion services, the Main method:

static public string[] Tohanyupinyinstringarray (char ch)

Converting char (which must be a Chinese character word) to Pinyin is useful in general format and returns NULL if CH is non-kanji.

Input: Re -output: [Zhong4, Chong2 ] Description of the word two pronunciations, pinyin after the 1,2,3,4 is the pronunciation


static public string[] Tohanyupinyinstringarray (char ch,hanyupinyinoutputformat OutputFormat)

Ditto, but this method can set the format of the output. Hanyupinyinoutputformat can set the phonetic case, whether to add a number after the pronunciation, special pronunciation of the display, defined as follows:

    /**     * The option indicates that the output of ' u ' is ' you: '     */public    static final Hanyupinyinvchartype with_u_a Nd_colon = new Hanyupinyinvchartype ("With_u_and_colon");    /**     * The option indicates that the output of ' u ' is "V"     */public    static final Hanyupinyinvchartype With_v = New Hanyupinyinvchartype ("With_v")    /**     * The option indicates that the output of ' U ' is "U" in Unicode form     * /public    static final Hanyupinyinvchartype With_u_unicode = new Hanyupinyinvchartype ("With_u_unicode");


static public string[] Totongyongpinyinstringarray (char ch)

Convert to Tongyong 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 Weimar 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 gross 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)

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


For the "heavy" pinyin conversion, the results obtained from the above methods are as follows:

Hanyu Pinyin: [Zhong4, chong2] tongyong pinyin: [Jhong4, chong2] gamma pinyin: [Chung4, ch ' ung2] phonetic symbols pinyin: [Jung4, Chung2]: [Jung4, Chung2] The Mandarin Roman Word: [ Jonq, Chorng]

Well, with the basics above, we can encapsulate a tool class that converts Chinese characters to pinyin, which only uses Chinese pinyin.

The first thing to do is to add pinyin4j to the project, and if it is a MAVEN project, you can include the reference:

<span style= "White-space:pre" ></span><!--add pinyin4j--        <dependency>            <groupid >com.belerweb</groupId>            <artifactId>pinyin4j</artifactId>            <version>2.5.0 </version>        </dependency>
Non-Maven can directly put the downloaded jar package into classpath.

Then write the tool class Pinyintool.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 Zengxm on 2014/12/4.    */public class Pinyintool {hanyupinyinoutputformat format = null;              public static enum Type {uppercase,//all uppercase lowercase,//all lowercase firstupper        First Letter Capital} public Pinyintool () {format = new Hanyupinyinoutputformat ();        Format.setcasetype (hanyupinyincasetype.uppercase);    Format.settonetype (Hanyupinyintonetype.without_tone); } public string Topinyin (String str) throws badhanyupinyinoutputformatcombination{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 kanji or no corresponding pinyin, do not convert * such as: tomorrow converted to Mingtian * @param str * @param spera * @return * @throws badhanyupinyinoutputformatcombination */Public String Topinyin (String str, String Spera, type 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 <=) py + = C;                else{t = Pinyinhelper.tohanyupinyinstringarray (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 results:




[pinyin4j] Java edition Kanji conversion Pinyin (case)

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.