cn.hutool.extra.pinyin.engine.tinypinyin.TinyPinyinEngine Maven / Gradle / Ivy
The newest version!
package cn.hutool.extra.pinyin.engine.tinypinyin;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinEngine;
import com.github.promeg.pinyinhelper.Pinyin;
/**
* 封装了TinyPinyin的引擎。
*
*
* TinyPinyin(https://github.com/promeG/TinyPinyin)提供者未提交Maven中央库,
* 因此使用
* https://github.com/biezhi/TinyPinyin打包的版本
*
*
*
* 引入:
*
* <dependency>
* <groupId>io.github.biezhi</groupId>
* <artifactId>TinyPinyin</artifactId>
* <version>2.0.3.RELEASE</version>
* </dependency>
*
*
* @author looly
*/
public class TinyPinyinEngine implements PinyinEngine {
/**
* 构造
*/
public TinyPinyinEngine(){
this(null);
}
/**
* 构造
* @param config 配置
*/
public TinyPinyinEngine(Pinyin.Config config){
Pinyin.init(config);
}
@Override
public String getPinyin(char c) {
if(false == Pinyin.isChinese(c)){
return String.valueOf(c);
}
return Pinyin.toPinyin(c).toLowerCase();
}
@Override
public String getPinyin(String str, String separator) {
final String pinyin = Pinyin.toPinyin(str, separator);
return StrUtil.isEmpty(pinyin) ? pinyin : pinyin.toLowerCase();
}
}