PinyinUtil增加选择是否返回声调

This commit is contained in:
choweli 2025-02-27 10:27:46 +08:00
parent f697141cab
commit 10f02bb9bc

View File

@ -86,16 +86,15 @@ public class Pinyin4jEngine implements PinyinEngine {
public String getPinyin(char c, boolean tone) { public String getPinyin(char c, boolean tone) {
String result; String result;
if(tone){ if(tone){
//增加声调
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
// 小写
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 加声调
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
//
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
try { try {
//增加声调
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
// 小写
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 加声调
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
//
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
String[] results = PinyinHelper.toHanyuPinyinStringArray(c, formatTemp); String[] results = PinyinHelper.toHanyuPinyinStringArray(c, formatTemp);
result = ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0]; result = ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
} catch (BadHanyuPinyinOutputFormatCombination e) { } catch (BadHanyuPinyinOutputFormatCombination e) {
@ -136,17 +135,8 @@ public class Pinyin4jEngine implements PinyinEngine {
@Override @Override
public String getPinyin(String str, String separator, boolean tone) { public String getPinyin(String str, String separator, boolean tone) {
final StrBuilder result = StrUtil.strBuilder();
if(tone){ if(tone){
//增加声调 final StrBuilder result = StrUtil.strBuilder();
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
// 小写
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 加声调
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
//
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
boolean isFirst = true; boolean isFirst = true;
final int strLen = str.length(); final int strLen = str.length();
try { try {
@ -156,6 +146,14 @@ public class Pinyin4jEngine implements PinyinEngine {
} else{ } else{
result.append(separator); result.append(separator);
} }
//增加声调
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
// 小写
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// 加声调
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
//
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), formatTemp); final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), formatTemp);
if(ArrayUtil.isEmpty(pinyinStringArray)){ if(ArrayUtil.isEmpty(pinyinStringArray)){
result.append(str.charAt(i)); result.append(str.charAt(i));
@ -166,10 +164,9 @@ public class Pinyin4jEngine implements PinyinEngine {
} catch (BadHanyuPinyinOutputFormatCombination e) { } catch (BadHanyuPinyinOutputFormatCombination e) {
throw new PinyinException(e); throw new PinyinException(e);
} }
return result.toString();
}else { }else {
result.append(getPinyin(str, separator)); return getPinyin(str, separator);
} }
return result.toString();
} }
} }