直接上代码
1、pom.xml
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
2、工具类
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 java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class PinyinUtil
{
/**
* 多音字拼音
*/
public static Map<String, String> FIRST_NAME_PINYIN = new HashMap<>();
static {
FIRST_NAME_PINYIN.put("繁", "po");
FIRST_NAME_PINYIN.put("区", "ou");
FIRST_NAME_PINYIN.put("仇", "qiu");
FIRST_NAME_PINYIN.put("种", "chong");
FIRST_NAME_PINYIN.put("单", "shan");
FIRST_NAME_PINYIN.put("解", "xie");
FIRST_NAME_PINYIN.put("查", "zha");
FIRST_NAME_PINYIN.put("曾", "zeng");
FIRST_NAME_PINYIN.put("秘", "bi");
FIRST_NAME_PINYIN.put("乐", "yue");
FIRST_NAME_PINYIN.put("重", "chong");
FIRST_NAME_PINYIN.put("朴", "piao");
FIRST_NAME_PINYIN.put("缪", " miao");
FIRST_NAME_PINYIN.put("冼", " xian");
FIRST_NAME_PINYIN.put("翟", "zhai");
FIRST_NAME_PINYIN.put("折", "she");
FIRST_NAME_PINYIN.put("黑", "he");
FIRST_NAME_PINYIN.put("盖", "ge");
FIRST_NAME_PINYIN.put("沈", "shen");
FIRST_NAME_PINYIN.put("尉迟", "yuchi");
FIRST_NAME_PINYIN.put("万俟", "moqi");
}
/**
* @Description: 过滤掉中文特殊字符
* @throws PatternSyntaxException
*/
public static String filterSpecialChars(String target) throws PatternSyntaxException
{
String regEx = "[\\uac00-\\ud7af\\u3130–\\u318F]*[\\u0800-\\u4e00`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(target);
return mat.replaceAll("").trim();
}
/**
* 获取汉字串全拼音,英文字符不变
* @param chinese 汉字串
* @return 汉语拼音
*/
private static String chinese2Spell(String chinese)
{
try {
chinese = PinyinUtil.filterSpecialChars(chinese);
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (pinyinStringArray != null && pinyinStringArray.length > 0) {
pybf.append(pinyinStringArray[0]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.getMessage();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
} catch (Exception e) {
e.getMessage();
return null;
}
}
/**
* 获得完整拼音
* 解决姓多音字问题
*
* @param keywords
* @return
*/
public static String getFullPinyin(String keywords) {
if(keywords == null || "".equals(keywords)) {
return keywords;
}
for (Map.Entry<String, String> map : FIRST_NAME_PINYIN.entrySet()) {
if (keywords.startsWith(map.getKey())) {
String suffix = keywords.substring(map.getKey().length());
return map.getValue() + chinese2Spell(suffix);
}
}
return chinese2Spell(keywords);
}
public static void main(String[] args) {
System.out.println(getFullPinyin("尉迟恭啊"));
System.out.println(getFullPinyin("乐毅啊"));
System.out.println(getFullPinyin("刘言曌"));
}
}
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏