تشترك الأمثلة الواردة في هذه المقالة في الكود المحدد لأحرف Java الصينية إلى أداة Pinyin للرجوع إليها. المحتوى المحدد كما يلي
استيراد com.google.common.base.strings ؛ استيراد net.sourceforge.pinyin4j.pinyinhelper ؛ استيراد net.sourceforge.pinyin4j.format.hanyupinyinoutputformat net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination ؛ استيراد org.slf4j.logger ؛ استيراد org.slf4j.loggerfactory ؛ java.util.arraylist ؛ java.util.list ؛ pinyinutils {private static final logger = loggerfactory.getLogger (pinyinutils.class) ؛ / ** * تحليل كلمة واحدة * * param str first * return */ public static string [] convert (String str) {string [] reslut = null ؛ hanyupinyinoutputformat hanyupinyinoutputformat = new hanyupinyinoutputformat () ؛ hanyupinyinoutputformat.settonetype (hanyupinyintoneType.without_tone) ؛ حاول {reslut = pinyinhelper.tohanyupinyinstringarray (str.charat (0) ، hanyupinyinoutputformat) ؛ Treeset <String> StringTreSet = New Treeset <> () ؛ لـ (int i = 0 ؛ i <reslut.length ؛ i ++) {if (reslut.length> = 3) {break ؛ } StringTreSet.add (reslut [i] .replace ("u:" ، "v")) ؛ } reslut = new string [stringTreSet.size ()] ؛ reslut = stringTreSet.Toarray (reslut) ؛ } catch (badhanyupinyinoutputformatcombination badhanyupinyinoutputformatcombination) {badhanyupinyinoutputformatcombination.printstacktrace () ؛ } catch (استثناء e) {logger.error ("[[convert]:" ، e) ؛ } إعادة reslut ؛ } / ** * phrase parsing (الكتابة الكاملة) * * param chs * @return * / public static string getelling (String chs) {return translate (chs ، false) ؛ } / ** * الأحرف الصينية إلى pinyin * * param chs * param اختصار * @return * / سلسلة static private (سلسلة chs ، اختصار منطقي) {StringBuffer Buffer = new StringBuffer () ؛ إذا (strings.isnullorempty (chs)) يعود "" ؛ حاول {list <list <string >> temps = new ArrayList <> () ؛ int len = chs.length () ؛ int len1 = 0 ؛ لـ (int i = 0 ؛ i <len ؛ i ++) {list <string> StringList = new ArrayList <> () ؛ مفتاح السلسلة = chs.charat (i) + "" ؛ if (key.getBytes (). length> = 2) {string [] temp = convert (key) ؛ if (temp.length == 0) {contert ؛ } if (temp == null) {StringList.add ("") ؛ } آخر {for (string v: temp) {StringList.add (v) ؛ }}} آخر {StringList.add (KEY) ؛ } temps.add (StringList) ؛ LEN1 ++ ؛ } قائمة <list <string> t = new ArrayList <> () ؛ لـ (int i = 0 ؛ i <len1 ؛ i ++) {list <string> currentList = new ArrayList <> () ؛ قائمة <Tring> StringList = temps.get (i) ؛ if (StringList! = null) {for (string s: stringlist) {if (admory) {s = s.charat (0) + "" ؛ } if (i> 0) {list <string> prelist = t.get (i - 1) ؛ if (prelist! = null) {for (string s1: prelist) {currentList.add (s1 + s) ؛ }}}} آخر {currentList.add (s) ؛ }}} t.add (i ، currentList) ؛ } if (t.size ()> 0) {list <string> currentList = t.get (t.size ()-1) ؛ if (currentList! = null) {for (String Current: CurrentList) {buffer.append (current) ؛ buffer.append ("") ؛ }}} return buffer.toString () ؛ } catch (استثناء e) {logger.error ("[getSTrletters]:" ، e) ؛ يعود ""؛ }} / ** * phrase parsing (الاختصار) * * param chs * @return * / public static string getSmallSelling (String chs) {return translate (chs ، true) ؛ } / ** * احصل على الحرف الأولي * * regurn * / string static public getSortleTs (String pingyin) {try {String sortString = pingyin.substring (0 ، 1) .ToupperCase (locale.getDefault ()) ؛ // التعبير العادي لتحديد ما إذا كان الحرف الأول هو حرف إنجليزي إذا كان (sortString.matches ("[[az]")) {return sortstring.touppercase (locale.getDefault ()) ؛ }} catch (استثناء e) {logger.error ("[getSortlets]:" ، e) ؛ } يعود "#"؛ } public static void main (string [] args) {pinyinutils p = new pinyinutils () ؛ System.out.println (p.getselling ("Single")) ؛ system.out.println (p.getSmallselling ("test")) ؛ }} ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.