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();
}
}