欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年01月30日 15:54:03   作者:小甜瓜安東泥  
這篇文章主要為大家詳細介紹了java漢字轉(zhuǎn)拼音工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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());
      // 正則表達式,判斷首字母是否是英文字母
      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("單個"));
    System.out.println(p.getSmallSelling("測試"));
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA插件指南之Mybatis?log插件安裝及使用方法

    IDEA插件指南之Mybatis?log插件安裝及使用方法

    這篇文章主要給大家介紹了關(guān)于IDEA插件指南之Mybatis?log插件安裝及使用的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-02-02
  • JAVA面試題之緩存擊穿、緩存穿透、緩存雪崩的三者區(qū)別

    JAVA面試題之緩存擊穿、緩存穿透、緩存雪崩的三者區(qū)別

    當(dāng)服務(wù)器QPS比較高,并且對數(shù)據(jù)的實時性要求不高時,往往會接入緩存以達到快速Response、降低數(shù)據(jù)庫壓力的作用,常用來做緩存的中間件如Redis等。本文主要介紹了JAVA面試時??嫉木彺鎿舸⒋┩?、雪崩場景三者區(qū)別,有興趣的小伙伴可以看一下
    2021-11-11
  • spring boot項目fat jar瘦身的實現(xiàn)

    spring boot項目fat jar瘦身的實現(xiàn)

    這篇文章主要介紹了spring boot項目fat jar瘦身的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Spring Boot之搞定mongoTemplate的知識小結(jié)

    Spring Boot之搞定mongoTemplate的知識小結(jié)

    這篇文章主要介紹了Spring Boot之搞定mongoTemplate的知識小結(jié),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • EL調(diào)用Java方法_動力節(jié)點Java學(xué)院整理

    EL調(diào)用Java方法_動力節(jié)點Java學(xué)院整理

    簡單來說,我們在一個類中的某個方法,可以使用EL進行調(diào)用,這個能被EL表達式調(diào)用的方法稱之為EL函數(shù),但是這種方式必須滿足兩點要求,具體哪兩點,大家可以參考下本文
    2017-07-07
  • Java 自定義錯誤類示例代碼

    Java 自定義錯誤類示例代碼

    以下是對Java中自定義錯誤類的示例代碼進行了介紹。需要的朋友可以過來參考下
    2013-08-08
  • 一文解決pom.xml報錯Dependency "xxx" not found的問題

    一文解決pom.xml報錯Dependency "xxx" not f

    我們在使用maven進行jar包管理時有時會遇到pom.xml中報錯Dependency “XXX” not found,所以在本文中將給大家介紹一下pom.xml報錯Dependency "xxx" not found的解決方案,需要的朋友可以參考下
    2024-01-01
  • 關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系

    關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系

    這篇文章主要介紹了關(guān)于@Autowired注解和靜態(tài)方法及new的關(guān)系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java8到Java19的一些變化分析詳解

    Java8到Java19的一些變化分析詳解

    這篇文章主要為大家介紹了Java8到Java19的一些變化分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • springcloud使用profile實現(xiàn)多環(huán)境配置方式

    springcloud使用profile實現(xiàn)多環(huán)境配置方式

    這篇文章主要介紹了springcloud使用profile實現(xiàn)多環(huán)境配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論