1. 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;
  2.  
  3. import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;
    import java.util.TreeSet;
  4.  
  5. public class PinyinUtils {
    private static final Logger logger = LoggerFactory.getLogger(PinyinUtils.class);
  6.  
  7. /**
    * 单字解析
    *
    * @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;
    }
  8.  
  9. /**
    * 词组解析(全写)
    *
    * @param chs
    * @return
    */
    public static String getSelling(String chs) {
    return translate(chs, false);
    }
  10.  
  11. /**
    * 汉字转拼音
    *
    * @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 "";
    }
    }
  12.  
  13. /**
    * 词组解析(缩写)
    *
    * @param chs
    * @return
    */
    public static String getSmallSelling(String chs) {
    return translate(chs, true);
    }
  14.  
  15. /**
    * 获取首字母
    *
    * @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 "#";
    }
  16.  
  17. public static void main(String [] args) {
    PinyinUtils p = new PinyinUtils();
  18.  
  19. System.out.println(p.getSelling("单个"));
    System.out.println(p.getSmallSelling("测试"));
    }
    }

java汉字转拼音的工具类的更多相关文章

  1. Java汉字转成汉语拼音工具类

    Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包. import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...

  2. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  3. java中excel导入\导出工具类

    1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...

  4. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  5. java代码行数统计工具类

    package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...

  6. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  7. Java 操作Redis封装RedisTemplate工具类

    package com.example.redisdistlock.util; import org.springframework.beans.factory.annotation.Autowire ...

  8. java后台表单验证工具类

    /** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...

  9. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

随机推荐

  1. ip地址库选择

    目前市面上常用的ip地址库,有以下几种 1,新浪的api接口(限制未知)http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js& ...

  2. Linux常用指令总结

    概述 因为平时不是经常使用Linux系统,每次用到都需要重新温习一遍,这里对自己平时经常使用到的指令做个小结,方便后面直接查阅. 常用指令 登陆root指令 sudo su - 安装软件及卸载指令 d ...

  3. 360极速浏览器无法正确getHours

    开发了一个显示时间的功能. 功能是这样的,首先从数据库中读取时间,然后显示到前端页面上. 数据库中:2017-08-10 14:25:35.867 前端显示:2017-08-10T14:25:35.8 ...

  4. (转)ASP.NET MVC:Razor 引入命名空间

    页面中引用 c# @using MvcApplication83.Models @using MvcApplication83.Common 行尾不需要加分号,加上也无妨(不过得全加上). VB.Ne ...

  5. Yii Framework2.0开发教程(3)数据库mysql入门

    沿用教程(2)的代码 第一步.在本地mysql数据库中新建数据库zhyoulun 第二步.在数据库中新建表并插入若干条数据 CREATE TABLE `country` ( `code` CHAR(2 ...

  6. NoSQL摘录

    NoSQL泛指非关系型数据库,诸如Cassandra.MongoDB.Neo4J和Riak等.它们主张使用无模式(schemaless)的数据,可以运行在集群环境中. 开源的NoSQL数据库:Redi ...

  7. memcache操作实例

    实例一: <?php //使用memcache类来操作 $mm = new Memcache(); $mm->addServer("192.168.70.114",11 ...

  8. 品茗论道说广播(Broadcast内部机制讲解)(下)

    下面我们来看,递送广播动作中最重要的processNextBroadcast(). 3.2 最重要的processNextBroadcast() 从processNextBroadcast()的代码, ...

  9. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  10. php将远程图片下载保存到本地

    /* *功能:php完美实现下载远程图片保存到本地 *参数:文件url,保存文件目录,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getIma ...