Lucene分词器测试

最近开始研究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
全文
搜索引擎
搜索
索引
引擎

学习



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值