获取中文姓名的拼音(含读音特殊姓氏)
获取中文姓名的拼音,或者首字母(因为中文姓名包含特殊读法,如单dan字作为姓氏时读shan)。
- 特殊读音的配置文件-pinyin.properties,放置在项目跟目录。
区=OU
缪=MIAO
晟=CHENG
乐=YUE
员=YUN
贠=YUN
黑=HE
重=CHONG
仇=QIU
秘=BI
冼=XIAN
解=XIE
折=SHE
单=SHAN
朴=PIAO
翟=ZHA
查=ZHA
盖=GE
万俟=MO QI
尉迟=YU CHI
项目中增加jar包
链接:https://pan.baidu.com/s/1NZqHWwtBvh_DUzsJ1Nq8hA
提取码:pseujava代码
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class Pinyin {
//特殊姓氏的转换
private static String path= "/pinyin.properties";
/**
* 将字符串中的中文转化为拼音,英文字符不变
*
* @param inputString 姓名
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
String output = "";
if (inputString != null && inputString.length() > 0 && !"null".equals(inputString)) {
String substring = diffPro(inputString);
char[] input = substring.trim().toCharArray();
try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
input[i], format);
output += temp[0]+" ";
} else{
output += java.lang.Character.toString(input[i]);
}
output = output;
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
return "";
}
return output;
}
/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines 汉字
* @return 拼音首字母
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
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].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
/**
* 与pinyin.properties中多音字的姓名对比
* @param inputString 中文姓名
* @return 百家姓的拼音读法
*/
private static String diffPro(String inputString){
Properties properties = new Properties();
try {
InputStreamReader reader =new InputStreamReader(Pinyin.class.getResourceAsStream(path),"utf-8");
properties.load(reader);
reader.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Set keyValue = properties.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();){
String key = (String) it.next();
if(inputString.substring(0, key.length()).equals(key)){
String value = inputString.substring(key.length(),inputString.length());
return properties.getProperty(key)+" "+value;
}
}
return inputString;
}
}
测试
String strs1 = Pinyin.getPingYin("单雄信");//拼音全拼
String strs2 = Pinyin.converterToFirstSpell("阿奇");//拼音首字母
System.out.println("输出结果:"+strs1+"\n输出结果:"+strs2);