溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java漢字轉(zhuǎn)拼音工具類分享

發(fā)布時(shí)間:2020-09-13 17:07:41 來(lái)源:腳本之家 閱讀:148 作者:小甜瓜安東泥 欄目:編程語(yǔ)言

本文實(shí)例為大家分享了java漢字轉(zhuǎn)拼音工具類的具體代碼,供大家參考,具體內(nèi)容如下

import com.google.common.base.Strings;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import 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.TreeSet;

public class PinyinUtils {
  private static final Logger 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);
    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) {
      badHanyuPinyinOutputFormatCombination.printStackTrace();
    } catch (Exception e) {
      logger.error("[convert]: ", e);
    }
    return reslut;
  }

  /**
   * 詞組解析(全寫)
   *
   * @param chs
   * @return
   */
  public static String getSelling(String chs) {
    return translate(chs, false);
  }

  /**
   * 漢字轉(zhuǎn)拼音
   *
   * @param chs
   * @param acronym
   * @return
   */
  private static String translate(String chs, boolean acronym) {
    StringBuffer buffer=new StringBuffer();
    if (Strings.isNullOrEmpty(chs))
      return "";
    try {
      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) {
            continue;
          }
          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 (acronym) {
              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("[getSortLetters]: ", e);
      return "";
    }
  }

  /**
   * 詞組解析(縮寫)
   *
   * @param chs
   * @return
   */
  public static String getSmallSelling(String chs) {
    return translate(chs, true);
  }

  /**
   * 獲取首字母
   *
   * @return
   */
  public static String getSortLetters(String pingyin) {
    try {
      String sortString = pingyin.substring(0, 1).toUpperCase(Locale.getDefault());
      // 正則表達(dá)式,判斷首字母是否是英文字母
      if (sortString.matches("[A-Z]")) {
        return sortString.toUpperCase(Locale.getDefault());
      }
    } catch (Exception e) {
      logger.error("[getSortLetters]: ", e);
    }
    return "#";
  }

  public static void main(String [] args) {
    PinyinUtils p = new PinyinUtils();

    System.out.println(p.getSelling("單個(gè)"));
    System.out.println(p.getSmallSelling("測(cè)試"));
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI