基于Lucene 4.x的ik-analyzer

2019-03-27 01:03|来源: 网路

需要修改IKAnalyzer.java、IKTokenizer.java、IKTokenizerFactory.java。

 

 1  import java.io.Reader;
 2  import org.apache.lucene.analysis.Analyzer;
 3  import org.apache.lucene.analysis.Tokenizer;
 4 
 5  /**
 6   * 实现Lucene Analyzer 基于IKTokenizer的中文分词器
 7   * 
 8   *  @author  林良益
 9   * 
10    */
11  public  final  class IKAnalyzer  extends Analyzer {
12 
13      private  boolean isMaxWordLength =  false;
14 
15      /**
16       * IK分词器Lucene Analyzer接口实现类 默认最细粒度切分算法
17        */
18      public IKAnalyzer() {
19          this( false);
20     }
21 
22      /**
23       * IK分词器Lucene Analyzer接口实现类
24       * 
25       *  @param  isMaxWordLength
26       *            当为true时,分词器进行最大词长切分
27        */
28      public IKAnalyzer( boolean isMaxWordLength) {
29          super();
30          this.setMaxWordLength(isMaxWordLength);
31     }
32 
33     @Override
34      public TokenStreamComponents createComponents(String fieldName,
35             Reader reader) {
36         Tokenizer tokenizer =  new IKTokenizer(reader, isMaxWordLength());
37          return  new TokenStreamComponents(tokenizer,  null);
38     }
39 
40      public  void setMaxWordLength( boolean isMaxWordLength) {
41          this.isMaxWordLength = isMaxWordLength;
42     }
43 
44      public  boolean isMaxWordLength() {
45          return isMaxWordLength;
46     }
47 
48 }

 

 

 1  import java.io.IOException;
 2  import java.io.Reader;
 3 
 4  import org.apache.lucene.analysis.Tokenizer;
 5  import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
 6  import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 7  import org.wltea.analyzer.IKSegmentation;
 8  import org.wltea.analyzer.Lexeme;
 9 
10  /**
11   * IK Analyzer v3.2 Lucene4.x Tokenizer适配器类 它封装了IKSegmentation实现
12   * 
13   *  @author  林良益
14   * 
15    */
16  public  final  class IKTokenizer  extends Tokenizer {
17      //  IK分词器实现
18       private IKSegmentation _IKImplement;
19      //  词元文本属性
20       private CharTermAttribute termAtt;
21      //  词元位移属性
22       private OffsetAttribute offsetAtt;
23      //  记录最后一个词元的结束位置
24       private  int finalOffset;
25 
26      /**
27       * Lucene Tokenizer适配器类构造函数
28       * 
29       *  @param  in
30       *  @param  isMaxWordLength
31       *            当为true时,分词器进行最大词长切分;当为false是,采用最细粒度切分
32        */
33      public IKTokenizer(Reader in,  boolean isMaxWordLength) {
34          super(in);
35         offsetAtt = addAttribute(OffsetAttribute. class);
36         termAtt = addAttribute(CharTermAttribute. class);
37         _IKImplement =  new IKSegmentation(in, isMaxWordLength);
38     }
39 
40     @Override
41      public  final  boolean incrementToken()  throws IOException {
42          //  清除所有的词元属性
43          clearAttributes();
44         Lexeme nextLexeme = _IKImplement.next();
45          if (nextLexeme !=  null) {
46              //  将Lexeme转成Attributes
47               //  设置词元文本
48              termAtt.setEmpty().append(nextLexeme.getLexemeText());
49              //  设置词元位移
50              offsetAtt.setOffset(nextLexeme.getBeginPosition(),
51                     nextLexeme.getEndPosition());
52             offsetAtt.setOffset(correctOffset(nextLexeme.getBeginPosition()), correctOffset(nextLexeme.getEndPosition()));
53             finalOffset = nextLexeme.getEndPosition();
54              //  返会true告知还有下个词元
55               return  true;
56         }
57          //  返会false告知词元输出完毕
58           return  false;
59     }
60 
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader)
65        */
66      public  void reset()  throws IOException {
67          super.reset();
68         _IKImplement.reset(input);
69     }
70 
71     @Override
72      public  final  void end() {
73         offsetAtt.setOffset(finalOffset, finalOffset);
74     }

75 } 

 

 1  import java.io.Reader;
 2  import java.util.Map;
 3 
 4  import org.apache.lucene.analysis.Tokenizer;
 5  import org.apache.lucene.analysis.util.TokenizerFactory;
 6  import org.wltea.analyzer.lucene.IKTokenizer;
 7 
 8  /**
 9   * 实现Solr4.x分词器接口
10   * 基于IKTokenizer的实现
11   * 
12   *  @author  林良益、李良杰
13   *
14    */
15  public  final  class IKTokenizerFactory  extends TokenizerFactory{
16     
17      private  boolean isMaxWordLength =  false;
18     
19      /**
20       * IK分词器Solr TokenizerFactory接口实现类
21       * 默认最细粒度切分算法
22        */
23      public IKTokenizerFactory(){
24     }
25     
26      /*
27       * (non-Javadoc)
28       * @see org.apache.solr.analysis.BaseTokenizerFactory#init(java.util.Map)
29        */
30      public  void init(Map<String,String> args){
31         String _arg = args.get("isMaxWordLength");
32         isMaxWordLength = Boolean.parseBoolean(_arg);
33     }
34     
35      /*
36       * (non-Javadoc)
37       * @see org.apache.solr.analysis.TokenizerFactory#create(java.io.Reader)
38        */
39      public Tokenizer create(Reader reader) {
40          return  new IKTokenizer(reader , isMaxWordLength());
41     }
42 
43      public  void setMaxWordLength( boolean isMaxWordLength) {
44          this.isMaxWordLength = isMaxWordLength;
45     }
46 
47      public  boolean isMaxWordLength() {
48          return isMaxWordLength;
49     }
50 }


转自:http://www.cnblogs.com/TerryLiang/archive/2012/10/08/2714918

相关问答

更多
  • lucene怎么用[2022-07-03]

    Lucene是一个全文检索系统框架,开源的。 用起来比较方便,去Lucene的官网上下一个包并导入到你的工程中就可以调用包里面的类了。 一般的书里面介绍的版本都是1.4.3或者稍微高级一点的,不过现在lucene3.0正式发布,一些函数调用方法已经改变了,你可以下载一个版本低一点的Lucene比较适合学习~ 当然你直接从3.0入手的话网上资料也是非常丰富的~
  • StandardAnalyzer在索引期间去除特殊字符。 您可以传入明确的停用词列表(不包括您想要的)。 StandardAnalyzer strips out the special characters during indexing. You can pass in a list of explicit stopwords (excluding the ones you want in).
  • Lucene特定配置尚未在Cypher中公开,因为索引查询语法应该从Cypher API中隐藏。 在以后的版本中,可能有更全面的方法将参数下载到标签的基础索引提供程序。 目前,请使用Java Index API(1.9.X),并构建一个服务器插件,公开您的特殊lucene配置。 The Lucene specific configugrations are not exposed in Cypher as of yet, since the index query syntax should be hidd ...
  • 最后我得到了它的工作: public final class StandardAnalyzerV36 extends Analyzer { public static final CharArraySet STOP_WORDS_SET = StopAnalyzer.ENGLISH_STOP_WORDS_SET; @Override protected TokenStreamComponents createComponents(String fieldName) { ...
  • 分析器旨在将字符串拆分为标记。 SmartChineseAnalyzer将“和试天下”分为“和”,“试”和“天下”,就像StandardAnalyzer将“谁获得世界”分为“谁”,“获取”和“世界”一样。 如果你想搜索一个短语,你的查询应该引用: qp.parse("\"且试天下\""); 如果您不希望分析该字段,请将其索引为StringField (或使用KeywordAnalyzer )。 Analyzers are intended to split strings into tokens. The ...
  • MultiFieldQueryParser允许您在同一个分析器中搜索多个文件中的“WORD”。 例如 Query query = MultiFieldQueryParser.parse("development", new String[]{"title", "subject"}, new SimpleAnalyzer()); 它会在字段中寻找词语发展:“标题”和字段:“主题” MultiFieldQueryParser allows you to search for ...
  • 看起来他们正在移动org.apache.lucene.modules.analysis.standard下的StandardAnalyzer(和相关的东西)。* 我不知道原因。 你可以在这里找到SVN Trunk中的StandardAnalyzer: http : //svn.apache.org/viewvc/lucene/dev/trunk/modules/analysis/common/src/java/org/apache/lucene/analysis/standard/ It looks lik ...
  • 长度归一化因子计算为1 / sqrt(numTerms) (您可以在DefaultSimilarity中看到这一点 此结果不直接存储在索引中。 此值乘以指定字段的提升值。 然后最终结果以8位编码,如Similarity.encodeNorm()中所述。这是一种有损编码,这意味着精细细节会丢失。 如果要查看操作中的长度标准化,请尝试使用以下句子创建文档。 the rolling stones tribute a b c d e f g h i j k 这将在您可以看到的长度标准化值中产生足够的差异。 现在 ...
  • 是的,版本5.0中删除了Version参数。 使用setVersion设置它是正确的。 但是,应该注意的是,设置版本实际上不会做任何事情。 StandardAnalyzer不使用它。 如果要使用旧的StandardAnalyzer逻辑(在3.1及ClassicAnalyzer版本中使用),则应使用ClassicAnalyzer 。 Yes, the Version argument was removed in version 5.0. Setting it using setVersion would b ...
  • 你不能混合和匹配你的lucene版本。 您使用的是4.2.1版。 它与版本3.1.0或3.0.3不兼容。 您需要删除这些依赖项。 WikipediaTokenizer包含在分析器中 - 常见。 此外,您没有履行TokenStream要求的合同。 请参阅TokenStream文档 ,其中描述了TokenStream API的工作流程。 特别是,在调用incrementToken() ,必须调用reset() 。 你应该真的end()和close()它。 WikipediaTokenizer x = wtt.t ...