Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation
Please read full article from Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation
这是旧代码在新版本Lucene中出现的异常,异常如下:
Exception in thread "main" 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.
at org.apache.lucene.analysis.Tokenizer$1.read(Tokenizer.java:110)
at java.io.Reader.read(Reader.java:140)
旧代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| IKAnalyzer ss = new IKAnalyzer(); StringReader reader = new StringReader(str); try { TokenStream tokenStream = ss.tokenStream( "" , reader); while (tokenStream.incrementToken()) { CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute. class ); System.out.println(termAttribute.toString()); } } catch (IOException e) { e.printStackTrace(); } |
根据新的API文档,调用TokenStream API的流程必须是:
The workflow of the newTokenStream
API is as follows:
The consumer callsreset()
. The consumer retrieves attributes from the stream and stores local references to all attributes it wants to access. The consumer callsincrementToken()
until it returns false consuming the attributes after each call. The consumer callsend()
so that any end-of-stream operations can be performed. The consumer callsclose()
to release any resource when finished using theTokenStream
.
所以代码必须在
incrementToken()
之前调用一次reset()
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| IKAnalyzer ss = new IKAnalyzer(); StringReader reader = new StringReader(str); try { TokenStream tokenStream = ss.tokenStream( "" , reader); tokenStream.reset(); while (tokenStream.incrementToken()) { CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute. class ); System.out.println(termAttribute.toString()); } } catch (IOException e) { e.printStackTrace(); } |
No comments:
Post a Comment