Java: Pinying 拼音

 

/**
 * encoding: utf-8
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:https://github.com/houbb/pinyin
 * https://mvnrepository.com/artifact/com.github.stuxuhai/jpinyin/1.1.8
 * https://github.com/ranLee1/jpinyin
 * https://github.com/shenkevin/jpinyin
 * https://github.com/hellokaton/TinyPinyin  速度快
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/12/16 - 6:18
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javademo
 * # File      : PinyinHelper.java  类
 * # explain   : 学习
 **/

package Common;

import java.util.Random;
import java.lang.String;
import java.io.UnsupportedEncodingException;
import java.util.Random;
//import opensource.jpinyin.ChineseHelper;
//import opensource.jpinyin.PinyinFormat;
//import opensource.jpinyin.PinyinHelper;
import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.github.stuxuhai.jpinyin.*;

/**
 * 汉字转拼音类
 */
public class DuPinyinHelper {

    /**
     *有声调的拼音
     * @param ChineseWord 中国的首都是北京
     * @return
     */
    public  static String ShenTiao(String ChineseWord)
    {
        String pin="";
        try {

            
            //带音标   zhōng,guó,de,shǒu,dū,shì,běi,jīng
            pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_MARK);
            


  

            return  pin;

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            return  null;
        }

    }

    /**
     * 用数字标的拼音
     * @param ChineseWord
     * @return
     */
    public static String ShengtiaoNum(String ChineseWord)
    {
        String pin="";
        try {


            //用数字代替音标   zhong1,guo2,de5,shou3,du1,shi4,bei3,jing1
            System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_NUMBER));
            pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord,",",PinyinFormat.WITH_TONE_NUMBER);
            //不带音标  zhong,guo,de,shou,du,shi,bei,jing
           // System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE));
           // pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE);
            return  pin;

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            return  null;
        }
    }

    /**
     * 无声调的拼音
     * @param ChineseWord
     * @return
     */
    public static String NoShengtiaoNum(String ChineseWord)
    {
        String pin="";
        try {


            //不带音标  zhong,guo,de,shou,du,shi,bei,jing
            // System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE));
             pin=com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinString(ChineseWord, ",", PinyinFormat.WITHOUT_TONE);
            return  pin;

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            return  null;
        }
    }
    /**
     * 小写首字母
     * @param ChineseWord
     * @return
     */
    public static String ShengMuLower(String ChineseWord)
    {
        String pin="";
        try {

            //System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord));//输出拼音首字母 小写 zgdsdsbj
            pin=com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord).toLowerCase();
            return  pin;

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            return  null;
        }
    }

    /**
     * 大写首字母
     * @param ChineseWord
     * @return
     */
    public static String ShengMuUpper(String ChineseWord)
    {
        String pin="";
        try {

            //System.out.println(com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord));//输出拼音首字母
            pin=com.github.stuxuhai.jpinyin.PinyinHelper.getShortPinyin(ChineseWord).toUpperCase();
            return  pin;

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            return  null;
        }
    }

}

  

调用:

DuPinyinHelper.ShenTiao("我是涂聚文,江西人");

  

输出:

 

posted @ 2023-12-16 07:28  ®Geovin Du Dream Park™  阅读(32)  评论(0编辑  收藏  举报