linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package org.springblade.modules.words;
 
import org.springblade.modules.words.internals.PinyinDict;
import org.springblade.modules.words.internals.Translate;
 
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
 
public class WordsHelper {
 
    /**
     * 获取首字母,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param text 原文本
     * @return
     * @throws IOException
     * @throws NumberFormatException
     */
    public static String GetFirstPinyin(String text) throws NumberFormatException, IOException {
        return PinyinDict.GetFirstPinyin(text, 0);
    }
 
    /**
     * 获取拼音全拼, 不支持多音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证 请使用GetPinyin方法,此方法不支持多音
     *
     * @param text 原文本
     * @param tone 是否带声调
     * @return
     * @throws IOException
     * @throws NumberFormatException
     */
    public static String GetPinyinFast(String text, Boolean tone) throws NumberFormatException, IOException {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            Character c = text.charAt(i);
            sb.append(PinyinDict.GetPinyinFast(c, tone ? 1 : 0));
        }
        return sb.toString();
    }
 
    /**
     * 获取拼音全拼, 不支持多音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证 请使用GetPinyin方法,此方法不支持多音
     *
     * @param text
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static String GetPinyinFast(String text) throws NumberFormatException, IOException {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            Character c = text.charAt(i);
            sb.append(PinyinDict.GetPinyinFast(c, 0));
        }
        return sb.toString();
    }
 
    /**
     * 获取拼音全拼,支持多音,中文字符集为[0x4E00,0x9FD5]
     *
     * @param text 原文本
     * @param tone 是否带声调
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static String GetPinyin(String text, Boolean tone) throws NumberFormatException, IOException {
        return PinyinDict.GetPinyin(text, tone ? 1 : 0);
    }
 
    /**
     * 获取拼音全拼,支持多音,中文字符集为[0x4E00,0x9FD5]
     *
     * @param text
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static String GetPinyin(String text) throws NumberFormatException, IOException {
        return PinyinDict.GetPinyin(text, 0);
    }
 
    /**
     * 获取所有拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param c    原文本
     * @param tone 是否带声调
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static List<String> GetAllPinyin(char c, Boolean tone) throws NumberFormatException, IOException {
        return PinyinDict.GetAllPinyin(c, tone ? 1 : 0);
    }
 
    /**
     * 获取所有拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param c
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static List<String> GetAllPinyin(char c) throws NumberFormatException, IOException {
        return PinyinDict.GetAllPinyin(c, 0);
    }
 
    /**
     * 获取姓名拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param name 姓名
     * @param tone 是否带声调
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static String GetPinyinForName(String name, Boolean tone) throws NumberFormatException, IOException {
        return String.join("", PinyinDict.GetPinyinForName(name, tone ? 1 : 0));
    }
 
    /**
     * 获取姓名拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param name
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static String GetPinyinForName(String name) throws NumberFormatException, IOException {
        return String.join("", PinyinDict.GetPinyinForName(name, 0));
    }
 
    /**
     * 获取姓名拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     *
     * @param name 姓名
     * @param tone 是否带声调
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static List<String> GetPinyinListForName(String name, Boolean tone)
            throws NumberFormatException, IOException {
        return PinyinDict.GetPinyinForName(name, tone ? 1 : 0);
    }
 
    /**
     * 获取姓名拼音,中文字符集为[0x3400,0x9FD5],注:偏僻汉字很多未验证
     * @param name
     * @return
     * @throws NumberFormatException
     * @throws IOException
     */
    public static List<String> GetPinyinListForName(String name) throws NumberFormatException, IOException {
        return PinyinDict.GetPinyinForName(name, 0);
    }
 
 
    /**
     * 判断输入是否为中文 ,中文字符集为[0x4E00,0x9FA5]
     *
     * @param content
     * @return
     */
    public static boolean HasChinese(String content) {
        return Pattern.matches("[\\u3400-\\u4db5\\u4e00-\\u9fd5]", content);
    }
 
    /**
     * 判断输入是否全为中文,中文字符集为[0x4E00,0x9FA5]
     *
     * @param content
     * @return
     */
    public static boolean IsAllChinese(String content) {
        return Pattern.matches("^[\\u3400-\\u4db5\\u4e00-\\u9fd5]*$", content);
    }
 
    /**
     * 判断含有英语
     *
     * @param content
     * @return
     */
    public static boolean HasEnglish(String content) {
        return Pattern.matches("[A-Za-z]", content);
    }
 
    /**
     * 判断是否全部英语
     *
     * @param content
     * @return
     */
    public static boolean IsAllEnglish(String content) {
        return Pattern.matches("^[A-Za-z]*$", content);
    }
 
    /**
     * 半角转全角
     *
     * @param input
     * @return
     */
    public static String ToSBC(String input) {
        StringBuilder sb = new StringBuilder(input);
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == 32) {
                sb.setCharAt(i, (char) 12288);
            } else if (c < 127) {
                sb.setCharAt(i, (char) (c + 65248));
            }
        }
        return sb.toString();
    }
 
    /**
     * 转半角的函数
     *
     * @param input
     * @return
     */
    public static String ToDBC(String input) {
        StringBuilder sb = new StringBuilder(input);
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == 12288) {
                sb.setCharAt(i, (char) 32);
            } else if (c > 65280 && c < 65375) {
                sb.setCharAt(i, (char) (c - 65248));
            }
        }
        return sb.toString();
    }
 
    /**
     * 转繁体中文
     *
     * @param text
     * @return
     * @throws Exception
     */
    public static String ToTraditionalChinese(String text) throws Exception {
        return Translate.ToTraditionalChinese(text, 0);
    }
 
    /**
     * 转繁体中文
     *
     * @param text
     * @param type 0、繁体中文,1、港澳繁体,2、台湾正体
     * @return
     * @throws Exception
     */
    public static String ToTraditionalChinese(String text, int type) throws Exception {
        return Translate.ToTraditionalChinese(text, type);
    }
 
    /***
     * 转简体中文
     *
     * @param text
     * @return
     * @throws Exception
     */
    public static String ToSimplifiedChinese(String text) throws Exception {
        return Translate.ToSimplifiedChinese(text, 0);
    }
 
    /**
     * 转简体中文
     *
     * @param text
     * @param srcType 0、繁体中文,1、港澳繁体,2、台湾正体
     * @return
     * @throws Exception
     */
    public static String ToSimplifiedChinese(String text, int srcType) throws Exception {
        return Translate.ToSimplifiedChinese(text, srcType);
    }
 
}