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/ ...
随机推荐
- JS关于scrollTop和可视区域clientWidth
一.scrollTop,scrollLeft 要获得页面的scrollTop及scrollLeft,在不同的浏览器中是不一样的: 谷歌浏览器和没声明DTD的文档,通过document.body.scr ...
- 【转】C# 视频监控系列(13):H264播放器——控制播放和截图
本文原文地址:http://www.cnblogs.com/over140/archive/2009/03/30/1421531.html 阿里云栖社区也有相关的视频开发案例:https://yq.a ...
- SQL语句创建相同结构的表
--Oracle的语句create table sa_salaryRecord as select * from sa_salary where 1=2; --MSSQL的语句select * int ...
- Thinkphp+AJAX动态验证用户输入是否合法
遇到用户注冊等情况时.假设等用户输入全部信息,点击注冊button提交后.再验证输入是否正确,体验非常不好,并且非常浪费用户的时间,添加注冊成本,这里提供一个样例,演示了怎么使用ajax进行单步验证, ...
- iOS 9应用开发教程之ios9中实现button的响应
iOS 9应用开发教程之ios9中实现button的响应 IOS9实现button的响应 button主要是实现用户交互的.即实现响应.button实现响应的方式能够依据加入button的不同分为两种 ...
- Xgboost 模型保存和载入()
https://blog.csdn.net/u012884015/article/details/78653178 xgb_model.get_booster().save_model('xgb.mo ...
- Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结
Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结 1. 本文范围 1 2. Angular的优点 1 2.1. 双向数据绑定 1 2.2. dsl ...
- 基于flink快速开发实时TopN程序
TopN 是统计报表和大屏非常常见的功能,主要用来实时计算排行榜.流式的TopN可以使业务方在内存中按照某个统计指标(如出现次数)计算排名并快速出发出更新后的排行榜. 我们以统计词频为例展示一下如何快 ...
- .net访问Oracle数据库
使用System.Data.OracleClient访问Oracle数据库时报错 尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Orac ...
- 第二百二十一节,jQuery EasyUI,Form(表单)组件
jQuery EasyUI,Form(表单)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Form(表单)组件的使用方法,这个组件不依赖于 ...