zoukankan      html  css  js  c++  java
  • Java之——汉字转换拼音(大小写)

    pinyin4J 是一个可以将汉字转换成拼音的lib,非常实用,其maven地址为:http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
    pinyin4J 提供PinyinHelper这个静态类对外提供拼音转换的服务我们可以封装一个工具类,用来将汉字转换成拼音,这里只使用了汉字拼音。

    首先要将pinyin4j加入项目中,如果是maven项目,可以添加引用:

    <dependency>  
        <groupId>com.belerweb</groupId>  
        <artifactId>pinyin4j</artifactId>  
        <version>2.5.0</version>  
    </dependency>  

    非maven的可以直接将下载好的jar包放入classpath。
    然后编写工具类 PinyinTool.java:

    package com.zsdf.drugcategoryrelation.controller;
    
    import net.sourceforge.pinyin4j.PinyinHelper;
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    
    
    /**
     * 汉字转化为拼音的工具类
     * @author liuyazhuang
     *
     */
    public class PinyinTool {
        HanyuPinyinOutputFormat format = null;
        public static enum Type {
            UPPERCASE,              //全部大写
            LOWERCASE,              //全部小写
            FIRSTUPPER              //首字母大写
        }
    
        public PinyinTool(){
            format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        }
    
        public String toPinYin(String str) throws BadHanyuPinyinOutputFormatCombination{
            return toPinYin(str, "", Type.UPPERCASE);
        }
    
        public String toPinYin(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{
            return toPinYin(str, spera, Type.UPPERCASE);
        }
    
        /**
         * 将str转换成拼音,如果不是汉字或者没有对应的拼音,则不作转换
         * 如: 明天 转换成 MINGTIAN
         * @param str:要转化的汉字
         * @param spera:转化结果的分割符
         * @return
         * @throws BadHanyuPinyinOutputFormatCombination
         */
        public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
            if(str == null || str.trim().length()==0)
                return "";
            if(type == Type.UPPERCASE)
                format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
            else
                format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    
            String py = "";
            String temp = "";
            String[] t;
            for(int i=0;i<str.length();i++){
                char c = str.charAt(i);
                if((int)c <= 128)
                    py += c;
                else{
                    t = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if(t == null)
                        py += c;
                    else{
                        temp = t[0];
                        if(type == Type.FIRSTUPPER)
                            temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                        py += temp+(i==str.length()-1?"":spera);
                    }
                }
            }
            return py.trim();
        }
    }

    编写测试类PingyinToolTest

    package com.lyz.pingyin.test;  
      
    import com.lyz.pingyin.PinyinTool;  
    import com.lyz.pingyin.PinyinTool.Type;  
      
    /** 
     * 测试拼音转化结果 
     * @author liuyazhuang 
     * 
     */  
    public class PingyinToolTest {  
        public static void main(String[] args) throws Exception{  
            PinyinTool tool = new PinyinTool();  
            System.out.println("刘亚壮的运行测试结果为====" + tool.toPinYin("刘亚壮", "", Type.LOWERCASE));  
        }  
    }  
     

    运行结果:刘亚壮的运行测试结果为====liuyazhuang  

    汉子转成拼音很快就搞定啦!

  • 相关阅读:
    [论文笔记]CVPR2017_Joint Detection and Identification Feature Learning for Person Search
    [论文笔记]Objects as Points
    [论文笔记]ICCV2017_SVDNet for Pedestrian Retrieval
    [论文笔记]ICPR2016_Person Re-Identification Using CNN Features Learned from Combination of Attributes
    VMware ESXI6.0服务器安装系列:RAID设置
    LVM基础详细说明及动态扩容lvm逻辑卷的操作记录
    调用对象 “ha-datastoresystem”的“HostDatastoreSystem.QueryVmfsDatastoreCreateOptions” 失败。
    动态扩容lvm逻辑卷的操作记录
    Kubernetes之Flannel介绍
    Linux服务器同步网络时间
  • 原文地址:https://www.cnblogs.com/baorantHome/p/8384962.html
Copyright © 2011-2022 走看看