Java realizes Chinese pinyin __java

Source: Internet
Author: User

Recently encountered in the project of Chinese Pinyin (with DataList implementation input Chinese, to the database matching the corresponding records, here no longer repeat), so found on the Internet pinyin4j This toolkit, very useful, others do not say, directly on the code:

Import Java.util.HashSet;  
  
Import Java.util.Set;  
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.HanyuPinyinVCharType;  
  
Import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; /** * Description: * processing Chinese characters and corresponding Pinyin conversion tool class * * public class Pinyinutil {/** * descrip   
    tion: * Incoming character string, stitching into the corresponding pinyin, return the collection of Pinyin * Note: If it is two Chinese characters within the whole spell, if more than two Chinese characters are the first Chinese characters full spell + back characters of the initial letter * * public static set<string> Getpinyinset (String src) {set<string> lstresult = new Hashset<st  
        Ring> ();  char[] T1 = null;  
          
        The string is converted to a char array t1 = Src.tochararray (); if (t1.length<=2) {//① iterative kanji for (char ch:t1){String s[] = getpinyin (CH);  
                set<string> lstnew = new hashset<string> ();  
                        ② iterate the phonetic array for each Chinese character for (String str:s) {if (Lstresult.size () ==0) {  
                    Lstnew.add (str);  
                            }else{for (String ss:lstresult) {ss = str;  
                        Lstnew.add (ss);  
                }} lstresult.clear ();  
            Lstresult = lstnew;  
            }}else{int index = 0;  
                ① Iteration Chinese for (char ch:t1) {index = index + 1;  
                String s[] = getpinyin (CH); if (index>1) {for (int i = 0; i < s.length i++) {S[i] = S[i].substr  
                    ING (0, 1); }  
                } 
                set<string> lstnew = new hashset<string> ();  
                        ② iterate the phonetic array for each Chinese character for (String str:s) {if (Lstresult.size () ==0) {  
                    Lstnew.add (str);  
                            }else{for (String ss:lstresult) {ss = str;  
                        Lstnew.add (ss);  
                }} lstresult.clear ();  
            Lstresult = lstnew;  
    } return Lstresult; /** * Description: * Incoming character string, stitching into the corresponding pinyin, return the set of Pinyin/public static  
        set<string> getpinyins (String src) {set<string> lstresult = new hashset<string> ();  char[] T1 = null;  
          
        The string is converted to a char array t1 = Src.tochararray ();    
            ① Iteration Kanji for (char ch:t1) {String s[] = getpinyin (CH);  
            set<string> lstnew = new hashset<string> (); ② iterates the phonetic array for each Chinese character for (String str:s) {if (Lstresult.size () ==0) {lstnew.  
                Add (str);  
                        }else{for (String ss:lstresult) {ss = str;  
                    Lstnew.add (ss);  
            }} lstresult.clear ();  
        Lstresult = lstnew;  
    return lstresult; /** * Description: * Incoming Chinese characters, convert the corresponding pinyin * Note: The emergence of a homonym, the default choice of Chinese characters in the first reading of the whole spell  
        Tone/public static string Getpinyin (String src) {char[] T1 = null;  
        T1 = Src.tochararray ();  
  
        string[] t2 = new String[t1.length];  
        Set the format of Chinese pinyin output hanyupinyinoutputformat t3 = new Hanyupinyinoutputformat (); T3.setcasetype (HanyupinYincasetype.lowercase);  
        T3.settonetype (Hanyupinyintonetype.without_tone);  
        T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);  
        String t4 = "";  
        int t0 = t1.length; try {for (int i = 0; i < t0 i++) {//judge whether it can be Kanji characters//System.out.print  
                ln (t1[i]); if (character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {t2 = pinyinhelper.tohanyupinyinstring  Array (t1[i], T3);//To save several Chinese characters to the t2 array t4 + = t2[0];//Remove the first pronunciation of the Chinese spelling and connect to the string T4 after} else  
                {//If it is not a kanji character, indirectly remove the character and connect to the string t4 after T4 + = character.tostring (T1[i]);  
        A catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace ());  
    return t4; /** * Description: * Converts a single Chinese character into Hanyu Pinyin, taking into account the homonym problem, returning the form of a string array/public StAtic string[] Getpinyin (char src) {char[] T1 = {src};  
          
        string[] t2 = new String[t1.length];  
        Set the format of Chinese pinyin output hanyupinyinoutputformat t3 = new Hanyupinyinoutputformat ();  
        T3.setcasetype (hanyupinyincasetype.lowercase);  
        T3.settonetype (Hanyupinyintonetype.without_tone);  
          
        T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);  
                Determine whether the Kanji character if (character.tostring (t1[0)). Matches ("[\\u4e00-\\u9fa5]+")) {try {  
            Several kinds of Chinese characters ' spelling are stored in t2 array t2 = Pinyinhelper.tohanyupinyinstringarray (t1[0), T3);  
            catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace ();  
        } else {//If it is not a Chinese character, put the character directly into the t2 array t2[0] = string.valueof (SRC);  
    return T2; /** * Description: * Incoming Chinese characters without multi-tone words, converting corresponding Pinyin * Note: if the incomingAny of the words in the text will return string information: false/public static string Getnopolyphone (String src) {char[] T1 = null;  
        T1 = Src.tochararray ();  
  
        string[] t2 = new String[t1.length];  
        Set the format of Chinese pinyin output hanyupinyinoutputformat t3 = new Hanyupinyinoutputformat ();  
        T3.setcasetype (hanyupinyincasetype.lowercase);  
        T3.settonetype (Hanyupinyintonetype.without_tone);  
        T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);  
        String t4 = "";  
        int t0 = t1.length; try {for (int i = 0; i < t0 i++) {//judge whether it can be Kanji characters//System.out.print  
                ln (t1[i]); if (character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {t2 = pinyinhelper.tohanyupinyinstring  
                    Array (t1[i], T3);//To save a few of the Chinese characters to the T2 array if (t2.length>1) {return "false"; }else{T4 + = t2[0];//outThe first pronunciation of the Chinese character is fully spelled and connected to the string T4 after}} else {//If it is not a kanji character, indirectly remove the character and connect to the string T4  
                after T4 + + character.tostring (T1[i]);  
        A catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace ());  
    return t4;   }  
      
      
}


Available Pinyin4j-2.5.0.jar Downloads: Http://pan.baidu.com/s/1kUAn3EZ


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.