本文实例为大家分享了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("测试"));
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/YuyuanNo1/p/8385332.html
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 40
-
2025-05-29 84
-
2025-05-29 83
-
2025-05-25 45
-
购买域名后,如何通过WordPress等CMS系统轻松创建网站?
2025-06-04 42
热门评论

