一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - java漢字轉拼音工具類分享

java漢字轉拼音工具類分享

2021-03-29 10:15小甜瓜安東泥 JAVA教程

這篇文章主要為大家詳細介紹了java漢字轉拼音工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java漢字轉拼音工具類的具體代碼,供大家參考,具體內容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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);
  }
 
  /**
   * 漢字轉拼音
   *
   * @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("測試"));
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/YuyuanNo1/p/8385332.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人高清精品免费5388密 | 99久久伊人一区二区yy5099 | 爸爸的宝贝小说全文在线阅读 | 国产麻豆精品免费视频 | 男人与禽交的方法 | 欧美另类69xxx | 99热在线免费观看 | 91久久国产露脸精品 | 91久久偷偷做嫩草影院免费看 | 亚洲AV精品无码喷水直播间 | 91麻豆精品国产片在线观看 | 欧美性f| 91看片淫黄大片欧美看国产片 | 我与岳乱短篇小说 | 久久综合中文字幕佐佐木希 | 隔壁老王国产在线精品 | jzzjlzz亚洲乱熟在线播放 | 好紧好爽的午夜寂寞视频 | 动漫美女被羞羞产奶 | 国产在线精品99一卡2卡 | 99精品视频一区在线观看miya | 国产精品一区二区三区久久 | 国产图片综合区 | 星星动漫在线观看免费 | 亚洲欧美日韩一区成人 | 日本视频在线免费播放 | 国产精品久久久久久久久久久搜索 | 我强进了老师身体在线观看 | 99er热| 乖女的嫩奶水h文孕妇 | 四虎影院网址大全 | 久久精品手机观看 | 亚洲精品青青草原avav久久qv | 亚洲天堂色图 | 免费观看欧美成人禁片 | 四虎最新永久免费网址 | 精品无码久久久久久久久 | 国产一级毛片国语版 | 精品亚洲综合在线第一区 | 亚洲视频一 | 天使萌痴汉在线中文字幕 |