java汉字转拼音工具类分享
本文实例为大家分享了java汉字转拼音工具类的具体代码,供大家参考,具体内容如下
importcom.google.common.base.Strings;
importnet.sourceforge.pinyin4j.PinyinHelper;
importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;
importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Locale;
importjava.util.TreeSet;
publicclassPinyinUtils{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(PinyinUtils.class);
/**
*单字解析
*
*@paramstrfirst
*@return
*/
publicstaticString[]convert(Stringstr){
String[]reslut=null;
HanyuPinyinOutputFormathanyuPinyinOutputFormat=newHanyuPinyinOutputFormat();
hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try{
reslut=PinyinHelper.toHanyuPinyinStringArray(str.charAt(0),hanyuPinyinOutputFormat);
TreeSetstringTreeSet=newTreeSet<>();
for(inti=0;i=3){
break;
}
stringTreeSet.add(reslut[i].replace("u:","v"));
}
reslut=newString[stringTreeSet.size()];
reslut=stringTreeSet.toArray(reslut);
}catch(BadHanyuPinyinOutputFormatCombinationbadHanyuPinyinOutputFormatCombination){
badHanyuPinyinOutputFormatCombination.printStackTrace();
}catch(Exceptione){
logger.error("[convert]:",e);
}
returnreslut;
}
/**
*词组解析(全写)
*
*@paramchs
*@return
*/
publicstaticStringgetSelling(Stringchs){
returntranslate(chs,false);
}
/**
*汉字转拼音
*
*@paramchs
*@paramacronym
*@return
*/
privatestaticStringtranslate(Stringchs,booleanacronym){
StringBufferbuffer=newStringBuffer();
if(Strings.isNullOrEmpty(chs))
return"";
try{
List>temps=newArrayList<>();
intlen=chs.length();
intlen1=0;
for(inti=0;istringList=newArrayList<>();
Stringkey=chs.charAt(i)+"";
if(key.getBytes().length>=2){
String[]temp=convert(key);
if(temp.length==0){
continue;
}
if(temp==null){
stringList.add("");
}else{
for(Stringv:temp){
stringList.add(v);
}
}
}else{
stringList.add(key);
}
temps.add(stringList);
len1++;
}
List>t=newArrayList<>();
for(inti=0;icurrentList=newArrayList<>();
ListstringList=temps.get(i);
if(stringList!=null){
for(Strings:stringList){
if(acronym){
s=s.charAt(0)+"";
}
if(i>0){
ListpreList=t.get(i-1);
if(preList!=null){
for(Strings1:preList){
currentList.add(s1+s);
}
}
}else{
currentList.add(s);
}
}
}
t.add(i,currentList);
}
if(t.size()>0){
ListcurrentList=t.get(t.size()-1);
if(currentList!=null){
for(Stringcurrent:currentList){
buffer.append(current);
buffer.append("");
}
}
}
returnbuffer.toString();
}catch(Exceptione){
logger.error("[getSortLetters]:",e);
return"";
}
}
/**
*词组解析(缩写)
*
*@paramchs
*@return
*/
publicstaticStringgetSmallSelling(Stringchs){
returntranslate(chs,true);
}
/**
*获取首字母
*
*@return
*/
publicstaticStringgetSortLetters(Stringpingyin){
try{
StringsortString=pingyin.substring(0,1).toUpperCase(Locale.getDefault());
//正则表达式,判断首字母是否是英文字母
if(sortString.matches("[A-Z]")){
returnsortString.toUpperCase(Locale.getDefault());
}
}catch(Exceptione){
logger.error("[getSortLetters]:",e);
}
return"#";
}
publicstaticvoidmain(String[]args){
PinyinUtilsp=newPinyinUtils();
System.out.println(p.getSelling("单个"));
System.out.println(p.getSmallSelling("测试"));
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。