Detailed description of the source code under the Pinyin4.jar package

Source: Internet
Author: User

In one sentence, java, as an open-source thing, can solve the problem as long as the corresponding jar toolkit is to implement a certain function. Since a company in Shenzhen used a toolkit pinyin. jar package, today I am writing the source code about the package. So much nonsense. Let's talk about it now !!!!!!! The ing relationship between unicode and Chinese Pinyin is as follows: 3007 (ling2) 4E00 (yi1) 4E01 (ding1, zheng1) 4E02 (kao3) 4E03 (qi1) 4E04 (shang4, shang3) 4E05 (xia4) 4E06 (none0) 4E07 (wan4, mo4) 4E08 (zhang4) 4E09 (san1) 4E0A (shang4, shang3) 4E0B (xia4) 4E0C (ji1) values (bu4, bu2, fou3) values (yu3, yu4, yu2) 4E0F (mian3) 4E10 (gai4) 4E11 (chou3) 4E12 (chou3) 4E13 (zhuan1) 4E14 (qie3, ju1) 4E15 (pi1) 4E16 (Shi4) 4E17 (shi4) 4E18 (qiu1) 4E19 (bing3) 4E1A (ye4) 4E1B (cong2) 4E1C (dong1) 4E1D (si1) 4E1E (cheng2) 1_( diu1) random (random) random (liang3) 4E22 (diu1) 4E23 (you3) 4E24 (liang3) 4E25 (yan2) random (bing4) 4E27 (sang1, sang4, sang5) 4E28 (shu4) 4E29 (jiu1) 4E2A (ge4, ge3) Next we will talk about the four classes in the format package ######## 1HanyuPinyinCaseType Chinese pinyin case-sensitive class, it contains two static subnode objects. When called, it can only be called, for example, HanyuPinyinCaseType. UPPERCASE ############## public class HanyuPinyi NCaseType {public static final HanyuPinyinCaseType UPPERCASE = new HanyuPinyinCaseType (); public static final HanyuPinyinCaseType LOWERCASE = new HanyuPinyinCaseType ();} ######## 2HanyuPinyinToneType a harmonic class used to determine whether to filter out the numbers in Chinese pinyin [1-5 ########### # public class HanyuPinyinToneType {public static final HanyuPinyinToneType WITH_TONE_NUMBER = new HanyuPinyinToneType (); public static final HanyuPinyinToneType WITHOUT_TONE = new HanyuPinyinToneType () ;######### 3HanyuPinyinVCharType Chinese pinyin class, used to determine whether the output pinyin contains special character encoding characters ############## public class HanyuPinyinVCharType {public static final HanyuPinyinVCharType WITH_U_AND_COLON = new character (); // The default public static final HanyuPinyinVCharType WITH_V = new HanyuPinyinVCharType (); // Replace "u:" with "v" public static final HanyuPinyinVCharType WITH_U_UNICODE = ne W HanyuPinyinVCharType (); // set "u: "Replace with" U "}######## 4HanyuPinyinOutputFormat class ############# public final class hanyupinyininoutputformat {private hanyupinyininvchartype vCharType; private HanyuPinyinCaseType caseType; private HanyuPinyinToneType toneType; public HanyuPinyinOutputFormat () {restoreDefault ();} public void restoreDefault () {this. vCharType = HanyuPinyinVCharType. WITH_U_AND_COLON; this. CaseType = HanyuPinyinCaseType. LOWERCASE; this. toneType = HanyuPinyinToneType. WITH_TONE_NUMBER ;}... The get/set method is omitted.} the principles of the above four classes are the same, but the fourth class encapsulates the first three classes. When calling a class, the memory automatically creates static objects. For example, when the primary class is called, the following two static shared classes (UPPERCASE, LOWERCASE) public static final classes UPPERCASE = new HanyuPinyinCaseType (); public static final HanyuPinyinCaseType LOWERCASE = new primary () are created (); ################# core class PinyinHelper ##################### ################ PinyinHelper class, there is only one field unicodeToHanyuPinyinTable and one static block, as follows: private static Properties unicodeToHanyuPinyinTable = Null; static {if (unicodeToHanyuPinyinTable = null) initializeTable ();} the code is easy to understand. When the class used to save the ing table is null, perform the following initialization process: private static void initializeTable () {try {String resourceName = "/pinyindb/unicode_to_hanyu_pinyin.txt"; unicodeToHanyuPinyinTable = new Properties (); unicodeToHanyuPinyinTable. load (new BufferedInputStream (PinyinHelper. class. getResourceAsStream ("/pinyindb/unicode_to_hanyu_pinyin.txt ")));} Catch (FileNotFoundException ex) {ex. printStackTrace ();} catch (IOException ex) {ex. printStackTrace () ;}} read the files in the root class path, so that the unicodeToHanyuPinyinTable table is not empty. The core method of the PinyinHelper class has only one method: toHanyuPinyinString. Other methods are auxiliary methods, for the principle, see the following public static String toHanyuPinyinString (String str, HanyuPinyinOutputFormat outputFormat, String seperater) // input a Character Sequence containing Chinese characters, a format class, And a grid character (custom, arbitrary write) {StringBuffer resultpinypolicbuf = New StringBuffer (); // Save the cache of the sequence for (int I = 0; I <str. length (); I ++) {// traverses the Character Sequence int codepointOfChar = str. codePointAt (I); // obtain the corresponding unicode encoded integer String mainPinyinStrOfChar = getFirstHanyuPinyinString (codepointOfChar, outputFormat); // according to the format of the three attribute classes, and the corresponding unicode encoding. In the ing file, the first pinyin string if (mainPinyinStrOfChar! = Null) {resultpinydomainbuf. append (mainPinyinStrOfChar); // Add it to the cache if (I! = Str. length ()-1) resultpiny?buf. append (seperater); // Add separator} else {resultpiny?buf. append (str. charAt (I);} return resultpiny?buf. toString (); // return} The above call the callback method private static String encode (int codepoint, HanyuPinyinOutputFormat outputFormat) {String [] pinyinStrArray = getHanyuPinyinStringArray (codepoint, outputFormat ); if (pinyinStr Array! = Null) & (pinydomainarray. length> 0) {// query the ing table and return the first or null return pinyinStrArray [0];} return null;} Where the getHanyuPinyinStringArray method, obtain the list array corresponding to the ing table. Source Code: private static String [] getHanyuPinyinStringArray (int codepoint, HanyuPinyinOutputFormat outputFormat) {String pinyinRecord = getHanyuPinyinRecord (codepoint ); // obtain the corresponding pinyin string based on the specific unicode and remove the brackets, for example, 4E36 (zhu3, dian3). Then, get "(zhu3, dian3) "the word" if (piny InRecord! = Null) {int indexOfLeftBracket = pinyinRecord. indexOf ("("); int indexOfRightBracket = pinyinRecord. lastIndexOf (")"); String stripedString = pinyinRecord. substring (indexOfLeftBracket + 1, indexOfRightBracket); // remove the Left and Right brackets. In the preceding example, obtain "zhu3, dian3"; Define if (HanyuPinyinVCharType. WITH_V = outputFormat. getVCharType () stripedString = stripedString. replaceAll ("u :", "V"); else if (HanyuPinyinVCharType. WITH_U_UNICODE = outputFormat. getVCharType () {stripedString = stripedString. replaceAll ("u:", "u");} if (HanyuPinyinToneType. WITHOUT_TONE = outputFormat. getToneType () {stripedString = stripedString. replaceAll ("[1-5]", "");} if (HanyuPinyinCaseType. UPPERCASE = outputFormat. getCaseType () {stripedString = stripedString. toUpperCase ();} return stripedStrin G. split (","); // is separated into a string array and return} return null;} How to call it? Code: PinyinHelper. toHanyuPinyinString ("People's Republic of China", new HanyuPinyinOutputFormat (), "#"); // use the default format class new hanyupinyininoutputformat ()

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.