Commit 22e39b42 by zhangxingmin

push

parent 3fcb1e98
...@@ -282,6 +282,47 @@ public class ApiCsfCommonServiceImpl implements ApiCsfCommonService { ...@@ -282,6 +282,47 @@ public class ApiCsfCommonServiceImpl implements ApiCsfCommonService {
} }
} }
// /**
// * 中文转拼音辅助方法
// * @param chinese
// * @return
// */
// private String convertChineseToPinyin(String chinese) {
// HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
// format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
// format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//
// StringBuilder pinyin = new StringBuilder();
// boolean lastIsChinese = false; // 标记上一个字符是否为汉字,用于控制空格添加
//
// for (char c : chinese.toCharArray()) {
// if (Character.toString(c).matches("[\\u4E00-\\u9FA5]")) {
// try {
// String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
// if (pinyinArray != null && pinyinArray.length > 0) {
// // 如果上一个字符是汉字且当前也是汉字,则先加空格(避免开头空格)
// if (lastIsChinese) {
// pinyin.append(' ');
// }
// pinyin.append(pinyinArray[0]);
// lastIsChinese = true;
// } else {
// // 如果没有拼音,原样追加
// pinyin.append(c);
// lastIsChinese = false;
// }
// } catch (BadHanyuPinyinOutputFormatCombination e) {
// pinyin.append(c);
// lastIsChinese = false;
// }
// } else {
// pinyin.append(c);
// lastIsChinese = false;
// }
// }
// return pinyin.toString();
// }
/** /**
* 中文转拼音辅助方法 * 中文转拼音辅助方法
* @param chinese * @param chinese
...@@ -293,21 +334,27 @@ public class ApiCsfCommonServiceImpl implements ApiCsfCommonService { ...@@ -293,21 +334,27 @@ public class ApiCsfCommonServiceImpl implements ApiCsfCommonService {
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuilder pinyin = new StringBuilder(); StringBuilder pinyin = new StringBuilder();
boolean lastIsChinese = false; // 标记上一个字符是否为汉字,用于控制空格添加 boolean lastIsChinese = false;
for (char c : chinese.toCharArray()) { for (char c : chinese.toCharArray()) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]")) { if (Character.toString(c).matches("[\\u4E00-\\u9FA5]")) {
try { try {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
if (pinyinArray != null && pinyinArray.length > 0) { if (pinyinArray != null && pinyinArray.length > 0) {
// 如果上一个字符是汉字且当前也是汉字,则先加空格(避免开头空格) String py = pinyinArray[0];
// 将 ü / Ü 替换为 v / V
py = py.replace("ü", "v").replace("Ü", "V");
// 移除可能出现的冒号或其他非字母字符(仅保留字母和空格)
py = py.replaceAll("[^A-Z]", "");
if (StringUtils.isBlank(py)) {
py = String.valueOf(c);
}
if (lastIsChinese) { if (lastIsChinese) {
pinyin.append(' '); pinyin.append(' ');
} }
pinyin.append(pinyinArray[0]); pinyin.append(py);
lastIsChinese = true; lastIsChinese = true;
} else { } else {
// 如果没有拼音,原样追加
pinyin.append(c); pinyin.append(c);
lastIsChinese = false; lastIsChinese = false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment