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<String> 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<List<String>> temps = new ArrayList<>();
int len = chs.length();
int len1 = 0;
for (int i = 0; i < len; i++) {
List<String> 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<List<String>> t = new ArrayList<>();
for (int i = 0; i < len1; i++) {
List<String> currentList = new ArrayList<>();
List<String> stringList = temps.get(i);
if (stringList != null) {
for (String s : stringList) {
if (acronym) {
s = s.charAt(0) + "";
}
if (i > 0) {
List<String> 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<String> 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.getDefault());
}
} 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("测试"));
}
}
java汉字转拼音的工具类的更多相关文章
- Java汉字转成汉语拼音工具类
Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包. import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...
- Java操作文件夹的工具类
Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...
- java中excel导入\导出工具类
1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...
- java中定义一个CloneUtil 工具类
其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...
- java代码行数统计工具类
package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...
- Java加载Properties配置文件工具类
Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...
- Java 操作Redis封装RedisTemplate工具类
package com.example.redisdistlock.util; import org.springframework.beans.factory.annotation.Autowire ...
- java后台表单验证工具类
/** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
随机推荐
- 通过Socket连接一次传输多个文件
近期在做一个通过WIFI在手机之间传输文件的功能.须要在手机之间建立一个持久的Socket 连接并利用该连接数据传输.能够一次传输一个或多个文件. 在一次传输多个文件时,遇到了一个困难:怎样在接收文件 ...
- unity 已知cosA和sinA,求A
和c++中的atan2(y,x)类似,unity中有也Mathf.Atan2(y,x).
- [转]Windows 10 安装SVN 不显示状态图标--解决方法
原文链接:https://www.cnblogs.com/lzpong/p/6187366.html --- auth:lzpong 升级win10以后,什么都正常,就是svn版本库图标不见了,图标的 ...
- C++语言基础(21)-异常
C++语言本身或者标准库抛出的异常都是 exception 的子类,称为标准异常(Standard Exception).你可以通过下面的语句来捕获所有的标准异常: try{ //可能抛出异常的语句 ...
- application/x-www-form-urlencoded和multipart/form-data的区别
在学习<form>元素时,enctype属性有三个值 enctype属性表格: 值 描述 application/x-www-form-urlencoded 在发送前编码所有字符(默认) ...
- iptables简单配置
iptables简单配置 分类: Linux 安全2010-10-20 16:56 4241人阅读 评论(0) 收藏 举报 input防火墙tcpfilterubuntuservice # iptab ...
- 转载C#操作数据库小结
1.常用的T-Sql语句 查询:SELECT * FROM tb_test WHERE ID='1' AND name='xia' SELECT * FROM ...
- vim 指令学习
移动行: 命令:3 move 4 光标移动 H : 左移 J :下移 K :上移 L : 右移 : 移到行首 $ :移到行尾 :n :定位到某一行 查找指令: fx :行内向后查找x Fx :行内向前 ...
- Oracle Explain plan 使用总结
Oracle Explain plan使用总结 写多了SQL语句,伴随着数据量的海增,总会遇到性能的问题.在Oracle领域一个不好的习惯,一旦遇到性能问题就推给DBA来做.长期如此,反而对DB ...
- linux命令之高级使用 du
du命令:disk usage,顾名思义,是关于目录使用情况的,它的作用就是计算目录大小的. 1. 想看当前目录下所有目录以及子目录的大小: # du -h . “.”代表当前目录下.也可以换成一个明 ...