Os exemplos deste artigo compartilham o código específico dos caracteres chineses Java para a ferramenta Pinyin para sua referência. O conteúdo específico é o seguinte
importar com.google.common.base.strings; importação net.sourcegege.pinyin4j.pinyinhelper; importação net.sourceforge.pinyin4j.format.hanyupinyInoutputFormat; importação net.sourceGe.pinyIn4j.format.hanyUpinImaty; Import.SourceGe.PinyIn4J.Format.hanyUsinTinEnType; net.sourceforge.pinyin4j.format.exception.badhanyUpinyInOutputFormatCombination; importar org.slf4j.logger; importar org.slf4j.loggerfactory; import javailil.util.arraylist; imporjé.util.list; import.utililil.util.ArrayList; import Java.util.List; Pinyinutils {private estático final logger = loggerFactory.getLogger (pinyinutils.class); / ** * Palavalidade de uma única palavra * * @param str primeiro * @return */ public static string [] convert (string str) {string [] reslut = null; HanyUpinyInOutputFormat HanyUpinyInOutputFormat = new HanyUpinyInOutputFormat (); hanyupinyInoutputFormat.SettoneType (hanyupinyIntoneType.without_tone); tente {reslut = pinyinhelper.tohanyupinyinstringArray (str.charat (0), hanyupinyInoutputFormat); TreeSet <String> stringtreeSet = new TreeSet <> (); for (int i = 0; i <reslut.length; i ++) {if (reslut.length> = 3) {break; } stringtreeset.add (reslut [i] .Rearplace ("u:", "v")); } reslut = new String [StringTreeSet.size ()]; reslut = stringtreeset.toArray (Reslut); } catch (badhanyUpinyInoutputFormatComBination badhanyUpinyInOutputFormatCombination) {badhanyUpinyInoutputFormatCombination.printStackTrace (); } catch (Exceção e) {Logger.error ("[convert]:", e); } retornar reslut; } / ** * Phrase Parsing (escrita completa) * * @param CHS * @return * / public static string gettselling (string chs) {return transllate (chs, false); } / ** * caracteres chineses para pinyin * * @param chs * @param sigla * @return * / string estática privada tradução (string chs, sigla booleana) {stringbuffer buffer = new stringBuffer (); if (strings.isnullorEmpty (chs)) retornar ""; tente {list <list <string>> temps = new ArrayList <> (); int len = chs.length (); int len1 = 0; for (int i = 0; i <len; i ++) {list <string> stringList = new ArrayList <> (); String key = chs.charat (i) + ""; if (key.getBytes (). length> = 2) {string [] temp = convert (key); if (temp.length == 0) {continua; } if (temp == null) {stringList.add (""); } else {for (string v: temp) {stringList.add (v); }}} else {stringList.add (key); } temps.add (stringList); Len1 ++; } Lista <list <string>> t = new ArrayList <> (); for (int i = 0; i <len1; i ++) {list <tring> currentList = new ArrayList <> (); List <string> stringList = temps.get (i); if (stringList! = null) {for (string s: stringList) {if (sigrony) {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); }}}} else {currentList.add (s); }}} t.add (i, CurrentList); } if (t.size ()> 0) {list <tring> currentList = t.get (t.size ()-1); if (currentList! = null) {for (string atual: currentlist) {buffer.append (atual); buffer.append (""); }}} return buffer.toString (); } Catch (Exceção e) {Logger.error ("[getSortLetters]:", e); retornar ""; }} / ** * Phrase Parsing (abreviação) * * @param chs * @return * / public static string getsMallSellSelling (string chs) {return tradutor (chs, true); } / ** * Obtenha a letra inicial * * @return * / public static string getSortletters (string pingyin) {try {string SortString = pingyin.substring (0, 1) .touppercase (loce.getDefault ()); // Expressão regular para determinar se a primeira letra é uma letra em inglês se (SortString.Matches ("[AZ]")) {return sortString.ToupPercase (Locale.getDefault ()); }} catch (Exceção e) {Logger.error ("[getSortLeTters]:", e); } retornar "#"; } public static void main (string [] args) {pinyinutils p = novo pinyinUtils (); System.out.println (p.getSelling ("single")); System.out.println (P.GetsMallSelling ("Test")); }} O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.