lucene分词器

lucene分词器

/*
 * 
 * 测试分词器...
 */
public class TestAnalyzer {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		String text="传智播客是一个高端大气上档次的培训机构,谁与争锋...";
		//单字分词...
//		Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_44);
//		二分法分词...
//		CJKAnalyzer analyzer=new CJKAnalyzer(Version.LUCENE_44);
		
		//词库分词... analzyer 是一个抽象类,有没有可能有第三方的jar 包来改造它,实现它
		
//		庖丁分词器,词库分词器
//		优点:可以进行词库分词,自定义停用词,扩展词。
		IKAnalyzer analyzer=new IKAnalyzer();
		
		testAnalyzer(text, analyzer);
	}
	/**
	 * 通过analyzer 切分text 的结果...
	 * @param text
	 * @param analyzer
	 * @throws IOException
	 */
	public static void testAnalyzer(String text,Analyzer analyzer) throws IOException{
		
		System.out.println("当前使用的分词器:" + analyzer.getClass().getSimpleName());
	    TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
	    tokenStream.addAttribute(CharTermAttribute.class);
	    tokenStream.reset();
	    while (tokenStream.incrementToken()) {
	       CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
	          System.out.println(new String(charTermAttribute.toString()));
	    }
	    tokenStream.close();
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值