这是旧代码在新版本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)
at org.wltea.analyzer.core.AnalyzeContext.fillBuffer(AnalyzeContext.java:124)
at org.wltea.analyzer.core.IKSegmenter.next(IKSegmenter.java:122)
at org.wltea.analyzer.lucene.IKTokenizer.incrementToken(IKTokenizer.java:78)
at com.hankcs.train.IKHelper.parse(IKHelper.java:36)
at com.hankcs.train.AnalysisAdjuster.handleFile(AnalysisAdjuster.java:44)
at com.hankcs.train.AnalysisAdjuster.main(AnalysisAdjuster.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Process finished with exit code 1

旧代码:

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 new TokenStream API is as follows:

  1. Instantiation of TokenStream/TokenFilters which add/get attributes to/from the AttributeSource.

  2. The consumer calls reset().

  3. The consumer retrieves attributes from the stream and stores local references to all attributes it wants to access.

  4. The consumer calls incrementToken() until it returns false consuming the attributes after each call.

  5. The consumer calls end() so that any end-of-stream operations can be performed.

  6. The consumer calls close() to release any resource when finished using the TokenStream.

所以代码必须在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();
        }

转载请注明:码农场 » Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation

最新文章

  1. tesseract 编译与使用(windows)
  2. Oracle 记录插入时“Invalid parameter binding ”错误
  3. a标签加入单击事件 屏蔽href跳转页面
  4. 数据库逆向框架代码生成工具:MyBatis Generator的使用
  5. distribution数据库过大问题
  6. js之规范代码写法
  7. 发短信的主要代码(SmsManger)
  8. ecstore生成二维码
  9. Request.Params用法
  10. perl 读取cookie
  11. js实时监听input中值的变化
  12. 【转】并行类加载——让tomcat玩转双十一 @双十一实战
  13. 12.python进程\协程\异步IO
  14. 使用CreateFile, ReadFile, WriteFile在Windows NT/2000/XP下读写绝对扇区的方法
  15. 想不到的:js中加号操作符
  16. Nginx+Tomcat整合的安装与配置(win.linux)
  17. “finally block does not complete normally”的警告解决
  18. nvidia Compute Capability(GPU)
  19. angularjs写公共方法
  20. beego跨域请求配置

热门文章

  1. linux下vim如何清空一个文件?
  2. PHP file函数
  3. mysql 分页查询及优化
  4. 原生JS 将时间转换成几秒前,几分钟前…常用于评论回复功能
  5. android 7.0适配(总结)
  6. Java——对象转型
  7. [luogu]P1053 篝火晚会[数学][群论]
  8. 【BZOJ5092】分割序列(高维前缀和)
  9. Android 快速索引(城市列表和联系人)
  10. [BZOJ1547]周末晚会:Burnside引理+DP