Pinyin4JUtil.java
4.94 KB
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
package com.bs.cardata.utils;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
import com.bs.common.utils.StringUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
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 org.springframework.util.CollectionUtils;
import java.util.List;
public class Pinyin4JUtil {
private static Logger logger = LoggerFactory.getLogger(Pinyin4JUtil.class);
/**
* 汉字转换位汉语拼音,英文字符不变
* @param chines 汉字
* @return 拼音
*/
public static String converterToSpell(String chines){
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}else{
pinyinName += nameChar[i];
}
}
return pinyinName;
}
public static String getFirstLetter(String chines) {
chines = removeSpecialStr(chines);
String pinyin = converterToSpell(chines);
String currentRes = String.valueOf(pinyin.charAt(0)).toUpperCase();
return convertNumber(currentRes);
}
public static String convertNumber(String fLetter) {
if (fLetter.equals("0")) {
fLetter = "L";
} else if (fLetter.equals("1")) {
fLetter = "Y";
} else if (fLetter.equals("2")) {
fLetter = "E";
} else if (fLetter.equals("3")) {
fLetter = "S";
} else if (fLetter.equals("4")) {
fLetter = "S";
} else if (fLetter.equals("5")) {
fLetter = "W";
} else if (fLetter.equals("6")) {
fLetter = "L";
} else if (fLetter.equals("7")) {
fLetter = "Q";
} else if (fLetter.equals("8")) {
fLetter = "B";
} else if (fLetter.equals("9")) {
fLetter = "J";
}
return fLetter;
}
/**
* 去掉chines里的特殊字符
*/
private static String removeSpecialStr(String chines) {
logger.info("chines before = " + chines);
String regex = "[^0-9a-zA-Z\\u4e00-\\u9fa5]";
chines = chines.replaceAll(regex, "");
logger.info("chines after = " + chines);
return chines;
}
public static void main(String[] args) throws Exception {
//System.out.println(converterToSpell("欢迎T4(到来)"));
/*File file = new File("c:/user.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while((line=reader.readLine())!=null){
//System.out.println(line);
String t = converterToSpell(line.trim());
System.out.println(t);
}
reader.close();*/
String content = "3";
System.out.println(Pinyin4JUtil.getFirstLetter(content));
System.out.println(Pinyin4JUtil.convertNumber(content));
String content2 = "3大";
System.out.println(Pinyin4JUtil.getFirstLetter(content2));
System.out.println(Pinyin4JUtil.convertNumber(content2));
System.out.println("==============================================");
System.out.println("==============================================");
/*
==============================================
增加对多音字的处理
==============================================
*/
String content3 = "长安";
System.out.println(Pinyin4JUtil.getFirstLetterPolyphonic(content3));
String content4 = "长大";
System.out.println(Pinyin4JUtil.getFirstLetterPolyphonic(content4));
System.out.println(Pinyin4JUtil.getFirstLetterPolyphonic(content));
System.out.println(Pinyin4JUtil.getFirstLetterPolyphonic(content2));
}
public static String getFirstLetterPolyphonic(String chineseStr) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(chineseStr);
if (CollectionUtils.isEmpty(pinyins)) {
return StringUtils.EMPTY;
}
String pinyinWithout = String.valueOf(pinyins.get(0).getPinyinWithoutTone());
if ("none".equals(pinyinWithout)) {
return getFirstLetter(chineseStr);
}
return String.valueOf(pinyinWithout.charAt(0));
}
}