最近开始研究Lucene,对里面的分词器进行进行了一下测试
代码
public class TestAnalyzer02 {
private static String chinese = "Lucene全文搜索引擎的学习";
private Analyzer analyzer = null;
@Test
public void testStandarAnalyzer() throws Exception{
analyzer = new StandardAnalyzer(Version.LUCENE_36);
testAnalyzer(analyzer, chinese);
}
@Test
public void testSimpleAnalyzer() throws Exception{
analyzer = new SimpleAnalyzer(Version.LUCENE_36);
testAnalyzer(analyzer, chinese);
}
@Test
public void testCJKAnalyzer() throws Exception{
analyzer = new CJKAnalyzer(Version.LUCENE_36);
testAnalyzer(analyzer, chinese);
}
@Test
public void testIKAnalyzer() throws Exception{
analyzer = new IKAnalyzer();
testAnalyzer(analyzer, chinese);
}
/**
* 使用指定的分词器对指定的文本进行分词,并打印出分出的词
*
* @param analyzer
* @param text
* @throws Exception
*/
private void testAnalyzer(Analyzer analyzer, String text) throws Exception {
System.out.println("分词器:" + analyzer.getClass().getSimpleName());
TokenStream tokenStream = analyzer.tokenStream("content",
new StringReader(text));
tokenStream.addAttribute(TermAttribute.class);
while (tokenStream.incrementToken()) {
TermAttribute termAttribute = tokenStream
.getAttribute(TermAttribute.class);
System.out.println(termAttribute.term());
}
System.out.println();
}
}
测试结果
分词器:StandardAnalyzer
lucene
全
文
搜
索
引
擎
的
学
习
分词器:SimpleAnalyzer
lucene全文搜索引擎的学习
分词器:CJKAnalyzer
lucene
全文
文搜
搜索
索引
引擎
擎的
的学
学习
分词器:IKAnalyzer
加载扩展停止词典:stopword.dic
lucene
全文
搜索引擎
搜索
索引
引擎
的
学习