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)
上一篇 2022年7月22日 下午2:00
下一篇 2022年7月22日 下午2:00


相关推荐

  • 0x80 的具体解释

    0x80 的具体解释0x800x 是 C 语言中 16 进制数的表示方法 0x80 等于十进制的 1280×80 在计算机内部表示为字符在计算机中以其 ASCII 码方式表示 nbsp 其长度为 1 个字节 有符号字符型数取值范围为 128 127 无符号字符型数到值范围是 0 255 因此在 TurboC 语言中 字符型数据在操作时将按整型数处理 如果某个变量定义成 char 则表明该变量是

    2026年3月17日
    2
  • ShuffleNet v2算法笔记

    ShuffleNet v2算法笔记论文 ShuffleNetV2 PracticalGui 论文链接 https pan baidu com s 1so7aD3hLKO 0PB8h4HWliw 这篇是 ECCV2018 关于模型加速和压缩的文章 是之前 ShuffleNet 的升级版 这篇文章的观点和实验都比较新颖 看完还是有不少收获的 特来

    2026年3月16日
    2
  • Hunyuan-MT-7B部署教程:vLLM –enable-prefix-caching提升长文档重复翻译速度

    Hunyuan-MT-7B部署教程:vLLM –enable-prefix-caching提升长文档重复翻译速度

    2026年3月13日
    3
  • offsetHeight, clientHeight与scrollHeight的区别

    offsetHeight, clientHeight与scrollHeight的区别在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论。以下结论皆是在标准模式下测试通过的,没有测试quirk模式。clientHeight大部分浏览器对clientHeight都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,即然是指可看到内容的区域,滚动条不算在内。但要注意padding是算在内。其计算方式

    2022年7月23日
    14
  • 「7年了!GTA 5联机版加载还是这么慢??一个if语句循环了19.8亿次??你的CPU在抽烟」

    「7年了!GTA 5联机版加载还是这么慢??一个if语句循环了19.8亿次??你的CPU在抽烟」你以为我上GitHub就是在学习?你以为我上GTA5就一定是在玩游戏?「7年了!GTA5联机版加载还是这么慢??」别急先献上地址https://github.com/tostercx/GTAO_Booster_PoC详情下面慢慢去了解只需要如下操作:gitclone—recurse-submoduleshttps://github.com/tostercx/GTAO_Booster_PoC之后,把dll文件粘贴到游戏根目录下就OK!彻底提升启动速度70%△Pleasewaitfor

    2022年4月30日
    211
  • html静态网页制作教程_简单html静态网页代码 首页

    html静态网页制作教程_简单html静态网页代码 首页编辑一个文本文件,代码如下;<!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>htmltest1</title></head><body><h1>DDDD</h1><p>PPPPPPPPPPP…</p><tableborder=”1″>

    2025年8月27日
    10

发表回复

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

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