Java拼音拆分算法
2018-07-20 来源:open-open
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)); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐