Java 输入中文名,同时搜索其拼音结果

今天,我的怨种 导师让我改两年前的代码。众所周知,外文论文的署名得放洋p,所以就有了 张三 的英文名是“zhang san”和“san zhang”,需要我在搜索中文名的同时能同时出英文拼音对应的List。
想法就是:分别搜索张三,zhang san,san zhang对应的List,取并集,一起返回,麻烦的点就是中文转拼音。
首先,使用java工具pinyin4j,并把它封装成一个工具类放在tool文件夹里(别问我为什么不说utils,问就是上一个后端的锅),这样就可以实现中转拼音
先在pom.xml中添加maven如下:


        
            com.belerweb
            pinyin4j
            2.5.0
        

然后封装工具类ToPY

package com.achement.achment.tool;
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;
import org.apache.commons.lang.StringUtils;

public class ToPY {

    /**
     * 判断该字符串是否为中文
     * @param string
     * @return
     */
    public static boolean isChinese(String string){
        int n = 0;
        for(int i = 0; i < string.length(); i++) {
            n = (int)string.charAt(i);
            if(!(19968 <= n && n <40869)) {
                return false;
            }
        }
        return true;
    }
    /**
     * 将汉字转换为全拼
     *
     * @param text 文本
     * @param separator 分隔符
     * @return {@link String}
     */
    public static String getPinyin(String text, String separator) {
        char[] chars = text.toCharArray();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        // 设置大小写(不用)
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 设置声调表示方法(这里不用)
//  format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] s;
        String rs = StringUtils.EMPTY;
        int num=0;
        try {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < chars.length; i++) {
                // 判断是否为汉字字符【在这里让张三 变 zhang san,张小四变 zhang xiaosi】,注意sql查询是模糊查询,所以这边不用分大小写,注意结合自己的情况哦
                if (String.valueOf(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    s = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);
                    if (s != null) {
                        String ss =s[0];
                        s[0]= ss.substring(0,ss.length() - 1);
                        if(num==0)
                            sb.append(s[0]).append(separator);
                        else
                            sb.append(s[0]);
                        num++;
                        continue;
                    }
                }
                sb.append(String.valueOf(chars[i]));
                if ((i + 1 >= chars.length) || String.valueOf(chars[i + 1]).matches("[\\u4E00-\\u9FA5]+")) {
                        sb.append(separator);
                }
            }
            rs = sb.substring(0, sb.length());
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return rs;
    }
}

编写在Servicelmpl中的实现类

@Override
    public List selectCondition1(String paperTitle,String correspondAuthor,String paperAuthor,String publishedPeriodicals ,String publishedDate,
                                        String paperLevel,String paperState,String firstUnit,String paperRank){
        //查中文
        List paperListChi,paperListPY,paperListReverse,paperList;
        boolean Authorflag = true;
        paperListChi = paperDao.selectCondition1(paperTitle,correspondAuthor,paperAuthor,publishedPeriodicals,publishedDate,
                paperLevel,paperState,firstUnit,paperRank);
        List paperLists = new ArrayList(paperListChi);
        //paperListChi 中文名:张雪蕉 paperListPY 中文拼音1:zhang xuejiao  paperListReverse 中文拼音2:xuejiao zhang
        //中转拼音
        if(paperAuthor =="")
            Authorflag = false;
        if(ToPY.isChinese(paperAuthor)&& Authorflag) {
            //制作中文拼音1
            String paperAuthorPY = ToPY.getPinyin(paperAuthor," ");
            paperListPY = paperDao.selectCondition1(paperTitle,correspondAuthor,paperAuthorPY,publishedPeriodicals,publishedDate,
                    paperLevel,paperState,firstUnit,paperRank);
            //这两步是取并集操作,去重
            paperLists.removeAll(paperListPY);
            paperLists.addAll(paperListPY);
            //制作中文拼音2
            String[] paperAuthorReverseString = paperAuthorPY.split(" ");
            StringBuilder paperAuthorSb = new StringBuilder();
            StringBuilder paperAuthorReverseSb = paperAuthorSb.append(paperAuthorReverseString[1]).append(" "+paperAuthorReverseString[0]);
            String paperAuthorReverse = paperAuthorReverseSb.toString();
            paperListReverse = paperDao.selectCondition1(paperTitle,correspondAuthor,paperAuthorReverse.toString(),publishedPeriodicals,publishedDate,
                    paperLevel,paperState,firstUnit,paperRank);
            paperLists.removeAll(paperListReverse);
            paperLists.addAll(paperListReverse);
        }
        paperList = paperLists;
        return paperList;
    }

最后致谢:感谢老师把我这个前端培养成全栈,thx,做得很好,下次不要再做了,学弟学妹逃光了。

你可能感兴趣的:(java,maven)