欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java汉字转拼音工具类分享

程序员文章站 2023-11-12 14:46:10
本文实例为大家分享了java汉字转拼音工具类的具体代码,供大家参考,具体内容如下 import com.google.common.base.strings;...

本文实例为大家分享了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("测试"));
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。