java汉字转拼音工具类分享

网友投稿 384 2023-02-21


java汉字转拼音工具类分享

本文实例为大家分享了java汉字转拼音工具类的具体代码,供大家参考,具体内容如下

import com.google.common.base.Strings;

import net.sourceforge.pinyin4j.PinyinHelper;

import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;

import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.ArrayList;

import java.util.List;

import java.util.Locale;

import java.util.TreeSet;

public class PinyinUtils {

private static final Logger logger = LoggerFactory.getLogger(PinyinUtils.class);

/**

* 单字解析

*

* @param str first

* @return

*/

public static String[] convert(String str) {

String[] reslut = null;

HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();

hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

try {

reslut = PinyinHelper.toHanyuPinyinStringArray(str.charAt(0), hanyuPinyinOutputFormat);

TreeSet stringTreeSet = new TreeSet<>();

for (int i = 0; i < reslut.length; i++) {

if(reslut.length >=3) {

break;

}

stringTreeSet.add(reslut[i].replace("u:","v"));

}

reslut = new String[stringTreeSet.size()];

reslut = stringTreeSet.toArray(reslut);

} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {

badHanyuPinyinOutputFormatCombination.printStackTrace();

} catch (Exception e) {

logger.error("[convert]: ", e);

}

return reslut;

}

/**

* 词组解析(全写)

*

* @param chs

* @return

*/

public static String getSelling(String chs) {

return translate(chs, false);

}

/**

* 汉字转拼音

*

* @param chs

* @param acronym

* @return

*/

private static String translate(String chs, boolean acronym) {

StringBuffer buffer=new StringBuffer();

if (Strings.isNullOrEmpty(chs))

return "";

try {

List> temps = new ArrayList<>();

int len = chs.length();

int len1 = 0;

for (int i = 0; i < len; i++) {

List stringList = new ArrayList<>();

String key = chs.charAt(i) + "";

if (key.getBytes().length >= 2) {

String[] temp = convert(key);

if(temp.length == 0) {

continue;

}

if (temp == null) {

stringList.add("");

} else {

for (String v : temp) {

stringList.add(v);

}

}

} else {

stringList.add(key);

}

temps.add(stringList);

len1++;

}

List> t = new ArrayList<>();

for (int i = 0; i < len1; i++) {

List currentList = new ArrayList<>();

List stringList = temps.get(i);

if (stringList != null) {

for (String s : stringList) {

if (acronym) {

s = s.charAt(0) + "";

}

if (i > 0) {

List preList = t.get(i - 1);

if (preList != null) {

for (String s1 : preList) {

currentList.add(s1 + s);

}

}

}else{

currentList.add(s);

}

}

}

t.add(i, currentList);

}

if (t.size()>0){

List currentList= t.get(t.size()-1);

if (currentList!=null){

for(String current : currentList){

buffer.append(current);

buffer.append("");

}

}

}

return buffer.toString();

} catch (Exception e) {

logger.error("[getSortLetters]: ", e);

return "";

}

}

/**

* 词组解析(缩写)

*

* @param chs

* @return

*/

public static String getSmallSelling(String chs) {

return translate(chs, true);

}

/**

* 获取首字母

*

* @return

*/

public static String getSortLetters(String pingyin) {

try {

String sortString = pingyin.substring(0, 1).toUpperCase(Locale.getDefault());

// 正则表达式,判断首字母是否是英文字母

if (sortString.matches("[A-Z]")) {

return sortString.toUpperCase(Locale.ghttp://etDefault());

}

} catch (Exception e) {

logger.error("[getSortLetters]: ", e);

}

return "#";

}

public static void main(String [] args) {

PinyinUtils p = new PinyinUtils();

System.out.println(p.getSelling("单个"));

System.out.println(p.getSmallSelling("测试"));

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:包含pthyon接口测试实例的词条
下一篇:使用IntelliJ IDEA 进行代码对比的方法(两种方法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~