pinyin4j是一个可以讲汉字转换为全拼的开源工具类。
具体下载地址可以去官网下载 http://pinyin4j.sourceforge.net/
本篇文章使用的是 pinyin4j-2.5.0.jar
pinyin4j的使用就是调用PinyinHelper帮助类,这边我们把PinyinHelper的方法都使用一遍
String[] hanzi = PinyinHelper.toHanyuPinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toGwoyeuRomatzyhStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toMPS2PinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toTongyongPinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toWadeGilesPinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toYalePinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
结果如下
//toHanyuPinyinStringArray
E/TAG: 汉字读音如下
E/TAG: dan1
E/TAG: chan2
E/TAG: shan4
//toGwoyeuRomatzyhStringArray
E/TAG: 汉字读音如下
E/TAG: dan
E/TAG: charn
E/TAG: shann
//toMPS2PinyinStringArray
E/TAG: 汉字读音如下
E/TAG: dan1
E/TAG: chan2
E/TAG: shan4
//toTongyongPinyinStringArray
E/TAG: 汉字读音如下
E/TAG: dan1
E/TAG: chan2
E/TAG: shan4
//toWadeGilesPinyinStringArray
E/TAG: 汉字读音如下
E/TAG: tan1
E/TAG: ch`an2
E/TAG: shan4
//toYalePinyinStringArray
E/TAG: 汉字读音如下
E/TAG: dan1
E/TAG: chan2
E/TAG: shan4
String[] hanzi = PinyinHelper.toHanyuPinyinStringArray('单');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
hanzi = PinyinHelper.toHanyuPinyinStringArray('d');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
log如下
E/TAG: 汉字读音如下
E/TAG: dan1
E/TAG: chan2
E/TAG: shan4
E/TAG: 当前不是汉字
我们可以看到当有多音字的时候就会打印多个结果,当不是汉字的时候会返回null
上面我们使用了默认的方式,这边之后我们可以加入format来设置,其实上面的多个PinyinHelper方法其实就是修给了format的自己设置
常用设置如下
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//全部小写
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不标声调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//u:转为v
format.setVCharType(HanyuPinyinVCharType.WITH_V);
调用如下代码
String[] hanzi = PinyinHelper.toHanyuPinyinStringArray('绿');
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//全部小写
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不标声调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//u:转为v
format.setVCharType(HanyuPinyinVCharType.WITH_V);
try {
hanzi = PinyinHelper.toHanyuPinyinStringArray('绿',format);
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
if (hanzi == null){
Log.e("TAG", "当前不是汉字");
}else{
Log.e("TAG", "汉字读音如下");
for (String s : hanzi) {
Log.e("TAG", s);
}
}
log如下
E/TAG: 汉字读音如下
E/TAG: lu:4
E/TAG: lu4
E/TAG: 汉字读音如下
E/TAG: lv
E/TAG: lu
经过上面的一些使用我们大体上就可以很好的使用了,下面我写了一个工具类用来实现一些基础的功能
使用如下
List Hanzi = new ArrayList<>();
Hanzi.add("绿色");
Hanzi.add("重庆");
Hanzi.add("超重");
for (String s : Hanzi) {
Log.e("TAG", PinyinUtils.getPinYin(s));
Log.e("TAG", PinyinUtils.getPinYin(s,true));
Log.e("TAG", PinyinUtils.getPinYin(s, new PinyinUtils.TongYinListener() {
@Override
public String onTongYinListener(char c, String[] ty, int index) {
if (index == 0 && c == '重') {
return ty[1];
}
return ty[0];
}
}));
}
结果
E/TAG: lvse
E/TAG: LvSe
E/TAG: lvse
E/TAG: zhongqing
E/TAG: ZhongQing
E/TAG: chongqing
E/TAG: chaozhong
E/TAG: ChaoZhong
E/TAG: chaozhong
具体代码
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* PinyinUtils
* Author: gjn.
* Time: 2018/2/28.
*/
public class PinyinUtils {
public static String getPinYin(String str) {
return getPinYin(str, false);
}
public static String getPinYin(String str, boolean isUpperCase) {
return getPinYin(str, isUpperCase, defualtListener());
}
public static String getPinYin(String str, TongYinListener listener) {
return getPinYin(str, false, listener);
}
public static String getPinYin(String str, boolean isUpperCase, TongYinListener listener) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//全部小写
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不标声调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//u:转为v
format.setVCharType(HanyuPinyinVCharType.WITH_V);
char[] c = str.toCharArray();
String result = "";
for (int i = 0; i < c.length; i++) {
//非ASCII码
if (c[i] > 128) {
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c[i], format);
//非中文
if (temp == null) {
result += c[i];
} else {
//是否首字母大写
if (isUpperCase) {
result += FirstUpperCase(listener.onTongYinListener(c[i], temp, i));
} else {
result += listener.onTongYinListener(c[i], temp, i);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
} else {
result += c[i];
}
}
if (result.isEmpty()) {
return str;
}
return result;
}
private static String FirstUpperCase(String str) {
if (str.isEmpty()) {
return str;
}
char first = str.charAt(0);
return String.valueOf(first).toUpperCase().concat(str.substring(1));
}
public static String getPinYinFrist(String str) {
return getPinYinFrist(str, defualtListener());
}
public static String getPinYinFrist(String str, TongYinListener listener) {
if (str.isEmpty()) {
return str;
}
String result = getPinYin(str, listener);
if (result.length() > 1) {
result = result.substring(0, 1);
}
return result;
}
public static TongYinListener defualtListener() {
return new TongYinListener() {
@Override
public String onTongYinListener(char c, String[] ty, int index) {
return ty[0];
}
};
}
public interface TongYinListener {
String onTongYinListener(char c, String[] ty, int index);
}
}