Les exemples de cet article partagent le code spécifique des caractères chinois Java à Pinyin pour votre référence. Le contenu spécifique est le suivant
import com.google.common.base.strings; import net.sourceforge.pinyin4j.pinyinhelper; import net.sourceforge.pinyin4j.format.hanyupinyinoutputat; import net.sourceforge.pinyin4j.format.hanyupinyIntoneType; net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.util.arraylist; import java.util.list; import java.util.locale; import java.util. PinyInUtils {Logger final statique privé Logger = loggerfactory.getLogger (pinyinutils.class); / ** * analyse de mot unique * * @param str d'abord * @return * / public static String [] convert (String str) {String [] resLut = null; Hanyupinyinoutputformat hanyupinyinoutputformat = new hanyupinyinoutputFormat (); hanyupinyinoutputformat.settonetype (hanyupinyintonetype.without_tone); try {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] .replace ("U:", "V")); } resLut = new String [stringTreeSet.size ()]; resLut = StringTreeSet.toArray (Reslut); } catch (badhanyupinyinoutputformatcombination badhanyupinyinoutputformatcombination) {badhanyupinyInTputFormatcombination.printStackTrace (); } catch (exception e) {logger.error ("[converti]:", e); } return reslut; } / ** * PHRASE PARSING (Écriture complète) * * @param chs * @return * / public static string getselling (String chs) {return tradlate (chs, false); } / ** * Caractéristiques chinoises à pinyin * * @param chs * @param acronyme * @return * / private static String tradat (String chs, boolean acronym) {StringBuffer tamper = new StringBuffer (); if (Strings.isnullorempty (chs)) renvoie ""; essayez {list <list <string>> temps = new ArrayList <> (); int len = chs.length (); int len1 = 0; pour (int i = 0; i <len; i ++) {list <string> stringList = new ArrayList <> (); String key = chs.charat (i) + ""; if (key.getBytes (). Longueur> = 2) {String [] temp = convert (key); if (temp.Length == 0) {continuant; } if (temp == null) {stringList.add (""); } else {for (string v: temp) {stringList.add (v); }}} else {stringList.add (key); } temps.add (stringList); Len1 ++; } List <list <string>> t = new ArrayList <> (); for (int i = 0; i <len1; i ++) {list <string> currentList = new ArrayList <> (); List <string> stringList = temps.get (i); if (stringList! = null) {for (string s: stringList) {if (acronyment) {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 <string> currentList = t.get (t.size () - 1); if (currentList! = null) {for (String current: currentList) {buffer.append (current); Buffer.APPEND (""); }}} return buffer.toString (); } catch (exception e) {logger.error ("[getOrtletters]:", e); retour ""; }} / ** * PHRASE PARSING (Abréviation) * * @param chs * @return * / public static static getSmallSelling (String chs) {return tradlate (chs, true); } / ** * Obtenez la lettre initiale * * @return * / public static String getsOrtletters (String pingyin) {try {String sortString = pingyin.substring (0, 1) .toupperase (locale.getdefault ()); // Expression régulière pour déterminer si la première lettre est une lettre anglaise if (sortstring.matches ("[az]")) {return sortstring.touppercase (locale.getDefault ()); }} catch (exception e) {logger.error ("[getOrtletters]:", e); } retour "#"; } public static void main (string [] args) {pinyinutils p = new pinyinutils (); System.out.println (p.getSelling ("Single")); System.out.println (p.getsmallSelling ("test")); }} Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.