package javatest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class ChangeToPinYinDuoYin {
private static Pattern LETTER_PATTERN = Pattern.compile("([a-z]|[A-Z])*");
/**
* 初始化多音字匹配条件数组
*/
private static Map<String, List<String>> pinyinMap = new HashMap<String, List<String>>();
/**
* 初始化多音字数组 姓氏
*/
private static final List<String> AREA_CODE_LIST;
/**
* pinyin4j格式类
*/
private HanyuPinyinOutputFormat format = null;
/**
* 拼音字符串数组
*/
private String[] pinyin;
/**
* 初始化复制多音字姓氏数据
*/
static {
AREA_CODE_LIST = Arrays.asList("wang#王", "piao#朴", "zeng#曾", "shan#单", "ye#叶", "qiu#仇", "xie#解", "xian#冼");
}
/**
* 通过构造方法进行初始化
*/
public ChangeToPinYinDuoYin() {
format = new HanyuPinyinOutputFormat();
/*
* 设置需要转换的拼音格式 以天为例 HanyuPinyinToneType.WITHOUT_TONE 转换为tian
* HanyuPinyinToneType.WITH_TONE_MARK 转换为tian1
* HanyuPinyinVCharType.WITH_U_UNICODE 转换为tiān
*
*/
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (String areaCode : AREA_CODE_LIST) {
String[] arr = areaCode.split("#");
String pinyinS = arr[0];
String chinese = arr[1];
if (chinese != null) {
String[] strs = chinese.split(" ");
List<String> list = Arrays.asList(strs);
pinyinMap.put(pinyinS, list);
}
}
pinyin = null;
}
/**
* 对单个字进行转换
*
* @param pinYinStr 需转换的汉字字符串
* @return 拼音字符串数组
*/
public String getCharPinYin(String pinYinStr) {
StringBuffer pinyinD = new StringBuffer();
String result = "#";
char ch = pinYinStr.charAt(0);
// 取得当前汉字的所有全拼
try {
pinyin = PinyinHelper.toHanyuPinyinStringArray(ch, format);
if (pinyin == null) { // 非中文
return result;
} else {
int len = pinyin.length;
if (len == 1) { // 不是多音字
result = pinyin[0];
} else if (pinyin[0].equals(pinyin[1])) { // 非多音字 有多个音,取第一个
result = pinyin[0];
} else { // 多音字
boolean flag = false;
String s = null;
List<String> keyList = null;
for (int x = 0; x < len; x++) {
String py = pinyin[x];
if (py.contains("u:")) { // 过滤 u:
py = py.replace("u:", "v");
System.out.println("filter u:" + py);
}
keyList = pinyinMap.get(py);
}
if (!flag) { // 都没有找到,匹配默认的
s = String.valueOf(ch);
for (int x = 0; x < len; x++) {
String py = pinyin[x];
if (py.contains("u:")) { // 过滤 u:
py = py.replace("u:", "v");
System.out.println("filter u:");
}
keyList = pinyinMap.get(py);
if (keyList != null && (keyList.contains(s))) {
System.out.println("default = " + py);
// pinyin.append(results[x]); //如果不需要拼音首字母大写 ,直接返回即可
pinyinD.append(py);
break;
}
}
if(pinyinD.toString().length()!=0){
try {
result = String.valueOf(pinyinD.toString());
} catch (Exception e) {
return result;
}
}else{
try{
//多音字会返回一个多音字拼音的数组,pinyiin4j并不能有效判断该字的读音
result = String.valueOf(pinyin[0]);
}catch (Exception e){
return result;
}
}
}
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
return result;
}
return result;
}
/**
* 返回全拼
* @param pinYinStr
* @return
*/
public String getCharPinYinName(char pinYinStr) {
String result = "#";
try {
//执行转换
pinyin = PinyinHelper.toHanyuPinyinStringArray(pinYinStr, format);
} catch (BadHanyuPinyinOutputFormatCombination e) {
return result;
}
StringBuffer pinyinD = new StringBuffer();
char ch = pinYinStr;
//pinyin4j规则,当转换的符串不是汉字,就返回null
if (pinyin == null) {
return result;
}else{
int len = pinyin.length;
if (len == 1) { // 不是多音字
result = pinyin[0];
}else if(pinyin[0].equals(pinyin[1])){ //非多音字 有多个音,取第一个
result = pinyin[0];
}else { // 多音字
boolean flag = false;
String s = null;
List<String> keyList =null;
for (int x = 0; x < len; x++) {
String py = pinyin[x];
if(py.contains("u:")){ //过滤 u:
py = py.replace("u:", "v");
}
keyList = pinyinMap.get(py);
}
if (!flag) { //都没有找到,匹配默认的
s = String.valueOf(ch);
for (int x = 0; x < len; x++) {
String py = pinyin[x];
if(py.contains("u:")){ //过滤 u:
py = py.replace("u:", "v");
}
keyList = pinyinMap.get(py);
if (keyList != null && (keyList.contains(s))) {
pinyinD.append(py);
break;
}
}
if(pinyinD.toString().length()!=0){
try {
result = pinyinD.toString();
} catch (Exception e) {
return result;
}
}else{
try{
//多音字会返回一个多音字拼音的数组,pinyiin4j并不能有效判断该字的读音
result = pinyin[0];
}catch (Exception e){
return result;
}
}
}
}
}
return result;
}
public String getStringPinYinAll(String pinYinStr) {
StringBuffer sb = new StringBuffer();
String tempStr = null;
//循环字符串
for (int i = 0; i < pinYinStr.length(); i++) {
tempStr = this.getCharPinYinName(pinYinStr.charAt(i));
if (tempStr == null) {
//非汉字直接拼接
sb.append(pinYinStr.charAt(i));
} else {
sb.append(tempStr);
}
}
return sb.toString();
}
/**
* 对单个字进行转换
*
* @param pinYinStr
* @return
*/
public String getStringPinYin(String pinYinStr) {
StringBuffer sb = new StringBuffer();
String tempStr = null;
//循环字符串
for (int i = 0; i < pinYinStr.length(); i++) {
tempStr = this.getCharPinYin(String.valueOf(pinYinStr.charAt(i)));
if (tempStr == null) {
//非汉字直接拼接
sb.append(pinYinStr.charAt(i));
} else {
sb.append(tempStr);
}
}
return sb.toString();
}
/**
* 判断是否是字母
*
* @param tap
* @return
*/
public Boolean isAbc(String tap) {
return LETTER_PATTERN.matcher(tap).matches();
}
}
对姓氏多音字进行转换
最新推荐文章于 2023-12-22 19:43:11 发布