欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java中文转拼音工具类详解

时间:2020-10-14 10:05:10|栏目:JAVA代码|点击:

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

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PinyinUtil {
 public static String getPinyin(String paramString) {
 return getPinyinZh_CN(convertStringByChinese(paramString));
 }

 public static String getPinyinToUpperCase(String paramString) {
 return getPinyinZh_CN(convertStringByChinese(paramString)).toUpperCase();
 }

 public static String getPinyinToLowerCase(String paramString) {
 return getPinyinZh_CN(convertStringByChinese(paramString)).toLowerCase();
 }

 public static String getPinyinFirstToUpperCase(String paramString) {
 return getPinyin(paramString);
 }

 private static HanyuPinyinOutputFormat getDefaultFormat() {
 HanyuPinyinOutputFormat localHanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
 localHanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 localHanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 localHanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
 return localHanyuPinyinOutputFormat;
 }

 private static Set<String> convertStringByChinese(String paramString) {
 char[] arrayOfChar1 = paramString.toCharArray();
 if ((paramString != null) && (!paramString.trim().equalsIgnoreCase(""))) {
 char[] arrayOfChar2 = paramString.toCharArray();
 String[][] arrayOfString = new String[paramString.length()][];
 for (int i = 0; i < arrayOfChar2.length; i++) {
 char c = arrayOfChar2[i];
 if ((String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) || (String.valueOf(c).matches("[\\u3007]"))) {
  try {
  arrayOfString[i] = PinyinHelper.toHanyuPinyinStringArray(arrayOfChar1[i], getDefaultFormat());
  } catch (BadHanyuPinyinOutputFormatCombination localBadHanyuPinyinOutputFormatCombination) {
  localBadHanyuPinyinOutputFormatCombination.printStackTrace();
  }
 } else {
  arrayOfString[i] = new String[] { String.valueOf(arrayOfChar2[i]) };
 }
 }
 String[] arrayOfString1 = exchange(arrayOfString);
 HashSet localHashSet = new HashSet();
 for (int j = 0; j < arrayOfString1.length; j++) {
 localHashSet.add(arrayOfString1[j]);
 }
 return localHashSet;
 }
 return null;
 }

 private static String[] exchange(String[][] paramArrayOfString) {
 String[][] arrayOfString = doExchange(paramArrayOfString);
 return arrayOfString[0];
 }

 private static String[][] doExchange(String[][] paramArrayOfString) {
 int i = paramArrayOfString.length;
 if (i >= 2) {
 int j = paramArrayOfString[0].length;
 int k = paramArrayOfString[1].length;
 int m = j * k;
 String[] arrayOfString = new String[m];
 int n = 0;
 for (int i1 = 0; i1 < j; i1++) {
 for (int i2 = 0; i2 < k; i2++) {
  arrayOfString[n] = (capitalize(paramArrayOfString[0][i1]) + capitalize(paramArrayOfString[1][i2]));
  n++;
 }
 }
 String[][] arrayOfString1 = new String[i - 1][];
 for (int i2 = 2; i2 < i; i2++) {
 arrayOfString1[(i2 - 1)] = paramArrayOfString[i2];
 }
 arrayOfString1[0] = arrayOfString;
 return doExchange(arrayOfString1);
 }
 return paramArrayOfString;
 }

 private static String capitalize(String paramString) {
 char[] arrayOfChar = paramString.toCharArray();
 if ((arrayOfChar != null) && (arrayOfChar.length > 0) && (arrayOfChar[0] >= 'a') && (arrayOfChar[0] <= 'z')) {
 arrayOfChar[0] = ((char) (arrayOfChar[0] - ' '));
 }
 return new String(arrayOfChar);
 }

 private static String getPinyinZh_CN(Set<String> paramSet) {
 StringBuilder localStringBuilder = new StringBuilder();
 int i = 0;
 Iterator localIterator = paramSet.iterator();
 while (localIterator.hasNext()) {
 String str = (String) localIterator.next();
 if (i == paramSet.size() - 1) {
 localStringBuilder.append(str);
 } else {
 localStringBuilder.append(str + ",");
 }
 i++;
 }
 return localStringBuilder.toString();
 }

 public static String getPinYinHeadChar(String paramString) {
 StringBuffer localStringBuffer = new StringBuffer();
 if ((paramString != null) && (!paramString.trim().equalsIgnoreCase(""))) {
 for (int i = 0; i < paramString.length(); i++) {
 char c = paramString.charAt(i);
 String[] arrayOfString = PinyinHelper.toHanyuPinyinStringArray(c);
 if (arrayOfString != null) {
  localStringBuffer.append(arrayOfString[0].charAt(0));
 } else {
  localStringBuffer.append(c);
 }
 }
 }
 return localStringBuffer.toString();
 }

 public static String strFilter(String paramString) throws PatternSyntaxException {
 String str = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()――+|{}【】‘;:”“'。,、?\"]";
 Pattern localPattern = Pattern.compile(str);
 Matcher localMatcher = localPattern.matcher(paramString);
 return localMatcher.replaceAll("").trim();
 }

 public static String getPinYinHeadCharFilter(String paramString) {
 return strFilter(getPinYinHeadChar(paramString));
 }

 public static void main(String[] paramArrayOfString) {
 Logger localLogger = LoggerFactory.getLogger(PinyinUtil.class);
 String str = "?的输¥¥#s,ldsa";
 localLogger.info("小写输出:" + getPinyinToLowerCase(str));
 localLogger.info("大写输出:" + getPinyinToUpperCase(str));
 localLogger.info("首字母大写输出:" + getPinyinFirstToUpperCase(str));
 localLogger.info("返回中文的首字母输出:" + getPinYinHeadChar(str));
 localLogger.info("返回中文的首字母并过滤特殊字符输出:" + getPinYinHeadCharFilter(str));
 }
}

上一篇:java常用工具类 Date日期、Mail邮件工具类

栏    目:JAVA代码

下一篇:Springboot内置tomcat配置虚拟路径过程解析

本文标题:java中文转拼音工具类详解

本文地址:http://www.codeinn.net/misctech/11156.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有