JAVA pinyin4j 中文多音字转拼音转字母大写;
class PinyinTool {
/**
* 解析并组合拼音,对象合并方案
*
* @return
*/
private static String[] parseTheChineseByObject(
List<Map<String, Integer>> list) {
String[] resultStr=new String[]{};
Map<String, Integer> first = null; // 用于统计每一次,集合组合数据
// 遍历每一组集合
for (int i = 0; i < list.size(); i++) {
// 每一组集合与上一次组合的Map
Map<String, Integer> temp = new Hashtable<String, Integer>();
Map<String, Integer> daxTemp = new Hashtable<String, Integer>();
// 第一次循环,first为空
if (first != null) {
// 取出上次组合与此次集合的字符,并保存
for (String s : first.keySet()) {
for (String s1 : list.get(i).keySet()) {
if (!Character.isLowerCase(s.charAt(0)))
{
String dax="";
//拼音转大写
String daxStr = s1.toUpperCase().charAt(0) + s1.substring(1);
if (daxStr.length() > 1) {
dax += daxStr.subSequence(0, 1);
} else {
dax += daxStr;
}
dax=s+dax;
daxTemp.put(dax, 2);//拼音大写
continue;
}
String str = s + s1;
temp.put(str, 1);
}
}
// 清理上一次组合数据
if (temp != null && temp.size() > 0) {
first.clear();
}
} else {
for (String s : list.get(i).keySet()) {
String str = s;
String dax="";
//拼音转大写
String daxStr = s.toUpperCase().charAt(0) + s.substring(1);
if (daxStr.length() > 1) {
dax += daxStr.subSequence(0, 1);
} else {
dax += daxStr;
}
daxTemp.put(dax, 2);//拼音大写
temp.put(str, 1);
}
}
// 保存组合数据以便下次循环使用
if (temp != null && temp.size() > 0) {
first = temp;
first.putAll(daxTemp);
}
}
if (first != null) {
// 遍历取出组合字符串
for (String str : first.keySet()) {
//取出大写字符串
if(!Character.isLowerCase(str.charAt(0))){
resultStr[0] += (str + ",");
}
resultStr[1] += (str + ",");
}
}
if (resultStr[0].length() > 0) {
resultStr[0] = resultStr[0].substring(0, resultStr[0].length() - 1);
resultStr[1] = resultStr[1].substring(0, resultStr[1].length() - 1);//大写字符串
}
return resultStr;
}
/**
* 汉字转换位汉语全拼,英文字符不变,特殊字符丢失
* 支持多音字,生成方式如(重当参:zhongdangcen,zhongdangcan,chongdangcen
* ,chongdangshen,zhongdangshen,chongdangcan)
*
* @param chines
* 汉字
* @return 拼音
*/
public static String[] converterToSpell(String chines) {
StringBuffer pinyinName = new StringBuffer();
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 {
// 取得当前汉字的所有全拼
String[] strs = PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat);
if (strs != null) {
for (int j = 0; j < strs.length; j++) {
pinyinName.append(strs[j]);
if (j != strs.length - 1) {
pinyinName.append(",");
}
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName.append(nameChar[i]);
}
pinyinName.append(" ");
}
// return pinyinName.toString();
return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
}
/**
* 去除多音字重复数据
*
* @param theStr
* @return
*/
private static List<Map<String, Integer>> discountTheChinese(String theStr) {
// 去除重复拼音后的拼音列表
List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>();
// 用于处理每个字的多音字,去掉重复
Map<String, Integer> onlyOne = null;
String[] firsts = theStr.split(" ");
// 读出每个汉字的拼音
for (String str : firsts) {
onlyOne = new Hashtable<String, Integer>();
String[] china = str.split(",");
// 多音字处理
for (String s : china) {
Integer count = onlyOne.get(s);
if (count == null) {
onlyOne.put(s, new Integer(1));
} else {
onlyOne.remove(s);
count++;
onlyOne.put(s, count);
}
}
mapList.add(onlyOne);
}
return mapList;
}
}