这是旧代码在新版本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

Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation的更多相关文章

  1. Lucene分词报错:”TokenStream contract violation: close() call missing”

    Lucene使用IKAnalyzer分词时报错:”TokenStream contract violation: close() call missing”  解决办法是每次完成后必须调用关闭方法. ...

  2. ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked

    ES出现异常: failed to notify ClusterStateListenerjava.lang.IllegalStateException: environment is not loc ...

  3. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  4. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  5. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

  6. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...

  7. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response

    1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...

  8. eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo

    报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...

  9. java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data fr

    Android中操作Sqlite遇到的错误:java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. ...

随机推荐

  1. Python核心技术与实战——八|匿名函数

    今天我们来学习一下匿名函数.在学习了上一节的自定义函数后,是时候了解一下匿名函数了.他们往往非常简短,就一行,而且有个关键字:lambda.这就是弥明函数. 一.匿名函数基础 匿名函数的基本格式是这样 ...

  2. PHP使用HighChart生成股票K线图详解

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  3. 【洛谷P3959】宝藏

    题目大意:比较复杂,点 这里 看题. 题解:对于状态压缩 dp 来讲,阶段的确立十分重要.本题中,采用以层次为阶段进行状压 dp. 设状态 \(f[i][S]\) 表示开凿到深度 \(i\),当前已经 ...

  4. Arduino-数学函数

  5. 使用mongodb

    1.安装 yarn add moogose 2.启动 在命令行 mongod --dbpath D:\data//这是最后出现 waiting for connections on port 2701 ...

  6. 数位dp进阶(hdu2089,3652)

    之前的文章已经讲过如何求1—r中的特殊数,这篇博客就来讲些进阶操作: 直接看例题(hdu2089): (题目是中文的我就不写大意了) 这题与hdu3555最大的区别就是规定了l,不再以1开始: 解决这 ...

  7. 【bzoj4136】[FJOI2015]带子串包含约束LCS问题

    题目描述: 带有子串包含约束的最长公共子序列问题可以具体表述如下. 给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S. S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长 ...

  8. Leetcode 2. Add Two Numbers(指针和new的使用)结构体指针

    ---恢复内容开始--- You are given two non-empty linked lists representing two non-negative integers. The di ...

  9. 6.25考试整理:江城唱晚&&不老梦&&棠梨煎雪——题解

    按照旧例,先安利一下主要作者:一扶苏一 以及扶苏一直挂念的——银临姐姐:银临_百度百科 (滑稽) 好哒,现在步入正题: 先看第一题: 题解: 在NOIP范围内,看到“求方案数”,就说明这个题是一个计数 ...

  10. 在XenCenter6.2中构建CentOS7虚拟机的启动错误

    在XenCenter6.2中创建CentOS7虚拟机时,发现系统并没有提供CentOS7 64bit的模板,只有CentOS6 64bit模板.如果采用CentOS6作为其模板来创建CentOS7虚拟 ...