一、概念认识

1、常用的Analyer

SimpleAnalyzer、StopAnalyzer、WhitespaceAnalyzer、StandardAnalyzer

2、TokenStream

分词器做好处理之后得到的一个流,这个流中存储了分词的各种信息,可以通过TokenStream有效的获取到分词单元信息生成的流程

在这个流中所需要存储的数据

3、Tokenizer

主要负责接收字符流Reader,将Reader进行分词操作。有如下一些实现类

4、TokenFilter

将分词的语汇单元,进行各种各样过滤

5、内置常用分词器分词进行分词的差异

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void displayToken(String str,Analyzer a) {
        try {
            TokenStream stream = a.tokenStream("content",new StringReader(str));
            //创建一个属性,这个属性会添加流中,随着这个TokenStream增加
            CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);
            while(stream.incrementToken()) {
                System.out.print("["+cta+"]");
            }
            System.out.println();
        catch (IOException e) {
            e.printStackTrace();
        }
    }
1
2
3
4
5
6
7
8
9
public Map<String,Analyzer> toMap(String[] str,Analyzer ... analyzers){
        Map<String,Analyzer> analyzerMap = new HashMap<String,Analyzer>();
        int i =0;
        for(Analyzer a : analyzers){
            analyzerMap.put(str[i], a);
            i++;
        }
        return analyzerMap;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Test
    public void test01() {
        String[] str ={"StandardAnalyzer","StopAnalyzer","SimpleAnalyzer","WhitespaceAnalyzer"};
        Map<String,Analyzer> analyzerMap = new HashMap<String,Analyzer>();
         
        Analyzer a1 = new StandardAnalyzer(Version.LUCENE_35);
        Analyzer a2 = new StopAnalyzer(Version.LUCENE_35);
        Analyzer a3 = new SimpleAnalyzer(Version.LUCENE_35);
        Analyzer a4 = new WhitespaceAnalyzer(Version.LUCENE_35);
         
        analyzerMap = toMap(str,a1,a2,a3,a4);
         
        String txt = "this is my house,I am come from bilibili qiansongyi," +
                "My email is dumingjun@gmail.com,My QQ is 888168";
         
        for(String analyzer : analyzerMap.keySet()){
            System.out.println(analyzer);
            AnalyzerUtils.displayToken(txt, analyzerMap.get(analyzer));
            System.out.println("==============================");
        }
    }

6、中文分词

1
2
3
4
5
6
7
8
9
public void toMap(String txt,Analyzer ... analyzers){
        for(Analyzer a : analyzers){
            int start = a.toString().lastIndexOf(".")+1;
            int end = a.toString().lastIndexOf("@")-1;
            System.out.println(a.toString().substring(start, end));
            AnalyzerUtils.displayToken(txt, a);
            System.out.println("====================");
        }
    }
1
2
3
4
5
6
7
8
9
10
public void test02() {
        Analyzer a1 = new StandardAnalyzer(Version.LUCENE_35);
        Analyzer a2 = new StopAnalyzer(Version.LUCENE_35);
        Analyzer a3 = new SimpleAnalyzer(Version.LUCENE_35);
        Analyzer a4 = new WhitespaceAnalyzer(Version.LUCENE_35);
        Analyzer a5 = new MMSegAnalyzer(new File("D:\\lucene\\mmseg4j\\data"));
        String txt = "我来自中国广东省广州市天河区的小白";
         
        toMap(txt,a1,a2,a3,a4,a5);
    }

7、位置增量、位置偏移量、分词单元、分词器的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void displayAllTokenInfo(String str,Analyzer a) {
        try {
            TokenStream stream = a.tokenStream("content",new StringReader(str));
            //位置增量的属性,存储语汇单元之间的距离
            PositionIncrementAttribute pia = 
            stream.addAttribute(PositionIncrementAttribute.class);
            //每个语汇单元的位置偏移量
            OffsetAttribute oa = stream.addAttribute(OffsetAttribute.class);
            //存储每一个语汇单元的信息(分词单元信息)
            CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class);
            //使用的分词器的类型信息
            TypeAttribute ta = stream.addAttribute(TypeAttribute.class);
            for(;stream.incrementToken();) {
                System.out.print(pia.getPositionIncrement()+":");
                System.out.print(cta+"["+oa.startOffset()+"-"+oa.endOffset()+"]-->"+ta.type()+"\n");
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

8、停用分词器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyStopAnalyzer extends Analyzer {
    @SuppressWarnings("rawtypes")
    private Set stops;
    @SuppressWarnings("unchecked")
    public MyStopAnalyzer(String[]sws) {
        //会自动将字符串数组转换为Set
        stops = StopFilter.makeStopSet(Version.LUCENE_35, sws, true);
        //将原有的停用词加入到现在的停用词
        stops.addAll(StopAnalyzer.ENGLISH_STOP_WORDS_SET);
    }
     
    public MyStopAnalyzer() {
        //获取原有的停用词
        stops = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
    }
 
    @Override
    public TokenStream tokenStream(String fieldName, Reader reader) {
        //为这个分词器设定过滤链和Tokenizer
        return new StopFilter(Version.LUCENE_35,
               new LowerCaseFilter(Version.LUCENE_35, 
               new LetterTokenizer(Version.LUCENE_35,reader)), stops);
    }
 
}
1
2
3
4
5
6
7
8
@Test
    public void test04() {
        Analyzer a1 = new MyStopAnalyzer(new String[]{"I","you","hate"});
        Analyzer a2 = new MyStopAnalyzer();
        String txt = "how are you thank you I hate you";
        AnalyzerUtils.displayToken(txt, a1);
        AnalyzerUtils.displayToken(txt, a2);
    }

9、简单实现同义词索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MySameAnalyzer extends Analyzer {
    private SamewordContext samewordContext;
     
    public MySameAnalyzer(SamewordContext swc) {
        samewordContext = swc;
    }
 
    @Override
    public TokenStream tokenStream(String fieldName, Reader reader) {
        Dictionary dic = Dictionary.getInstance("D:\\lucene\\mmseg4j\\data");
        return new MySameTokenFilter(
                new MMSegTokenizer(new MaxWordSeg(dic), reader),samewordContext);
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class MySameTokenFilter extends TokenFilter {
    private CharTermAttribute cta = null;
    private PositionIncrementAttribute pia = null;
    private AttributeSource.State current;
    private Stack<String> sames = null;
    private SamewordContext samewordContext;
 
    protected MySameTokenFilter(TokenStream input,SamewordContext samewordContext) {
        super(input);
        cta = this.addAttribute(CharTermAttribute.class);
        pia = this.addAttribute(PositionIncrementAttribute.class);
        sames = new Stack<String>();
        this.samewordContext = samewordContext;
    }
 
    @Override
    public boolean incrementToken() throws IOException {
        if(sames.size()>0) {
            //将元素出栈,并且获取这个同义词
            String str = sames.pop();
            //还原状态
            restoreState(current);
            cta.setEmpty();
            cta.append(str);
            //设置位置0
            pia.setPositionIncrement(0);
            return true;
        }
         
        if(!this.input.incrementToken()) return false;
         
        if(addSames(cta.toString())) {
            //如果有同义词将当前状态先保存
            current = captureState();
        }
        return true;
    }
     
    private boolean addSames(String name) {
        String[] sws = samewordContext.getSamewords(name);
        if(sws!=null) {
            for(String str:sws) {
                sames.push(str);
            }
            return true;
        }
        return false;
    }
     
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public class SimpleSamewordContext2 implements SamewordContext {
     
    Map<String,String[]> maps = new HashMap<String,String[]>();
    public SimpleSamewordContext2() {
        maps.put("中国",new String[]{"天朝","大陆"});
    }
 
    @Override
    public String[] getSamewords(String name) {
        return maps.get(name);
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Test
    public void test05() {
        try {
            Analyzer a2 = new MySameAnalyzer(new SimpleSamewordContext2());
            String txt = "我来自中国广东省广州市天河区的小白";
            Directory dir = new RAMDirectory();
            IndexWriter writer = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_35, a2));
            Document doc = new Document();
            doc.add(new Field("content",txt,Field.Store.YES,Field.Index.ANALYZED));
            writer.addDocument(doc);
            writer.close();
            IndexSearcher searcher = new IndexSearcher(IndexReader.open(dir));
            TopDocs tds = searcher.search(new TermQuery(new Term("content","咱")),10);
//          Document d = searcher.doc(tds.scoreDocs[0].doc);
//          System.out.println(d.get("content"));
            AnalyzerUtils.displayAllTokenInfo(txt, a2);
        catch (CorruptIndexException e) {
            e.printStackTrace();
        catch (LockObtainFailedException e) {
            e.printStackTrace();
        catch (IOException e) {
            e.printStackTrace();
        }
    }

lucene 分词实现的更多相关文章

  1. Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息

    Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息 在此回复牛妞的关于程序中分词器的问题,其实可以直接很简单的在词库中配置就好了,Lucene中分词的所有信息我们都可以从 ...

  2. Hibernate Search集与lucene分词查询

    lucene分词查询参考信息:https://blog.csdn.net/dm_vincent/article/details/40707857

  3. Lucene系列三:Lucene分词器详解、实现自己的一个分词器

    一.Lucene分词器详解 1. Lucene-分词器API (1)org.apache.lucene.analysi.Analyzer 分析器,分词器组件的核心API,它的职责:构建真正对文本进行分 ...

  4. WebGIS中兴趣点简单查询、基于Lucene分词查询的设计和实现

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 兴趣点查询是指:输入框中输入地名.人名等查询信息后,地图上可 ...

  5. lucene分词器与搜索

    一.分词器 lucene针对不同的语言和虚伪提供了许多分词器,我们可以针对应用的不同的需求使用不同的分词器进行分词.我们需要注意的是在创建索引时使用的分词器与搜索时使用的分词器要保持一致.否则搜索的结 ...

  6. 全文索引(三)lucene 分词 Analyzer

    分词: 将reader通过阅读对象Analyzer字处理,得到TokenStream处理流程被称为分割. 该解释可能是太晦涩.查看示例,这个东西是什么感性的认识. 样品:一段文本"this ...

  7. lucene分词多种方法

    目前最新版本的lucene自身提供的StandardAnalyzer已经具备中文分词的功能,但是不一定能够满足大多数应用的需要.另外网友谈的比较多的中文分词器还有:CJKAnalyzerChinese ...

  8. Lucene分词详解

    分词和查询都是以词项为基本单位,词项是词条化的结果.在Lucene中分词主要依靠Analyzer类解析实现.Analyzer类是一个抽象类,分词的具体规则是由子类实现的,所以对于不同的语言规则,要有不 ...

  9. 学习笔记(三)--Lucene分词器详解

    Lucene-分词器API org.apache.lucene.analysi.Analyzer 分析器,分词器组件的核心API,它的职责:构建真正对文本进行分词处理的TokenStream(分词处理 ...

随机推荐

  1. WCF 依赖注入-- Attribute

    最近,工作之余学习WCF(Windows Communication Fundation)时,感觉自己还有好多的东西需要学习呀⊙﹏⊙b汗,于是想记录下自己学习WCF的心得,以鼓励自己再接再厉,同时希望 ...

  2. 对点餐APP现阶段开发的问题

    团队的成立,基本是一气呵成.但是,github团队的建立却成为 第一个难题,大家对github都不熟,又刚刚好没课时间的任务,大家 已经各有安排,造成时间上的紧急.没有按时.按要求完成github的 ...

  3. hasOwnProperty 递归 简单回调 链式调用

    1.hasOwnProperty 函数的返回值为Boolean类型.如果对象object具有名称为propertyName的属性,则返回true,否则返回false. function Box(){ ...

  4. 设置更新源和下载ferret

    kali无法定位软件包 解决: deb http://http.kali.org/kali kali-rolling main non-free contrib kali可用的官方更新源(cd /et ...

  5. mpt_voronoi demo

    % %demo1% A=rand(3,10);% pbound=Polyhedron([0 0 0;150 0 0;150 150 0;0 150 0; 0 0 1;150 0 1;150 150 1 ...

  6. KEEPALIVED 双机自动切换部署备忘

    1.配置文件的名字不要型错了.开始我将配置文件写成keeplive.conf,运行后也不报错,但无法看到VIP.日志里也看不到任何有价值信息.直到后来反复检查才发现可能配置文件名有问题,修正为keep ...

  7. paper 130:MatLab分类器大全(svm,knn,随机森林等)

    train_data是训练特征数据, train_label是分类标签.Predict_label是预测的标签.MatLab训练数据, 得到语义标签向量 Scores(概率输出).1.逻辑回归(多项式 ...

  8. UBUNTU 10.04上安装和使用HAMACHI

    https://secure.logmein.com/US-ES/labs/#HamachiforLinux https://secure.logmein.com/CN/,经过笔者使用,非常不错. 官 ...

  9. Entity Framework7 有哪些不同?现在开发到什么程度了?

    Entity Framework7之开篇 一.Entity Framework7 简介 Entity Framework7简称EF7,是微软正在开发的最新的在.NET应用中首选的数据访问技术.它是轻量 ...

  10. Apache部署django项目

    在此之前,我们一直使用django的manage.py 的runserver 命令来运行django应用,但这只是我们的开发环境,当项目真正部署上线的时候这做就不可行了,必须将我们的项目部署到特定的w ...