1.自定义Analyzer:

@Test
 public void t01() throws Exception {
  ArrayList<String> strings = new ArrayList<String>() {
   {
    this.add("小鬼子");
    this.add("美国佬");
   }
  };
  Analyzer analyzer = new CustomStandardAnalyzer(strings);
  String content = "小鬼子 and 美国佬 are playing together!";
  TokenStream tokenStream = analyzer.tokenStream("myfield", content);
  tokenStream.reset();
  CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
  while (tokenStream.incrementToken()) {
   // 已经过滤掉自定义停用词
   // 输出:playing together
   System.out.println(charTermAttribute.toString());
  }
  tokenStream.end();
  tokenStream.close();
  analyzer.close();
 }
 
 @Test
 public void t02() throws Exception {
  
  Analyzer analyzer = new SameWordAnalyzer();
  String content = "这花美丽";
  TokenStream tokenStream = analyzer.tokenStream("myfield", content);
  tokenStream.reset();
  CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
  while (tokenStream.incrementToken()) {
   
   System.out.println(charTermAttribute.toString());
  }
  tokenStream.end();
  tokenStream.close();
  analyzer.close();
 }

2.自定义TokenFilter

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class SameWordTokenFilter extends TokenFilter {

    private CharTermAttribute charTermAttribute;
    private PositionIncrementAttribute positionIncrementAttribute;
    private State state;
    private Stack<String> stack;

    public SameWordTokenFilter(TokenStream input) {
        super(input);
        this.stack = new Stack<>();
        this.charTermAttribute = this.addAttribute(CharTermAttribute.class);
        this.positionIncrementAttribute = this.addAttribute(PositionIncrementAttribute.class);
        this.stack = new Stack<>();
    }

    @Override
    public final boolean incrementToken() throws IOException {

        while (this.stack.size() > 0) {

            this.restoreState(this.state);

            this.charTermAttribute.setEmpty();
            this.charTermAttribute.append(this.stack.pop());

            this.positionIncrementAttribute.setPositionIncrement(0);

            return true;
        }

        if (!this.input.incrementToken()) {
            return false;
        }

        String term = this.charTermAttribute.toString();

        if (this.getSameWords(term)) {
            this.state = this.captureState();
        }

        return true;
    }

    private boolean getSameWords(String name) {

        Map<String, String[]> map = new HashMap<>();
        map.put("美", new String[]{"美丽", "好看"});
        map.put("花", new String[]{"鲜花", "花朵"});

        String[] words = map.get(name);

        if (words != null) {
            for (String word : words) {
                this.stack.push(word);
            }

            return true;
        }

        return false;
    }
}

3.使用自定义Analyzer和自定义TokenFilter

ArrayList<String> strings = new ArrayList<String>() {{
            this.add("小鬼子");
            this.add("美国佬");
        }};
        Analyzer analyzer = new CustomStandardAnalyzer(strings);
        String content = "小鬼子 and 美国佬 are playing together!";
        TokenStream tokenStream = analyzer.tokenStream("myfield", content);
        tokenStream.reset();
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        while (tokenStream.incrementToken()) {
            // 已经过滤掉自定义停用词
            // 输出:playing   together
            System.out.println(charTermAttribute.toString());
        }
        tokenStream.end();
        tokenStream.close();

        analyzer.close();

4.代码解释,具体Analyzer和 TokenFilter之间的关联,用Eclipse的DEBUG功能,跟踪理解。

Lucene 7.2.1 自定义Analyzer和TokenFilter的更多相关文章

  1. Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer

    原文:Elasticsearch7.X 入门学习第七课笔记-----Mapping多字段与自定义Analyzer 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处 ...

  2. Lucene 7.2.1 自定义TokenFilter

    1.自定义TokenFilter import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.To ...

  3. lucene分词器中的Analyzer,TokenStream, Tokenizer, TokenFilter

    分词器的核心类: Analyzer:分词器 TokenStream: 分词器做优点理之后得到的一个流.这个流中存储了分词的各种信息,能够通过TokenStream有效的获取到分词单元. 下面是把文件流 ...

  4. lucene源码分析(7)Analyzer分析

    1.Analyzer的使用 Analyzer使用在IndexWriter的构造方法 /** * Constructs a new IndexWriter per the settings given ...

  5. Lucene根据字段进行自定义搜索扩展

    最近需要对公司的产品搜索功能做一步改动,搜索到的结果首先按照是否有库存进行排序,然后再按照销量.由于库存量也是一个整数,如果直接按照库存量进行倒序排序的话,是不符合要求的,Lucene也没有支持我们这 ...

  6. 多字段特性及配置自定义Analyzer

    PUT logs/_doc/1 {"level":"DEBUG"} GET /logs/_mapping POST _analyze { "token ...

  7. Lucene 中自定义排序的实现

    使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...

  8. ElasticSearch 启动时加载 Analyzer 源码分析

    ElasticSearch 启动时加载 Analyzer 源码分析 本文介绍 ElasticSearch启动时如何创建.加载Analyzer,主要的参考资料是Lucene中关于Analyzer官方文档 ...

  9. lucene学习教程

    1Lucene的介绍 ①Lucene是什么: 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎 ②Lu ...

随机推荐

  1. OSI网络七层协议+火了火了火

    因为部门新进了一台价值百万的网络测试设备,所以有太大的必要了解有关网络相关的基础知识了. 网络七层协议OSI(open system interconnection)从上到下依次为:应用层.表示层.会 ...

  2. linux下面重启apche 与mysql服务

    1.service httpd restart 重启apache 2.service mysqld restart 重启mysql 开启与停止换成start与stop即可

  3. Available Captures for Rook LT999

    On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, an ...

  4. delete,truncate ,drop区别

    use [database_name]go delete from table_nameTest  where FCRTime<(Select CONVERT(varchar(100),DATE ...

  5. 别人的Linux私房菜(5)首次CentOS7与帮助等

    ctrl alt F1-F6切换终端tty1-6,其中,F1的终端带有用户界面. 在终端登录后,输入startx启动个人图形界面.(启动有一些条件限制,如没有其他的X Window启用,已经安装,并具 ...

  6. lombok(@Getter&@Setter)

    Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法. 官方地址:https://project ...

  7. ubuntu无法打开software-center

    ubuntu无法打开software-center BUG: 在ubuntu14.04LTS版本下,点击软件中心图标,过了一会软件未能启动,没有动静.用命令行启动报如下错误: perrin@Littl ...

  8. OpenCV2.4.10 + VS2010开发环境配置

    原文转载自:qinyang8513 一.开发环境 1.操作系统:Windows 7(64位) 2.编程环境:Microsoft Visual Studio 2010 3.OpenCV版本:2.4.10 ...

  9. 题解 luogu P1144 【最短路计数】

    本蒟蒻也来发一次题解第一篇请见谅 这个题有几个要点 1.无向无权图,建图的时候别忘记建来回的有向边[因此WA掉1次 2.无权嘛,那么边长建成1就好了2333333 3.最短路采用迪杰斯特拉(别忘用堆优 ...

  10. HTK计算mfcc/filter_bank源码解析

    HTK计算mfcc/filter_bank源码解析 HTK可以用简单的 HCopy -C config -s scp 求取mfcc或者filter_bank 关于mfcc的原理在 http://my. ...