package IkLucene;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IKAnalyzerTest {
public static void main(String[] args) {
String keyWord = "自动化物流系统,规划,管理流程,效益";
//创建IKAnalyzer中文分词对象
IKAnalyzer analyzer = new IKAnalyzer();
// 使用智能分词
analyzer.setUseSmart(true);
// 打印分词结果
try {
printAnalysisResult(analyzer, keyWord);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印出给定分词器的分词结果
*
* @param analyzer
* 分词器
* @param keyWord
* 关键词
* @throws Exception
*/
private static void printAnalysisResult(Analyzer analyzer, String keyWord)
throws Exception {
System.out.println("["+keyWord+"]分词效果如下");
TokenStream tokenStream = analyzer.tokenStream("content",
new StringReader(keyWord));
tokenStream.addAttribute(CharTermAttribute.class);
tokenStream.reset(); //新版中需要有这么个reset,不可遗漏!
while (tokenStream.incrementToken()) {
CharTermAttribute charTermAttribute = tokenStream
.getAttribute(CharTermAttribute.class);
System.out.println(charTermAttribute.toString());
}
}
}
上面代码中keyword不管怎么变,结果却都是一样的。求帮忙看看错哪了?