package util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
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 classHanyuPinyinUtil {public static MaphanziToPinyin(String name) {
Map map = new HashMap();
map.put("name", name);returnhanziToPinyin(map);
}public static Map hanziToPinyin(Mapmap) {
Map newMap = null;//参数校验
if (map == null) return null;//map 是否有元素//if(map.isEmpty()) return null;//不存在键名 name
if (!map.containsKey("name")) return null;//校验值
String hanzi = map.get("name");//是否为null 或空白字符
if (hanzi == null || hanzi.trim().equals("")) return null;//去除特殊字符只留汉字
while (!hanzi.matches("[\\u4e00-\\u9fa5]+")) {//替换掉非中文
hanzi = hanzi.replaceAll("[^\\u4e00-\\u9fa5]+", "");
}if (hanzi.equals("")) return null;
newMap= new HashMap();
newMap.put("name", map.get("name"));//获取 汉字字符串的首个中文字符的首个字母
newMap.put("firstChar", getFirstChar(hanzi));//获取全拼音
newMap.put("all", getPinyinAll(hanzi));//获取拼音缩写
newMap.put("acronym", getPinyinAcronym(hanzi));returnnewMap;
}//获取汉字字符串的首个汉字的首字母
private static SetgetFirstChar(String hanzi) {
Set firsts = null;//获取汉字字符串的拼音
String[][] pinyins =getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0 && pinyins[0].length > 0) {
firsts= new HashSet();
String[] temp= pinyins[0];for(String str : temp) {if (str != null && !str.trim().equals("")) {
firsts.add(Character.toString(str.charAt(0)));
}
}
}returnfirsts;
}//获取中文字符串的全拼音(多音字的不同组合)
private static SetgetPinyinAll(String hanzi) {
Set set = null;
String[][] pinyins=getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0) {
List list = new ArrayList();//获取首个汉字的所有拼音(多音字)
int n = 0;for (int i = 0; i < pinyins[0].length; i++) {
list.add(newStringBuffer(pinyins[n][i]));
}//循环取下个中文的拼音, 处理多种组合
while (pinyins.length > ++n) {//复制一份
List tempList1 = new ArrayList();//确保list里的对象不是相同的
for (int i = 0; i < list.size(); i++) {
tempList1.add(new StringBuffer(list.get(i).toString()));
}//处理 多个中文的拼音组合
int n2 = 0;for (int i = 0; i < list.size(); i++) {
list.get(i).append(pinyins[n][n2]);
}do{//再复制一份 确保list里的对象不是相同的
List tempList2 = new ArrayList();for (int i = 0; i < tempList1.size(); i++) {
tempList2.add(new StringBuffer(tempList1.get(i).toString()));
}//处理 多个中文的拼音组合
for (int i = 0; i < tempList2.size(); i++) {
tempList2.get(i).append(pinyins[n][n2]);
}
list.addAll(tempList2);
}while (pinyins[n].length > ++n2);
}set = new TreeSet();for (int i = 0; i < list.size(); i++) {set.add(list.get(i).toString());
}
}return set;
}//和获取中文全拼音相同,这里只是获取中文拼音的首字母
private static SetgetPinyinAcronym(String hanzi) {
Set set = null;
String[][] pinyins=getHanyuPinyin(hanzi);if (pinyins != null && pinyins.length > 0) {
List list = new ArrayList();int n = 0;for (int i = 0; i < pinyins[0].length; i++) {
list.add(new StringBuffer(Character.toString(pinyins[n][i].charAt(0))));
}while (pinyins.length > ++n) {
List tempList1 = new ArrayList();for (int i = 0; i < pinyins[0].length; i++) {
tempList1.add(new StringBuffer(list.get(i).toString()));
}int n2 = 0;for (int i = 0; i < list.size(); i++) {
list.get(i).append(pinyins[n][n2].charAt(0));
}do{
List tempList2 = new ArrayList();for (int i = 0; i < tempList1.size(); i++) {
tempList2.add(new StringBuffer(tempList1.get(i).toString()));
}for (int i = 0; i < tempList2.size(); i++) {
tempList2.get(i).append(pinyins[n][n2].charAt(0));
}
list.addAll(tempList2);
}while (pinyins[n].length > ++n2);
}set = new TreeSet();for (int i = 0; i < list.size(); i++) {set.add(list.get(i).toString());
}
}return set;
}//把汉字转成拼音
private staticString[][] getHanyuPinyin(String hanzi) {
String[][] pinyins= null;//自定义转成拼音后的模板
HanyuPinyinOutputFormat format = newHanyuPinyinOutputFormat();//设置转后的拼音为全小写字母
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);//无音调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//特殊拼音(ü)用(v)替代
format.setVCharType(HanyuPinyinVCharType.WITH_V);//先初始化一维数组
pinyins = newString[hanzi.length()][];for (int i = 0; i < hanzi.length(); i++) {char word =hanzi.charAt(i);try{//如果不是汉字会返回null
String[] temp =PinyinHelper.toHanyuPinyinStringArray(word, format);//去除重复
Set set = new HashSet();for (int j = 0; j < temp.length; j++) {set.add(temp[j]);
}//初始化对应的二维数组
pinyins[i] = new String[set.size()];int n = 0;for (String str : set) {
pinyins[i][n++] =str;
}
}catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
}returnpinyins;
}
}