I. Description of the problem
Convert Chinese characters into corresponding pinyin or get the first letter of Chinese pinyin, these are often encountered in the development of the problem, in the acquisition of Chinese pinyin or pinyin of the first letter, we in the referral or search department can greatly improve the user experience, such as the user input "NH", we can associate "Hello", "you will "," Years later "," connotation "and other words. In Java, Pinyin4j.jar is a great tool for translating Chinese characters into corresponding pinyin, and here's how to use this jar package.
Second, the resources to download
after the download decompression, directly using the file in the Pinyin4j-2.5.0.jar can be.
III. Provision of methodologies
we can use the Hanyupinyinoutputformat class to set the return method of pinyin, such as setting the capitalization of phonetic Alphabet, phonetic symbols, and the display form of phonetic U, as follows:
Directly using the Pinyinhelper method to do the corresponding transformation of Chinese characters, there are the following three kinds of, three effects how to do their own test can be:
Four, write code
For the features that we might normally use, I did the following encapsulation, the functionality provided, and the specific implementation steps in reference to the comments in the code:
Package com.lulei.util;
Import java.util.ArrayList;
Import java.util.List;
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;
public class Pinyinutil {private static Hanyupinyinoutputformat format = NULL;
static {format = new Hanyupinyinoutputformat ();
Pinyin lowercase format.setcasetype (hanyupinyincasetype.lowercase); No phonetic notation; with_tone_number:1-4 number denotes an English standard; With_tone_mark: direct use of the phonetic symbols (must be with_u_unicode otherwise abnormal format.settonetype (
Hanyupinyintonetype.without_tone);
V is used to denote üformat.setvchartype (HANYUPINYINVCHARTYPE.WITH_V); /** * @param str * @return * @Description: Returns the phonetic/public static string[of the string getcharpinyinstr ing (String str) {if (str = NULL | | str.length () < 1) {Return null;
} list<string> result = new arraylist<string> (); Analyze the records in the string one by one for (int i = 0; i < str.length (); i++) {results = getcharpinyinstring (Str.charat (i), result)
;
Return Result.toarray (New String[result.size ()); /** * @param c * @param list * @return * @Description: the pinyin of the character C is spliced into the record in the list/private stat
IC list<string> getcharpinyinstring (char C, list<string> List) {string[] STRs = getcharpinyinstring (c);
list<string> result = new arraylist<string> (); If the parsed pinyin is empty, judge whether the character C is an English letter, and if it is an English letter, add the value of the phonetic result if (STRs = null) {if (C >= ' a ' && c <= ' z ') | | (c >= ' A ' && C <= ' Z ')) {c = C <= 91? (char)
(C +): C;
if (list = = NULL | | list.size () = = 0) {Result.add (c + "");
else {for (String s:list) {Result.add (S + c); } return ResULt
} return list; ///Combine the phonetic head of character C with the first group of existing pinyin for a new record for (String str:strs) {if (list = = NULL | | list.size () = 0) {RE
Sult.add (str);
else {for (String s:list) {result.add (S + str);
}} return result; /** * @param c * @return * @Description: return Chinese pinyin/public static string[] Getcharpinyinstring (char c)
{try {//returns the phonetic return Pinyinhelper.tohanyupinyinstringarray (c, format) of the character C;
catch (Exception e) {e.printstacktrace ();
return null; /** * @param str * @return * @Description: Returns the phonetic initials of the string/public static string[] Getcharpinyi
NChar (String str) {if (str = NULL | | str.length () < 1) {return null;
} list<string> result = new arraylist<string> (); Analyze the records in the string one by one for (int i = 0; i < str.length (); i++) {results = Getcharpinyinchar (Str.charat (i), result);
Return Result.toarray (New String[result.size ()); /** * @param c * @param list * @return * @Description: Stitching the phonetic initials of the character C into the records in the list/private s
Tatic list<string> Getcharpinyinchar (char C, list<string> List) {char[] chars = Getcharpinyinchar (c);
list<string> result = new arraylist<string> (); If the parsed pinyin is empty, judge whether the character C is an English letter, and if it is an English letter, add the value of the phonetic result if (chars = null) {if (C >= ' a ' && c <= ' z ') | | (c >= ' A ' && C <= ' Z ')) {c = C < 91? (char)
(C +): C;
if (list = = NULL | | list.size () = = 0) {Result.add (c + "");
else {for (String s:list) {Result.add (S + c);
} return result;
} return list;
A new record for (char ch:chars) {if (list = = NULL | | list.size () = 0) is synthesized with the phonetic initials of the character C and the existing phonetic first letter group {
Result.add (ch + ""); else {for (String s:list) {result.add (s + ch);
}} return result; /** * @param c * @return * @Description: Returns the first letter of Chinese pinyin/public static char[] Getcharpinyinchar (CH
Ar c) {//character C Pinyin string[] STRs = getcharpinyinstring (c);
if (STRs!= null) {//intercept the first letter of Pinyin char[] chars = new Char[strs.length];
for (int i = 0; I <chars.length; i++) {Chars[i] = Strs[i].charat (0);
return chars;
return null;
public static void Main (string[] args) {//TODO auto-generated method stub char c = "Chongqing". CharAt (0);
string[] str = pinyinutil.getcharpinyinstring (c);
for (String s:str) {System.out.println (s);
} char[] chars = Pinyinutil.getcharpinyinchar (c);
for (char c1:chars) {System.out.println (C1);
str = pinyinutil.getcharpinyinstring ("Chongqing C");
for (String s:str) { System.out.println (s);
str = Pinyinutil.getcharpinyinchar ("Chongqing a");
for (String s:str) {System.out.println (s); }
}
}
V. OUTPUT results
The above is the Java implementation of Chinese pinyin all the content, I hope to help you learn.