Lucene使用IKAnalyzer分詞時報錯:」TokenStream contract violation: close() call missing」 解決辦法是每次完成後必須調用關閉方法。html
若是報錯:java.lang.illegalstateexception: tokenstream contract violation: reset()/close() call missing,則要在tokenStream.incrementToken(),緣由是lucene從4.6.0開始tokenstream使用方法更改的問題,在使用incrementtoken方法前必須調用reset方法,詳見api http://lucene.apache.org/core/4_6_0/core/index.html 。java
如下正確示例代碼(第10行和22行調用reset()和close()方法):apache
public Set<String> slicing(String text){ Set<String> result = new HashSet<>(); StringReader reader = null; TokenStream tokenStream = null; try { reader = new StringReader(text); tokenStream = analyzer.tokenStream("", reader); CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class); OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { int startOffset = offsetAttribute.startOffset(); int endOffset = offsetAttribute.endOffset(); if((endOffset - startOffset) > 1){ String term = charTermAttribute.toString(); result.add(term); } } } catch (IOException e) { e.printStackTrace(); } finally{ IOs.close(tokenStream, reader); } return result; }
http://www.lizi.pw/archives/56api
org.wltea.analyzer.lucene.IKAnalyzereclipse
Exception in thread "main" java.lang.IllegalStateException: 詞典還沒有初始化,請先調用initial方法 at org.wltea.analyzer.dic.Dictionary.getSingleton(Dictionary.java:137) at org.wltea.analyzer.core.CJKSegmenter.analyze(CJKSegmenter.java:80) at org.wltea.analyzer.core.IKSegmenter.next(IKSegmenter.java:116) at org.wltea.analyzer.lucene.IKTokenizer.incrementToken(IKTokenizer.java:88)