lucene Analyzer 庖丁解牛 中文分词

本文介绍了一种基于庖丁解牛分词法的关键词提取方法,并通过具体实现展示了如何从输入文本中提取有意义的关键词。文章详细解释了使用PaodingAnalyzer进行分词的过程,包括TokenStream的使用、关键词的提取与计数,以及最终关键词的排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /*
     *param   分词
     */
    public List getname(String param) throws IOException{
        //分词(庖丁解牛分词法)
        Analyzer ika = new PaodingAnalyzer();
        List<String> keys = new ArrayList<String>();
            TokenStream ts = null;
           
            try{
                Reader r = new StringReader(param);
                ts = ika.tokenStream("TestField", r);
                TermAttribute termAtt = (TermAttribute) ts.getAttribute(TermAttribute.class);
                TypeAttribute typeAtt = (TypeAttribute) ts.getAttribute(TypeAttribute.class);
                String key = null;
                while (ts.incrementToken()) {
                    if ("word".equals(typeAtt.type())) {
                        key = termAtt.term();
                        if (key.length() >= 2) {
                            keys.add(key);
                        }
                    }
                }
            }catch(IOException e){
                e.printStackTrace();
            } finally {
                if (ts != null) {
                    ts.close();
                }
            }
           
            Map<String, Integer> keyMap = new HashMap<String, Integer>();
            Integer $ = null;
            //计算每个词出现的次数
            for (String key : keys) {
                keyMap.put(key, ($ = keyMap.get(key)) == null ? 1 : $ + 1);
            }
            List<Map.Entry<String, Integer>> keyList = new ArrayList<Map.Entry<String, Integer>>(keyMap.entrySet());
            //进行排序
            Collections.sort(keyList, new Comparator<Map.Entry<String, Integer>>() {
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return (o2.getValue() - o1.getValue());
                }
            });
            //取出关键词
            String id = null;
            String str = "";
            List list = new ArrayList();
            if(keyList.size() >0){
                for (int i = 0;i < keyList.size(); i++) {
                     id = keyList.get(i).toString();
                     String[] strs = id.split("\\=");
                     str = strs[0];
                     list.add(strs[0]);
                     System.out.println("id:"+id);
                }
            }
            return list;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值