This article describes the Java implementation of the transformation of Chinese characters into Hanyu Pinyin method. Share to everyone for your reference, specific as follows:
On the internet, accidentally saw a very interesting gadget, named Pinyin4j, can convert Chinese characters to Hanyu Pinyin, use his words and then with the Lucene, Chinese participle can make similar to google that input Hanyu Pinyin for full-text search function. The implementation code is as follows
Package pinyin4j;
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; public class Pinyin4jtest {public static void main (string argsp[]) {try {string output = Pinyin4jtest.cntop
Inyin ("You and Hello", null);
SYSTEM.OUT.PRINTLN (output);
catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch block E.printstacktrace (); }/** * @parm INPUTCN input Chinese String * @parm seg Output Pinyin Separator * * Hanyupinyinoutputformat provides several output modes * Han Yupinyincasetype: Set the result of the input is uppercase or lowercase English lowercase: lowercase uppercase: Uppercase * Hanyupinyintonetype: output indicates pitch and accent With_tone_number: Mark tones like YE1 1-4 for 1-4 * without_tone: no diacritics hanyupinyinvchArtype: What phonetic code to use for output/public static string Cntopinyin (string inputcn, String seg) throws Badhanyupinyinoutputf
ormatcombination {char[] Inputarray = Inputcn.tochararray ();
if (seg = = null) SEG = "";
Hanyupinyinoutputformat format = new Hanyupinyinoutputformat ();
Format.setcasetype (hanyupinyincasetype.lowercase);
Format.settonetype (Hanyupinyintonetype.without_tone);
Format.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);
String output = "";
string[] temp = new STRING[10]; for (int i = 0; i < inputarray.length i++) {temp = Pinyinhelper.tohanyupinyinstringarray (inputarray[i], format)
; If the input of Chinese characters is pronunciation will be the different pronunciations in temp[], if not pronunciation only temp[0] in the value for (int j = 0; J < Temp.length; j +) {output + = tem
P[J] + seg;
} return output;
}
}
I hope this article will help you with your Java programming.