In fact, the conversion of each other through key-value pairs to obtain, such as (\u81fa=\u53f0) and other composition rich correspondence. Because authors use static to save these data dictionaries, the functions of these transformations are not commonly used in applications. It is optimized by instantiating objects to be released when memory is tight.
The optimized scenario here is to have the DB data dictionary present in the Asset/py directory
public class Pinyinhelper {public Properties pinyin_table;public properties Mutil_pinyin_table;public chinesehelper Chinesehelper;public String pinyin_separator = ","; Phonetic separator public string all_unmarked_vowel = "Aeiouv";p ublic string all_marked_vowel = "Āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜ"; All tones of phonetic Alphabet public pinyinhelper (context context) {Assetmanager Assetmanager = Context.getassets (); pinyin_table = new Properties (); mutil_pinyin_table = new Properties (); Properties Chineseproperties = new properties (); try {pinyin_table.load (Assetmanager.open ("py/pinyin.db")); Mutil_pinyin_table.load (Assetmanager.open ("py/mutil_pinyin.db")) Chineseproperties.load (AssetManager.open ("py/ Chinese.db "));} catch (IOException e) {e.printstacktrace ();} Chinesehelper = new Chinesehelper (chineseproperties);} /** * converts pinyin with tonal format to a number representing phonetic tones in tone format * * @param pinyinarraystring * phonetic format pinyin * @return numbers for phonetic format Pinyin */private string[] Convertwithtonenumber (String pinyinarraystring) {string[] Pinyinarray = Pinyinarraystring.split (PINyin_separator); for (int i = pinyinarray.length-1; I >= 0; i--) {Boolean hasmarkedchar = false; String Originalpinyin = Pinyinarray[i].replaceall ("ü", "V"); Replace ü in pinyin with vfor (int j = originalpinyin.length ()-1; J >= 0; j--) {Char Originalchar = Originalpinyin.charat (j);//Search A tonal phonetic alphabet, if present, is replaced with a non-tonal English letter if (Originalchar < ' a ' | | originalchar > ' z ') {int indexinallmarked = All_marked_vowel. IndexOf (originalchar); int tonenumber = indexinallmarked% 4 + 1; Number of tones char Replacechar = All_unmarked_vowel.charat (((indexinallmarked-indexinallmarked% 4)/4);p inyinarray[i] = Origi Nalpinyin.replaceall (String.valueof (Originalchar), string.valueof (Replacechar)) + Tonenumber;hasmarkedchar = true; Break;}} if (!hasmarkedchar) {//Cannot find a tonal phonetic Alphabet description is softly, the number 5 means pinyinarray[i] = Originalpinyin + "5";}} return Pinyinarray;} /** * convert pinyin with tonal format to pinyin with no tonal format * * @param pinyinarraystring * Pinyin with tonal format * @return phonetic pinyin */public string[] Conver Twithouttone (String pinyinarraystring) {string[] Pinyinarray;for (int i = All_marked_vowel.length ()-1; I >= 0; i--) {Char Originalchar = All_marked_vowel.charat (i); Char Replac Echar = All_unmarked_vowel.charat (((i-i% 4)/4);p inyinarraystring = Pinyinarraystring.replaceall (String.valueOf ( Originalchar), string.valueof (Replacechar));} Replace ü in pinyin with vpinyinarray = Pinyinarraystring.replaceall ("ü", "V"). Split (Pinyin_separator);//The Pinyin may be duplicated after the tone is removed. Do go to redo set<string> pinyinset = new linkedhashset<string> (); for (String Pinyin:pinyinarray) {Pinyinset.add ( pinyin);} Return Pinyinset.toarray (New String[pinyinset.size ())); /** * The phonetic pinyin format to the corresponding format pinyin * * @param pinyinstring * phonetic Pinyin * @param pinyinformat * Pinyin format: With_tone_nu mber--numbers represent tones, without_tone--without tones, * with_tone_mark--with tones * @return format converted Pinyin */private string[] Formatpinyin (strin G pinyinstring, Pinyinformat Pinyinformat) {if (Pinyinformat = = Pinyinformat.with_tone_mark) {return pinyinString.split (Pinyin_separator);} else if (Pinyinformat = = Pinyinformat.witH_tone_number) {return convertwithtonenumber (pinyinstring);} else if (Pinyinformat = = Pinyinformat.without_tone) { Return Convertwithouttone (pinyinstring);} return null;} /** * Convert a single Chinese character to the phonetic alphabet of the corresponding format * * @param c * need to be converted to pinyin kanji * @param pinyinformat * Pinyin format: with_tone_number--digit representation Tones, without_tone--without tones, * with_tone_mark--with tones * @return Chinese pinyin */public string[] Converttopinyinarray (char C, Pin Yinformat pinyinformat) {String Pinyin = Pinyin_table.getproperty (string.valueof (c)); if (pinyin! = null) && (! Pinyin.equals ("null"))) {return formatpinyin (pinyin, Pinyinformat);} return null;} /** * Convert a single Chinese character to pinyin in tonal format * * @param c * need to be converted to pinyin kanji * @return string pinyin */public string[] Converttopinyinarray (char c) {return Converttopinyinarray (c, Pinyinformat.with_tone_mark);} /** * Convert the string to the phonetic alphabet in the appropriate format * * @param str * required to convert the string * @param separator * Pinyin separator * @param pinyinformat * Pinyin format: with_tone_number--number for tone, without_tone--without tone, * with_tone_mark--Phonetic * @return string Pinyin */public string converttopinyinstring (String str, string separator, Pinyinformat pinyinfor MAT) {str = Chinesehelper.converttosimplifiedchinese (str); StringBuilder sb = new StringBuilder (); for (int i = 0, Len = str.length (), i < Len; i++) {char c = str.charat (i); if (Ch Inesehelper.ischinese (c) | | c = = ' 0 ')//determine if the character is Chinese or 0{//polyphone recognition processing Boolean isfoundflag = False;int RightMove = 3;//The current Chinese character in turn with the following 3, 2, 1 Chinese characters, determine if there is a polyphone word for (int rightindex = (i + rightMove) < Len? (i + rightMove): (len-1); Rightindex > i; rightindex--) {String Cizu = str.substring (i, Rightindex + 1); if (Mutil_pinyin_table.containskey (Cizu)) {string[] Pinyinarray = Formatpinyin (Mutil_pinyin_table.getproperty (Cizu), Pinyinformat); for (int j = 0, L = pinyinarray.length; J & Lt L J + +) {sb.append (pinyinarray[j]), if (J < l-1) {sb.append (separator);}} i = Rightindex;isfoundflag = True;break;}} if (!isfoundflag) {string[] Pinyinarray = Converttopinyinarray (Str.charat (i), pinyinformat);Pinyinarray! = null) {sb.append (pinyinarray[0]);} else {sb.append (Str.charat (i));}} if (I < len-1) {sb.append (separator);}} else {sb.append (c); if ((i + 1) < Len && Chinesehelper.ischinese (Str.charat (i + 1)) {sb.append (separator);}}} return sb.tostring ();} /** * Convert a string to phonetic pinyin * * @param str * required to convert the string * @param separator * Pinyin separator * @return converted phonetic Pinyin */pu Blic string converttopinyinstring (String str, string separator) {return converttopinyinstring (str, separator, Pinyinformat.with_tone_mark);} /** * Determine if a Chinese character is polyphone * * @param c * Kanji * @return The result is that the kanji returns true, otherwise the return is False */public Boolean hasmultipinyin (char C {string[] Pinyinarray = Converttopinyinarray (c); if (Pinyinarray! = null && pinyinarray.length > 1) {return TR UE;} return false;} /** * Gets the string corresponding to the first letter of the phonetic alphabet * * @param str * required to convert the string * @return corresponding to the first letter of the phonetic */public string getshortpinyin (String str) {S Tring separator = "#"; Use # as the phonetic separator StringBuilder sb = new StringBuilder (); char[] ChArarray = new Char[str.length ()];for (int i = 0, Len = str.length (), i < Len; i++) {char c = str.charat (i);//First judge whether it is a Chinese character or 0, if not, return the character directly to the IF (! Chinesehelper.ischinese (c) && c! = ' 0 ') {chararray[i] = C;} else {int j = i + 1;sb.append (c);//Search continuous Chinese character string while (J < Len && (Chinesehelper.ischinese (Str.charat (j)) | | Str.charat (j) = = ' 0 ')) {Sb.append (Str.charat (j)); J + +;} String Hanzipinyin = converttopinyinstring (sb.tostring (), separator, pinyinformat.without_tone); string[] Pinyinarray = hanzipinyin.split (separator); for (String String:pinyinarray) {Chararray[i] = String.charat (0); I ++;} I--;sb.delete (0, sb.tostring (). Length ()); Sb.trimtosize ();}} Return string.valueof (Chararray);}}
public class Chinesehelper {private Properties Chinese_table;public Chinesehelper (Properties properties) {Chinese_ TABLE = properties;} /** * Convert a single Traditional Chinese characters to simplified characters * * @param c * Characters that need to be converted * @return converted simplified characters */public Char Converttosimplifiedchinese (char c) {if (Istraditionalchinese (c)) {return Chinese_table.getproperty (string.valueof (c)). CharAt (0);} return c;} /** * Convert individual simplified characters to traditional characters * * @param c * Simplified characters required for conversion * @return converted Font */public char Converttotraditionalchinese (char c) {String Hanzi = string.valueof (c); if (Chinese_table.containsvalue (Hanzi)) {iterator<entry<object, Object> > ITR = Chinese_table.entryset (). iterator (); while (Itr.hasnext ()) {entry<object, object> e = Itr.next (); if ( E.getvalue (). ToString (). Equals (Hanzi)) {return E.getkey (). ToString (). charAt (0);}} return c;} /** * Convert traditional characters to simplified characters * * @param str * The traditional characters that need to be converted * @return converted simplified */public string Converttosimplifiedchinese (string STR) {StringBuilder SB = new StringBuilder (); for (int i = 0, len = str.Length (); i < Len; i++) {char c = str.charat (i); Sb.append (Converttosimplifiedchinese (c));} return sb.tostring ();} /** * Convert simplified characters to traditional characters * * @param str * Simplified characters required for conversion * @return converted font */public string Converttotraditionalchinese (string STR) {StringBuilder SB = new StringBuilder (); for (int i = 0, Len = str.length (), i < Len; i++) {char c = str.charat (i) ; Sb.append (Converttotraditionalchinese (c));} return sb.tostring ();} /** * Determines if a character is a traditional Chinese character * * @param c * The characters to be judged * @return are traditional characters return true, otherwise return False */public Boolean Istraditionalchinese ( Char c) {return Chinese_table.containskey (string.valueof (c));} /** * Determine if a character is a Chinese character * * @param c * The character that needs to be judged * @return is the Chinese character returns true, otherwise it returns false */public static Boolean Ischinese (char C {String regex = "[\\u4e00-\\u9fa5]"; return string.valueof (c). Matches (regex);}}
public class Pinyinformat {private String name;public static final Pinyinformat With_tone_mark = new Pinyinformat ("with_to Ne_mark ");p ublic static final Pinyinformat Without_tone = new Pinyinformat (" Without_tone ");p ublic static final Pinyinformat with_tone_number = new Pinyinformat ("With_tone_number");p rotected Pinyinformat (String name) {this.name = Name;} Protected String GetName () {return this.name;}}