博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据汉字首拼、全拼及汉字检索
阅读量:4694 次
发布时间:2019-06-09

本文共 2103 字,大约阅读时间需要 7 分钟。

类似淘宝、京东等电商系统的汉字、拼音检索功能的实现。

在检索商品时,如何根据商品名的首拼、全拼检索商品?

方案一: 在商品表goods中添加两个新字段存放首拼、全拼,让后按一般的字符串去查询。

方案二: 将全拼、首拼放入Lucene索引中,根据Lucene的"多域值"的特性检索。

 

1.Lucene的知识请自行查询其他资料.

2.汉字拼音、首拼获取如下:

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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PingYinUtil {
    /**
     * 得到 全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }
    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }
    /**
     * 将字符串转移为ASCII码
     *
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            // System.out.println(Integer.toHexString(bGBK[i]&0xff));
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }
}

转载于:https://www.cnblogs.com/wjdtyp/p/3991654.html

你可能感兴趣的文章
批量更新
查看>>
Vue学习记录(二)
查看>>
前端面试-难点问题2-java和javascript的区别
查看>>
Linux及Arm-Linux程序开发笔记(零基础入门篇)
查看>>
Redis集群创建报错
查看>>
DispacherServlet 的作用
查看>>
POJ - 1426(Find The Multiple)
查看>>
如何引用vue
查看>>
一张图带你看懂原始dao与SQL动态代理开发的区别-Mybatis
查看>>
最锋利的Visual Studio Web开发工具扩展:Web Essentials详解
查看>>
Python面试315题
查看>>
python中使用多继承
查看>>
HDU 3578 Greedy Tino(双塔DP)
查看>>
HDU 1103 Flo's Restaurant(模拟+优先队列)
查看>>
游戏机生产厂家世界OL敏枪如何玩 敏枪玩法攻略
查看>>
瀑布流
查看>>
s.t.
查看>>
2016年10月30日--JavaScript语法
查看>>
MiCode 40: 找小“3”
查看>>
四则运算1.0版本
查看>>