lucene 获取分词后的关键字,该如何解决
1个回答
展开全部
String keyWord = "java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。";
IKAnalyzer analyzer = new IKAnalyzer();
System.out.println("分词:"+keyWord);
try {
TokenStream tokenStream = analyzer.tokenStream("content",
new StringReader(keyWord));
tokenStream.addAttribute(CharTermAttribute.class);
tokenStream.reset();//必须先调用reset方法,否则会报下面的错,可以参考TokenStream的API说明
/* java.lang.IllegalStateException: TokenStream contract violation: reset()/close() call missing, reset() called multiple times, or subclass does not call super.reset(). Please see Javadocs of TokenStream class for more information about the correct consuming workflow.*/
System.out.print("结果:");
while (tokenStream.incrementToken()) {
CharTermAttribute charTermAttribute = (CharTermAttribute) tokenStream
.getAttribute(CharTermAttribute.class);
System.out.print(charTermAttribute.toString() + " ");
}
tokenStream.end();
tokenStream.close();
} catch(Exception e) {
e.printStackTrace();
}
lucene 4.9.0
ikanalyzer 2012FF_u1
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询