IKAnalyzer2012FF + Lucene4.9 TokenStream contract violation: reset()/close() call missing

IKAnalyzer2012FF + Lucene4.9 TokenStream contract violation: reset()/close() call missing异常信息如下:

大家好,又见面了,我是你们的朋友全栈君。

异常信息如下:

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:111)
	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 unit.test.IKAnalyzerTest.test01(IKAnalyzerTest.java:29)
	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:601)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:243)
	at junit.framework.TestSuite.run(TestSuite.java:238)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

代码:

public void test01() throws IOException {
<span style="white-space:pre">	</span>String text="基于java语言开发的轻量级的中文分词工具包,一个轻量级框架!"; 
		
        // 创建分词对象  
        Analyzer analyzer = new IKAnalyzer(true);       
        StringReader reader = new StringReader(text);  
        
        // 分词  
        TokenStream ts = analyzer.tokenStream("", reader);  
        CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);  
        
        // 遍历分词数据  
        while(ts.incrementToken()){  
            System.out.print(term.toString()+" | ");  
        }  
        
        reader.close();  
        System.out.println();  
}

上面的代码为旧的分词步骤,按照新的API,调用TokenStream的流程如下:

1、Instantiation of TokenStream/TokenFilters which add/get attributes to/ the AttributeSource.
2、The consumer calls reset().
3、The consumer retrieves attributes 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(),如下面的第10行代码:

public void test01() throws IOException {
		String text="基于java语言开发的轻量级的中文分词工具包,一个轻量级框架!"; 
		
        // 创建分词对象  
        Analyzer analyzer = new IKAnalyzer(true);       
        StringReader reader = new StringReader(text);  
        
        // 分词  
        TokenStream ts = analyzer.tokenStream("", reader); 
        ts.reset();
        CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);  
        
        // 遍历分词数据  
        while(ts.incrementToken()){  
            System.out.print(term.toString()+" | ");  
        }  
        
        reader.close();  
        System.out.println();  
	}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163106.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PAT乙级——1003

    PAT乙级——1003题目:我要通过!(20分)“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送——只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。得到“答案正确”的条件是:字符串中必须仅有P、A、T这三种字符,不可以包含其它字符;任意形如xPATx的字符串都可以获得“答案正确”,其中x或者是空字符串,或者是仅由字母A…

    2022年5月22日
    33
  • C++:cstdio 头文件详解

    C++:cstdio 头文件详解&lt;cstdio&gt;(stdio.h)headerC库执行输入/输出操作:输入和输出操作也可以在C++实现,通过使用C标准输入和输出库(cstdio,在C语言中称为stdio.h)。这个库使用流来操作物理设备如键盘,打印机,终端或者系统支持的任何其他类型的文件。流是一种以统一的方式与这些交互的抽象; 所有流都具有相似的属性,与它们所关联的物理介质的各个特征无关。流…

    2025年8月9日
    2
  • java 中级面试题_java中级面试题[通俗易懂]

    java 中级面试题_java中级面试题[通俗易懂]本帖最后由唯我独赞mo于2015-11-1222:47编辑1、java中wait和sleep有什么区别?多线程条件下如何保证数据安全?答:最大区别是等待时wait会释放锁,而sleep会一直持有锁,wait通常用于线程时交互,sleep通常被用于暂停执行。2、java中volatile和synchronized有什么区别?1.volatile本质是在告诉jvm当前变量在寄存器(工作内存…

    2022年10月12日
    5
  • 更改文字、图片和视频大小(缩放)

    更改文字、图片和视频大小(缩放)

    2022年2月9日
    118
  • 网卡绑定模式bond0(多个网卡bond)

    在现在的网络中,带宽越来越高,线路的带宽可以达到1000m的带宽,但是想要达到整体性能达到1000m的带宽却很难,因为网络i/o限制着,无法整体达到这么高的带宽,甚至有时以前买的服务器网卡带宽不咋地,导致整个网络的带宽无法提升。但是linux的bond模块和ifenslave网卡聚合工具可以解决这一问题。利用bond模块连接内核实现双网卡通信,使用ifens…

    2022年4月10日
    162
  • 中文写代码?开始不信后来用中文写了剧情小游戏!嗯,真香~

    中文写代码?开始不信后来用中文写了剧情小游戏!嗯,真香~你还不知道可以用中文编写脚本制作游戏?那还不赶紧点进来看看~

    2022年6月16日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号