mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-03 16:24:31 +08:00
增加是否音标参数
This commit is contained in:
parent
636df4b911
commit
5fa572c6ae
@ -36,7 +36,18 @@ public interface PinyinEngine {
|
||||
* @param c 任意字符,汉字返回拼音,非汉字原样返回
|
||||
* @return 汉字返回拼音,非汉字原样返回
|
||||
*/
|
||||
String getPinyin(char c);
|
||||
default String getPinyin(final char c){
|
||||
return getPinyin(c, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* 如果c为汉字,则返回大写拼音;如果c不是汉字,则返回String.valueOf(c)
|
||||
*
|
||||
* @param c 任意字符,汉字返回拼音,非汉字原样返回
|
||||
* @param tone 是否返回声调
|
||||
* @return 汉字返回拼音,非汉字原样返回
|
||||
*/
|
||||
String getPinyin(char c, boolean tone);
|
||||
|
||||
/**
|
||||
* 获取字符串对应的完整拼音,非中文返回原字符
|
||||
@ -45,7 +56,19 @@ public interface PinyinEngine {
|
||||
* @param separator 拼音之间的分隔符
|
||||
* @return 拼音
|
||||
*/
|
||||
String getPinyin(String str, String separator);
|
||||
default String getPinyin(final String str, final String separator){
|
||||
return getPinyin(str, separator, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字符串对应的完整拼音,非中文返回原字符
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param separator 拼音之间的分隔符
|
||||
* @param tone 是否返回声调
|
||||
* @return 拼音
|
||||
*/
|
||||
String getPinyin(String str, String separator, boolean tone);
|
||||
|
||||
/**
|
||||
* 将输入字符串转为拼音首字母,其它字符原样返回
|
||||
|
@ -51,12 +51,12 @@ public class Bopomofo4jEngine implements PinyinEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final char c) {
|
||||
return Bopomofo4j.pinyin(String.valueOf(c), ToneType.WITHOUT_TONE, false, false, StrUtil.EMPTY);
|
||||
public String getPinyin(final char c, final boolean tone) {
|
||||
return Bopomofo4j.pinyin(String.valueOf(c), tone ? ToneType.WITH_VOWEL_TONE : ToneType.WITHOUT_TONE, false, false, StrUtil.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final String str, final String separator) {
|
||||
return Bopomofo4j.pinyin(str, ToneType.WITHOUT_TONE, false, false, separator);
|
||||
public String getPinyin(final String str, final String separator, final boolean tone) {
|
||||
return Bopomofo4j.pinyin(str, tone ? ToneType.WITH_VOWEL_TONE : ToneType.WITHOUT_TONE, false, false, separator);
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.extra.pinyin.engine.houbb;
|
||||
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
|
||||
import com.github.houbb.pinyin.constant.enums.PinyinStyleEnum;
|
||||
import com.github.houbb.pinyin.util.PinyinHelper;
|
||||
@ -41,48 +42,25 @@ import com.github.houbb.pinyin.util.PinyinHelper;
|
||||
*/
|
||||
public class HoubbEngine implements PinyinEngine {
|
||||
|
||||
// 汉字拼音输出的格式
|
||||
private PinyinStyleEnum format;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public HoubbEngine() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param format 格式
|
||||
*/
|
||||
public HoubbEngine(final PinyinStyleEnum format) {
|
||||
init(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param format 格式
|
||||
*/
|
||||
public void init(PinyinStyleEnum format) {
|
||||
if (null == format) {
|
||||
format = PinyinStyleEnum.NORMAL;
|
||||
}
|
||||
this.format = format;
|
||||
// SPI方式加载时检查库是否引入
|
||||
Assert.notNull(PinyinHelper.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final char c) {
|
||||
public String getPinyin(final char c, final boolean tone) {
|
||||
final String result;
|
||||
result = PinyinHelper.toPinyin(String.valueOf(c), format);
|
||||
result = PinyinHelper.toPinyin(String.valueOf(c), tone ? PinyinStyleEnum.DEFAULT : PinyinStyleEnum.NORMAL);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final String str, final String separator) {
|
||||
public String getPinyin(final String str, final String separator, final boolean tone) {
|
||||
final String result;
|
||||
result = PinyinHelper.toPinyin(str, format, separator);
|
||||
result = PinyinHelper.toPinyin(str, tone ? PinyinStyleEnum.DEFAULT : PinyinStyleEnum.NORMAL, separator);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -16,11 +16,12 @@
|
||||
|
||||
package org.dromara.hutool.extra.pinyin.engine.jpinyin;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
|
||||
import com.github.stuxuhai.jpinyin.PinyinException;
|
||||
import com.github.stuxuhai.jpinyin.PinyinFormat;
|
||||
import com.github.stuxuhai.jpinyin.PinyinHelper;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
|
||||
|
||||
/**
|
||||
* 封装了Jpinyin的引擎。
|
||||
@ -43,49 +44,24 @@ import com.github.stuxuhai.jpinyin.PinyinHelper;
|
||||
*/
|
||||
public class JPinyinEngine implements PinyinEngine {
|
||||
|
||||
//设置汉子拼音输出的格式
|
||||
private PinyinFormat format;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public JPinyinEngine() {
|
||||
this(null);
|
||||
// SPI方式加载时检查库是否引入
|
||||
Assert.notNull(PinyinHelper.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param format {@link PinyinFormat}
|
||||
*/
|
||||
public JPinyinEngine(final PinyinFormat format) {
|
||||
init(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化格式
|
||||
*
|
||||
* @param format 格式{@link PinyinFormat}
|
||||
*/
|
||||
public void init(PinyinFormat format) {
|
||||
if (null == format) {
|
||||
// 不加声调
|
||||
format = PinyinFormat.WITHOUT_TONE;
|
||||
}
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getPinyin(final char c) {
|
||||
final String[] results = PinyinHelper.convertToPinyinArray(c, format);
|
||||
public String getPinyin(final char c, final boolean tone) {
|
||||
final String[] results = PinyinHelper.convertToPinyinArray(c, tone ? PinyinFormat.WITH_TONE_MARK : PinyinFormat.WITHOUT_TONE);
|
||||
return ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final String str, final String separator) {
|
||||
public String getPinyin(final String str, final String separator, final boolean tone) {
|
||||
try {
|
||||
return PinyinHelper.convertToPinyinString(str, separator, format);
|
||||
return PinyinHelper.convertToPinyinString(str, separator, tone ? PinyinFormat.WITH_TONE_MARK : PinyinFormat.WITHOUT_TONE);
|
||||
} catch (final PinyinException e) {
|
||||
throw new org.dromara.hutool.extra.pinyin.PinyinException(e);
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.extra.pinyin.engine.pinyin4j;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.extra.pinyin.engine.PinyinEngine;
|
||||
import org.dromara.hutool.extra.pinyin.PinyinException;
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
@ -47,48 +48,37 @@ import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombi
|
||||
*/
|
||||
public class Pinyin4jEngine implements PinyinEngine {
|
||||
|
||||
//设置汉子拼音输出的格式
|
||||
private HanyuPinyinOutputFormat format;
|
||||
private static final HanyuPinyinOutputFormat WITH_TONE_MARK;
|
||||
private static final HanyuPinyinOutputFormat WITHOUT_TONE;
|
||||
static {
|
||||
WITH_TONE_MARK = new HanyuPinyinOutputFormat();
|
||||
// 小写
|
||||
WITH_TONE_MARK.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
// 'ü' 使用 "v" 代替
|
||||
WITH_TONE_MARK.setVCharType(HanyuPinyinVCharType.WITH_V);
|
||||
WITH_TONE_MARK.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
|
||||
|
||||
WITHOUT_TONE = new HanyuPinyinOutputFormat();
|
||||
// 小写
|
||||
WITHOUT_TONE.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
// 'ü' 使用 "v" 代替
|
||||
WITHOUT_TONE.setVCharType(HanyuPinyinVCharType.WITH_V);
|
||||
WITHOUT_TONE.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public Pinyin4jEngine() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param format 格式
|
||||
*/
|
||||
public Pinyin4jEngine(final HanyuPinyinOutputFormat format) {
|
||||
init(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param format 格式
|
||||
*/
|
||||
public void init(HanyuPinyinOutputFormat format) {
|
||||
if (null == format) {
|
||||
format = new HanyuPinyinOutputFormat();
|
||||
// 小写
|
||||
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
// 不加声调
|
||||
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
// 'ü' 使用 "v" 代替
|
||||
format.setVCharType(HanyuPinyinVCharType.WITH_V);
|
||||
}
|
||||
this.format = format;
|
||||
// SPI方式加载时检查库是否引入
|
||||
Assert.notNull(PinyinHelper.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final char c) {
|
||||
public String getPinyin(final char c, final boolean tone) {
|
||||
String result;
|
||||
try {
|
||||
final String[] results = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
||||
final String[] results = PinyinHelper.toHanyuPinyinStringArray(c, tone ? WITH_TONE_MARK : WITHOUT_TONE);
|
||||
result = ArrayUtil.isEmpty(results) ? String.valueOf(c) : results[0];
|
||||
} catch (final BadHanyuPinyinOutputFormatCombination e) {
|
||||
result = String.valueOf(c);
|
||||
@ -97,7 +87,7 @@ public class Pinyin4jEngine implements PinyinEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final String str, final String separator) {
|
||||
public String getPinyin(final String str, final String separator, final boolean tone) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
final int strLen = str.length();
|
||||
@ -108,7 +98,7 @@ public class Pinyin4jEngine implements PinyinEngine {
|
||||
} else{
|
||||
result.append(separator);
|
||||
}
|
||||
final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), format);
|
||||
final String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), tone ? WITH_TONE_MARK : WITHOUT_TONE);
|
||||
if(ArrayUtil.isEmpty(pinyinStringArray)){
|
||||
result.append(str.charAt(i));
|
||||
} else{
|
||||
|
@ -60,7 +60,7 @@ public class TinyPinyinEngine implements PinyinEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final char c) {
|
||||
public String getPinyin(final char c, final boolean tone) {
|
||||
if (!Pinyin.isChinese(c)) {
|
||||
return String.valueOf(c);
|
||||
}
|
||||
@ -68,7 +68,7 @@ public class TinyPinyinEngine implements PinyinEngine {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPinyin(final String str, final String separator) {
|
||||
public String getPinyin(final String str, final String separator, final boolean tone) {
|
||||
final String pinyin = Pinyin.toPinyin(str, separator);
|
||||
return StrUtil.isEmpty(pinyin) ? pinyin : pinyin.toLowerCase();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user