lucene初探(二):中文分词,以及系统自带分词简单比较

lucene学习我不得不承认这门技术是我目前接触的最有难度的一门技术,也许是因为我最近比较浮躁吧,也也是因为我没有找到,官方的说明文档和网络上比较不错的视频教程,不是有的讲解人普通话说得跟方言似的,英文读的跟3岁小孩似的,比如宋亮,他的lucene3.5的教程简直让我难以忍受
所以还是自己摸索摸索吧

上一篇lucene初探(一),讲解了简单的lucene文件查询,貌似lucene和solr配合更能发挥lucene的作用

也许是因为我现在的知识都没有系统的掌握,现在学习起来有点吃力,所以现在决定,先暂时放一放,lucene的学习,系统复习,web基础知识,和web前端框架的学习
言归正传,lucene自带分词器比较:
这边播客讲的比较系统大部分都涉及到了,但是没有什么用,所以我们只介绍一些实用的
http://blog.csdn.net/chs_jdmdr/article/details/7359773

导入所需jar包:

这里写图片描述

测试代码:

package com.leige.demo;



import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.util.Version;




public class AnalyzerTest {
public static void main(String[] args) {


    try {

/*      
 * simpleAnalyzer分词,根据标点符号分词SimpleAnalyzer simple=new SimpleAnalyzer(Version.LUCENE_40);
 * StandardAnalyzer standard=new StandardAnalyzer(Version.LUCENE_40);标准分词只会将中文分为一个个单字
 * WhitespaceAnalyzer wAnalyzer=new WhitespaceAnalyzer(Version.LUCENE_40);//英文分词,根据空格
 * ChineseAnalyzer analyzer=new ChineseAnalyzer(); //已经过时,一元分词
 * CJKAnalyzer analyzer=new CJKAnalyzer(Version.LUCENE_40);//二元分词
        */

        SimpleAnalyzer simpleAnalyzer=new SimpleAnalyzer(Version.LUCENE_40);
    displayTokens(simpleAnalyzer,new File("F:\\lucene\\example\\news.txt"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

    public static void displayTokens(Analyzer analyzer,File file) throws IOException {//打印分词结果,我也不懂,网上抄的,我就想用
        TokenStream tokenStream = analyzer.tokenStream("text2", new FileReader(file));
        displayTokens(tokenStream);
    }

    public static void displayTokens(TokenStream tokenStream) throws IOException {
        OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
        PositionIncrementAttribute positionIncrementAttribute = tokenStream.addAttribute(PositionIncrementAttribute.class);
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        TypeAttribute typeAttribute = tokenStream.addAttribute(TypeAttribute.class);

        tokenStream.reset();
        int position = 0;
        System.out.println("分词结果");
        while (tokenStream.incrementToken()) {
            int increment = positionIncrementAttribute.getPositionIncrement();
            if(increment > 0) {
                position = position + increment;
                System.out.print(position + ":");
            }
            int startOffset = offsetAttribute.startOffset();
            int endOffset = offsetAttribute.endOffset();
            String term = charTermAttribute.toString();
            System.out.println("[" + term + "]" + ":(" + startOffset + "-->" + endOffset + "):" + typeAttribute.type());
        }
    }
}

lucene自带分词比较:

分词文本:
**仔细想想我上一次熬夜看书是高中 的时候几年来再一次熬夜看书久违了的感觉,《偷影子的人》,就是有这种感觉让你一发不可收拾的想读下去那种爱情,亲情,友情我们也许一辈子都不会遇到但是每一种感情都没有可比性虽然书里的那种很令人艳羡 重要的是我们可以想主人公一样活的问心无永远天真,富有想象力有时候不那么成熟或许会好一点
**

分词器 分词类型 分词结果
simpleAnalyzer 根据标点符号分词或者宫格分词 结果1
StandardAnalyzer 一元分词 一个一个分词,没有必要贴出来
ChineseAnalyzer 一二元分词 没有必要贴出来
WhitespaceAnalyzer 空格分词,主要用在英文 没有必要贴出来
CJKAnalyzer 二元分词 没有必要贴出来

结果1:
这里写图片描述

总结,lucene不带任何插件的话,还是simpleAnalyzer比较好,可以根据标点符号分词,不会造成索引项过多,搜索也比较快

插件分词,最好的就是mmseg4j分词,使用的是搜狗的词库,而且一直在更新,podding分词,好像没有更新了

我作为小白,我只用最好的一个就可以了

mmseg4j使用,

引入jar包,这里写图片描述

使用,只要指定分词器和自己的自定义词库地址就好

MMSegAnalyzer analyzer=new MMSegAnalyzer(new File("F:\\lucene\\example\\data"));

注意词库命名方式必须为:wordsXXX.dic
所以如果没有什么特殊的要求,lucene自带的simple分词器,就可以满足简单的需求

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章