这是旧代码在新版本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. Qt中添加自定义信号和槽带来的一些问题

    背景: 自己定义了一个类,并在类中添加了槽函数 class XImage : public QWidget { public: XImage(QWidget *p = 0); //重载绘制方法 upd ...

  2. Vue this.$nextTick原理

    虽然 Vue.js 通常鼓励开发人员沿着“数据驱动”的方式思考,避免直接接触 DOM,但是有时我们确实要这么做.比如一个新闻滚动的列表项.如果在这里需要操作dom, 应该是等待 Vue 完成更新 DO ...

  3. 闲着没事写点啥之springmvc梳理

    用户通过浏览器向服务器发送请求,请求被springmvc的前端控制器dispatcherservlet拦截. dispatcherservlet拦截到请求后,会调用handlermapping处理器映 ...

  4. git log混乱之混乱操作

    好几个分支 然后就混乱了 git log信息一坨屎 git 删除某次指定的提交 git reset只是在本地仓库中回退版本,而远程仓库的版本不会变化. 以删除master分支为例 #新建一个备份的分支 ...

  5. 主流Linux发行版及联系

    一.主流Linux主流发行版 RedHat:Red Hat Linux是由Red Hat公司发行的一个Linux发行包.其1.0版本于1994年11月3日发行,虽然其历史不及Slackware般悠久, ...

  6. Tomcat与jdk在Linux上的安装与配置

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

  7. STM32CubeMX FreeRTOS no definition for "osThreadGetState" 解决办法

    用STM32CubuMX默认加入的FreeRTOS默认配置eTaskGetState是禁止的 把该功能设为Enabled编译就不会出错了 IAR的编译器要勾选Allow VLA

  8. java编写算法题格式(链表和二叉树)

    (1)链表 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; ...

  9. hive group by distinct区别以及性能比较

    Hive去重统计 相信使用Hive的人平时会经常用到去重统计之类的吧,但是好像平时很少关注这个去重的性能问题,但是当一个表的数据量非常大的时候,会发现一个简单的count(distinct order ...

  10. Oracle中的rowid rownum

    1. rowid和rownum都是虚列 2. rowid是物理地址,用于定位oracle中具体数据的物理存储位置 3. rownum则是sql的输出结果排序,从下面的例子可以看出其中的区别. rowi ...