2019独角兽企业重金招聘Python工程师标准>>>
public static String polishPinyin(String name) {
if (name == null || "".equals(name))
return name;
name = name.replaceAll("[0-9_]+", "");
StringBuffer buffer = new StringBuffer();
char[] chars = name.toCharArray();
int size = chars.length;
int i = 0;
boolean flag = true;// 当前检测的是声母还是韵母
while (i < size) {
if (flag) {
// 先判断前两位是不是声母
if (i + 1 < size && PinyinUtil.isShengmu(chars[i], chars[i + 1])) {
buffer.append(chars, i, 2);
i += 2;
flag = false;
} else if (PinyinUtil.isShengmu(chars[i])) {
buffer.append(chars[i]);
flag = false;
i++;
} else {
i++;
}
if (i == size)
buffer.append("%");
} else {
if (i + 2 < size && PinyinUtil.isYunmu(chars[i], chars[i + 1], chars[i + 2])) {
buffer.append(chars, i, 3);
i += 3;
} else if (i + 1 < size && PinyinUtil.isYunmu(chars[i], chars[i + 1])) {
buffer.append(chars, i, 2);
i += 2;
} else if (PinyinUtil.isYunmu(chars[i])) {
buffer.append(chars[i]);
i++;
} else {
buffer.append("%");
}
flag = true;
}
}
return buffer.toString();
}
public class PinyinUtil {
private static final String[] Shengmu = { "b", "p", "m", "f", "d", "t", "l", "n", "g", "k", "h", "j", "q", "x", "z",
"c", "s", "zh", "ch", "sh", "y", "w" };
private static final String[] Yunmu = { "a", "o", "e", "i", "u", "v", "ai", "ei", "ui", "ao", "ou", "iu", "ie",
"ue", "ve", "er", "an", "en", "in", "un", "ang", "eng", "ing", "ong" };
public static boolean isShengmu(String str) {
for (String shengmu : Shengmu)
if (shengmu.equalsIgnoreCase(str))
return true;
return false;
}
public static boolean isShengmu(char c) {
return isShengmu(String.valueOf(c));
}
public static boolean isShengmu(char c1, char c2) {
char[] chars = { c1, c2 };
return isShengmu(String.valueOf(chars));
}
public static boolean isYunmu(String str) {
for (String yunmu : Yunmu)
if (yunmu.equalsIgnoreCase(str))
return true;
return false;
}
public static boolean isYunmu(char c) {
return isYunmu(String.valueOf(c));
}
public static boolean isYunmu(char c1, char c2) {
char[] chars = { c1, c2 };
return isYunmu(String.valueOf(chars));
}
public static boolean isYunmu(char c1, char c2, char c3) {
char[] chars = { c1, c2, c3 };
return isYunmu(String.valueOf(chars));
}
}