在建立索引过程中,还有一个非常重要的部分, 就是Analyzer(分词器)。
回想创建IndexWriter对象时,需要一个IndexWriterConfig对象,而这个IndexWriterConfig对象的构造方法,需要一个Analyze对象作为参数。像这样:
Directory directory = FSDirectory.open(new File(indexDir)); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new StandardAnalyzer()); IndexWriter writer = new IndexWriter(directory, config);
本文详细说一下Analyzer的内容。
Analyzer是分词器,负责将一段话分解成为一个个的Token(词,词元,这个概念在自然语言中使用比较广泛)。lucene在建立索引时,记录每个token的内容,并且记录该token在文中的”偏移“。检索时,需要使用建立索引时相同的分词器对输入进行分词,然后lucene就可以根据token的内容及偏移信息,检索出结果。
举个例子:“我爱北京小吃街”。
在比较理想的情况下,可以被分解成”我“、”爱“、”北京“、”小吃街“这几个token。
在建立索引时,索引保存每个token以及每个token在文中的偏移。(假设”我“的偏移为0,”爱“的偏移就为1)。
在检索时,对输入的”北京小吃街“,首先经过分词器处理,得到”北京“和”小吃街“这两个token,并且,计算出这两个token之间的距离为1(两个token之间没有其他token)。通过检索同时含有”北京“和”小吃街“这两个token,并且这两个token之间的距离为1的,就可以找到”我爱北京小吃街“这条记录。
分词器分词过程中完成两个过程:一是去掉StopWords(停用词);二是拆成Token(同时完成名词复数转为单数、动词时态转换等)。
停用词是指在自然语言处理中,没有实际意义的词(比如英文中的a、an、the等),去掉停用词可以提高检索效率。
关于停用词的一个段子:
-如何评价中国移动新品牌「and 和」的名称? -这个名字很好,不管中文还是英文都可以被搜索引擎滤掉。
每种语言都有其特有的停用词表,并且每种语言的语言规则不尽相同,因此,不同的语言有不同的分词器。
一般来说,对英文的分词使用StandardAnalyzer,对中文的分词使用SmartChineseAnalyzer。
同样的文本,使用不同的分词器的效果是不一样的。用户可以自行测试,选择与业务比较贴合的分词器。
如何查看分词器的分词结果呢?
Analyzer中提供有tokenStream方法,其返回值为TokenStream。TokenStream的设计使用了设计模式中的”迭代器模式“和”访问者模式“。通过TokenStream的addAttribute方法,将访问者添加到访问者列表中,并且通过incrementToken方法,进行迭代。
下面提供一个一般的方法,可以观察分词器的分词结果。该方法接收两个参数:第一个参数为使用的分词器,第二个参数为待分词文本。
private void printToken(Analyzer analyzer, String text) throws IOException { TokenStream tokenStream = analyzer.tokenStream("content", text); CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { System.out.println(new String(charTermAttribute.buffer(), 0, charTermAttribute.length())); } tokenStream.end(); }
整个单元测试代码:
package cn.lym.lucene.quickstart.analyzer; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cjk.CJKAnalyzer; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.analysis.en.EnglishAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.junit.Test; public class AnalyzerTest { @Test public void testStandardAnalyzer() throws Exception { StandardAnalyzer analyzer = new StandardAnalyzer(); printToken(analyzer, "An apple a day keeps doctor away"); analyzer.close(); } @Test public void testEnglishAnalyzer() throws Exception { EnglishAnalyzer analyzer = new EnglishAnalyzer(); printToken(analyzer, "An apple a day keeps doctor away"); analyzer.close(); } @Test public void testCJKAnalyzer() throws Exception { CJKAnalyzer analyzer = new CJKAnalyzer(); printToken(analyzer, "我爱北京小吃街"); analyzer.close(); } @Test public void testChineseAnalyzer() throws Exception { SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(); printToken(analyzer, "我爱北京小吃街"); analyzer.close(); } private void printToken(Analyzer analyzer, String text) throws IOException { TokenStream tokenStream = analyzer.tokenStream("content", text); CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { System.out.println(new String(charTermAttribute.buffer(), 0, charTermAttribute.length())); } tokenStream.end(); } }
本文的代码可以从 https://git.oschina.net/coding4j/lucene-quickstart 获得。