Lucene 自定义分析器

自定义一个NGramAnalyzer:

  1 package org.apache.lucene.analysis.ngram;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileReader;
  6 import java.io.IOException;
  7 import java.io.Reader;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 import org.apache.lucene.analysis.Analyzer;
 12 import org.apache.lucene.analysis.TokenStream;
 13 import org.apache.lucene.analysis.Tokenizer;
 14 import org.apache.lucene.analysis.Analyzer.TokenStreamComponents;
 15 import org.apache.lucene.analysis.core.StopFilter;
 16 import org.apache.lucene.analysis.core.WhitespaceTokenizer;
 17 import org.apache.lucene.analysis.standard.StandardFilter;
 18 import org.apache.lucene.analysis.util.CharArraySet;
 19 import org.apache.lucene.util.Version;
 20 
 21 public class NGramAnalyzer extends Analyzer{
 22     
 23     private Version matchVersion ;
 24     
 25     private int gramSize ;
 26     
 27     private CharArraySet stopwordSet ;
 28     
 29     public NGramAnalyzer(){
 30         
 31     }
 32     
 33     public NGramAnalyzer(Version matchVersion ,int gramSize) {
 34         
 35         this.matchVersion = matchVersion ;
 36         
 37         this.gramSize = gramSize ;
 38 
 39         try {
 40             
 41             List<String> stopWords;
 42             
 43             String stopWordPath = "E://Java program//PatentRetrival//resouce//stopwords.txt";
 44             
 45             stopWords = readStopwords(stopWordPath);
 46             
 47             this.stopwordSet = new CharArraySet(Version.LUCENE_4_9 ,stopWords ,true);
 48             
 49         } catch (IOException e) {
 50             // TODO Auto-generated catch block
 51             e.printStackTrace();
 52         }
 53         
 54         
 55         
 56         
 57     }
 58     
 59     
 60 
 61     @Override
 62     protected TokenStreamComponents createComponents(String fieldName,Reader reader) {
 63         
 64         // TODO Auto-generated method stub
 65         
 66          Tokenizer tokenizer = new NGramTokenizer(Version.LUCENE_4_9, reader,gramSize ,gramSize); 
 67          
 68          TokenStream tokenStream = new NGramTokenFilter(matchVersion, tokenizer);
 69          
 70             //tok = new LowerCaseFilter(matchVersion, tok);
 71          tokenStream = new StopFilter(matchVersion, tokenStream, stopwordSet);
 72          return new TokenStreamComponents(tokenizer ,tokenStream);
 73         
 74     }
 75     
 76     public  List<String> readStopwords(String filepath) throws IOException{
 77         
 78         List<String> stopwords = new ArrayList<String>() ;
 79         
 80         File file = new File(filepath);
 81         
 82         BufferedReader reader = new BufferedReader(new FileReader(file));
 83         
 84         String word;
 85         
 86         while((word = reader.readLine() )!= null){
 87             
 88             
 89              
 90              word = new String(word.getBytes("UTF-8"), "gb2312");
 91              
 92              stopwords.add(word);
 93              
 94              System.out.print(" stopword = " + word);
 95         }
 96         
 97         return stopwords  ;
 98         
 99     }
100 
101 }

 

转载于:https://www.cnblogs.com/song-yajun/p/3878781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值