mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
PinyinUtil增加选择是否返回声调
This commit is contained in:
parent
bd94c09e91
commit
f697141cab
@ -21,6 +21,17 @@ public interface PinyinEngine {
|
|||||||
*/
|
*/
|
||||||
String getPinyin(char c);
|
String getPinyin(char c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果c为汉字,则返回大写拼音;如果c不是汉字,则返回String.valueOf(c)
|
||||||
|
*
|
||||||
|
* @param c 任意字符,汉字返回拼音,非汉字原样返回
|
||||||
|
* @param tone 是否返回声调
|
||||||
|
* @return 汉字返回拼音,非汉字原样返回
|
||||||
|
*/
|
||||||
|
default String getPinyin(char c, boolean tone){
|
||||||
|
return getPinyin(c);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字符串对应的完整拼音,非中文返回原字符
|
* 获取字符串对应的完整拼音,非中文返回原字符
|
||||||
*
|
*
|
||||||
@ -30,6 +41,19 @@ public interface PinyinEngine {
|
|||||||
*/
|
*/
|
||||||
String getPinyin(String str, String separator);
|
String getPinyin(String str, String separator);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取字符串对应的完整拼音,非中文返回原字符
|
||||||
|
*
|
||||||
|
* @param str 字符串
|
||||||
|
* @param separator 拼音之间的分隔符
|
||||||
|
* @param tone 是否返回声调
|
||||||
|
* @return 拼音
|
||||||
|
*/
|
||||||
|
default String getPinyin(String str, String separator,boolean tone){
|
||||||
|
return getPinyin(str, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将输入字符串转为拼音首字母,其它字符原样返回
|
* 将输入字符串转为拼音首字母,其它字符原样返回
|
||||||
*
|
*
|
||||||
|
@ -31,6 +31,17 @@ public class PinyinUtil {
|
|||||||
return getEngine().getPinyin(c);
|
return getEngine().getPinyin(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果c为汉字,则返回大写拼音;如果c不是汉字,则返回String.valueOf(c)
|
||||||
|
*
|
||||||
|
* @param c 任意字符,汉字返回拼音,非汉字原样返回
|
||||||
|
* @param tone 是否返回声调
|
||||||
|
* @return 汉字返回拼音,非汉字原样返回
|
||||||
|
*/
|
||||||
|
public static String getPinyin(final char c, boolean tone) {
|
||||||
|
return getEngine().getPinyin(c,tone);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将输入字符串转为拼音,每个字之间的拼音使用空格分隔
|
* 将输入字符串转为拼音,每个字之间的拼音使用空格分隔
|
||||||
*
|
*
|
||||||
@ -41,6 +52,17 @@ public class PinyinUtil {
|
|||||||
return getPinyin(str, StrUtil.SPACE);
|
return getPinyin(str, StrUtil.SPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将输入字符串转为拼音,每个字之间的拼音使用空格分隔
|
||||||
|
*
|
||||||
|
* @param str 任意字符,汉字返回拼音,非汉字原样返回
|
||||||
|
* @param tone 是否返回声调
|
||||||
|
* @return 汉字返回拼音,非汉字原样返回
|
||||||
|
*/
|
||||||
|
public static String getPinyin(final String str, boolean tone) {
|
||||||
|
return getPinyin(str, StrUtil.SPACE, tone);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将输入字符串转为拼音,以字符为单位插入分隔符
|
* 将输入字符串转为拼音,以字符为单位插入分隔符
|
||||||
*
|
*
|
||||||
@ -52,6 +74,18 @@ public class PinyinUtil {
|
|||||||
return getEngine().getPinyin(str, separator);
|
return getEngine().getPinyin(str, separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将输入字符串转为拼音,以字符为单位插入分隔符
|
||||||
|
*
|
||||||
|
* @param str 任意字符,汉字返回拼音,非汉字原样返回
|
||||||
|
* @param separator 每个字拼音之间的分隔符
|
||||||
|
* @param tone 是否返回声调
|
||||||
|
* @return 汉字返回拼音,非汉字原样返回
|
||||||
|
*/
|
||||||
|
public static String getPinyin(final String str, final String separator, boolean tone) {
|
||||||
|
return getEngine().getPinyin(str, separator, tone);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将输入字符串转为拼音首字母,其它字符原样返回
|
* 将输入字符串转为拼音首字母,其它字符原样返回
|
||||||
*
|
*
|
||||||
|
@ -36,8 +36,26 @@ public class Bopomofo4jEngine implements PinyinEngine {
|
|||||||
return Bopomofo4j.pinyin(String.valueOf(c), ToneType.WITHOUT_TONE, false, false, StrUtil.EMPTY);
|
return Bopomofo4j.pinyin(String.valueOf(c), ToneType.WITHOUT_TONE, false, false, StrUtil.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(char c, boolean tone) {
|
||||||
|
if (tone) {
|
||||||
|
return Bopomofo4j.pinyin(String.valueOf(c), ToneType.WITH_VOWEL_TONE, false, false, StrUtil.EMPTY);
|
||||||
|
}else{
|
||||||
|
return getPinyin(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPinyin(String str, String separator) {
|
public String getPinyin(String str, String separator) {
|
||||||
return Bopomofo4j.pinyin(str, ToneType.WITHOUT_TONE, false, false, separator);
|
return Bopomofo4j.pinyin(str, ToneType.WITHOUT_TONE, false, false, separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(String str, String separator, boolean tone) {
|
||||||
|
if (tone) {
|
||||||
|
return Bopomofo4j.pinyin(str, ToneType.WITH_VOWEL_TONE, false, false, separator);
|
||||||
|
}else{
|
||||||
|
return getPinyin(str, separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,10 +65,28 @@ public class HoubbPinyinEngine implements PinyinEngine {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(char c, boolean tone) {
|
||||||
|
if (tone){
|
||||||
|
return PinyinHelper.toPinyin(String.valueOf(c), PinyinStyleEnum.DEFAULT);
|
||||||
|
}else {
|
||||||
|
return getPinyin(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPinyin(String str, String separator) {
|
public String getPinyin(String str, String separator) {
|
||||||
String result;
|
String result;
|
||||||
result = PinyinHelper.toPinyin(str, format, separator);
|
result = PinyinHelper.toPinyin(str, format, separator);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(String str, String separator, boolean tone) {
|
||||||
|
if(tone){
|
||||||
|
return PinyinHelper.toPinyin(str, PinyinStyleEnum.DEFAULT, separator);
|
||||||
|
}else {
|
||||||
|
return getPinyin(str,separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,16 @@ public class JPinyinEngine implements PinyinEngine {
|
|||||||
return ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
return ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(char c, boolean tone) {
|
||||||
|
if (tone) {
|
||||||
|
String[] results = PinyinHelper.convertToPinyinArray(c, PinyinFormat.WITH_TONE_MARK);
|
||||||
|
return ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
||||||
|
}else {
|
||||||
|
return getPinyin(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPinyin(String str, String separator) {
|
public String getPinyin(String str, String separator) {
|
||||||
try {
|
try {
|
||||||
@ -61,4 +71,19 @@ public class JPinyinEngine implements PinyinEngine {
|
|||||||
throw new cn.hutool.extra.pinyin.PinyinException(e);
|
throw new cn.hutool.extra.pinyin.PinyinException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(String str, String separator, boolean tone) {
|
||||||
|
if (tone) {
|
||||||
|
try {
|
||||||
|
return PinyinHelper.convertToPinyinString(str, separator, PinyinFormat.WITH_TONE_MARK);
|
||||||
|
} catch (PinyinException e) {
|
||||||
|
throw new cn.hutool.extra.pinyin.PinyinException(e);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
return getPinyin(str, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -82,6 +82,32 @@ public class Pinyin4jEngine implements PinyinEngine {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(char c, boolean tone) {
|
||||||
|
String result;
|
||||||
|
if(tone){
|
||||||
|
//增加声调
|
||||||
|
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
|
||||||
|
// 小写
|
||||||
|
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||||
|
// 加声调
|
||||||
|
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
|
||||||
|
//
|
||||||
|
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String[] results = PinyinHelper.toHanyuPinyinStringArray(c, formatTemp);
|
||||||
|
result = ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
||||||
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||||
|
result = String.valueOf(c);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
result = getPinyin(c);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPinyin(String str, String separator) {
|
public String getPinyin(String str, String separator) {
|
||||||
final StrBuilder result = StrUtil.strBuilder();
|
final StrBuilder result = StrUtil.strBuilder();
|
||||||
@ -107,4 +133,43 @@ public class Pinyin4jEngine implements PinyinEngine {
|
|||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPinyin(String str, String separator, boolean tone) {
|
||||||
|
final StrBuilder result = StrUtil.strBuilder();
|
||||||
|
if(tone){
|
||||||
|
//增加声调
|
||||||
|
HanyuPinyinOutputFormat formatTemp = new HanyuPinyinOutputFormat();
|
||||||
|
// 小写
|
||||||
|
formatTemp.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||||
|
// 加声调
|
||||||
|
formatTemp.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
|
||||||
|
//
|
||||||
|
formatTemp.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
|
||||||
|
|
||||||
|
boolean isFirst = true;
|
||||||
|
final int strLen = str.length();
|
||||||
|
try {
|
||||||
|
for(int i = 0; i < strLen; i++){
|
||||||
|
if(isFirst){
|
||||||
|
isFirst = false;
|
||||||
|
} else{
|
||||||
|
result.append(separator);
|
||||||
|
}
|
||||||
|
final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), formatTemp);
|
||||||
|
if(ArrayUtil.isEmpty(pinyinStringArray)){
|
||||||
|
result.append(str.charAt(i));
|
||||||
|
} else{
|
||||||
|
result.append(pinyinStringArray[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||||
|
throw new PinyinException(e);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
result.append(getPinyin(str, separator));
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user