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

如果报错:java.lang.illegalstateexception: tokenstream contract violation: reset()/close() call missing,则要在tokenStream.incrementToken(),原因是lucene从4.6.0开始tokenstream使用方法更改的问题,在使用incrementtoken方法前必须调用reset方法,详见api http://lucene.apache.org/core/4_6_0/core/index.html 。

以下正确示例代码(第10行和22行调用reset()和close()方法):

public Set<String> slicing(String text){
Set<String> result = new HashSet<>();
StringReader reader = null;
TokenStream tokenStream = null;
try {
reader = new StringReader(text);
tokenStream = analyzer.tokenStream("", reader);
CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
tokenStream.reset();
while (tokenStream.incrementToken()) {
int startOffset = offsetAttribute.startOffset();
int endOffset = offsetAttribute.endOffset();
if((endOffset - startOffset) > 1){
String term = charTermAttribute.toString();
result.add(term);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
IOs.close(tokenStream, reader);
}
return result;
}

http://www.lizi.pw/archives/56

org.wltea.analyzer.lucene.IKAnalyzer

Exception in thread "main" java.lang.IllegalStateException: 词典尚未初始化,请先调用initial方法
at org.wltea.analyzer.dic.Dictionary.getSingleton(Dictionary.java:137)
at org.wltea.analyzer.core.CJKSegmenter.analyze(CJKSegmenter.java:80)
at org.wltea.analyzer.core.IKSegmenter.next(IKSegmenter.java:116)
at org.wltea.analyzer.lucene.IKTokenizer.incrementToken(IKTokenizer.java:88)

Lucene分词报错:”TokenStream contract violation: close() call missing”的更多相关文章

  1. Lucene 4.6.1 java.lang.IllegalStateException: TokenStream contract violation

    这是旧代码在新版本Lucene中出现的异常,异常如下: Exception in thread "main" java.lang.IllegalStateException: To ...

  2. mysql报错sql injection violation, syntax error: syntax error, expect RPAREN, actual IDENTIFIER

    mysql报错sql injection violation, syntax error: syntax error, expect RPAREN, actual IDENTIFIER 处理,在控制台 ...

  3. 修改umask后apache报错:because search permissions are missing on a component of the path,

    0.修改umask后apache报错:because search permissions are missing on a component of the path, 1.ls -lrth ./h ...

  4. Jfinal报错sql injection violation, multi-statement not allow

    Jfinal报错: com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLException: sql injection ...

  5. 安卓模拟器的报错This AVD's configuration is missing a kernel file!!

    安卓模拟器的报错: 可能的原因是target设置问题:

  6. idea中 参数没有描述报错 @param XX tag description is missing错误,去除黄色警告

    最近在使用idea开发工具,在方法备注中参数没有描述报错就会报一些黄色警告: @param XX tag description is missing,下面展示去除黄色警告的方法 File--sett ...

  7. [已解决]报错:Required request body is missing

    问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...

  8. DELLR720服务器更换硬盘,启动系统报错:there are offline or missing virtual drivers with preserved cache

    linux系统启动过程中给出错误: There are offline or missing virtual drives with preserved cache. Please check the ...

  9. Glibc编译报错:*** These critical programs are missing or too old: as ld gcc

    Binutils版本升级 这里是binutils版本过低导致, 查看已部署版本 上传离线升级包 [root@sdw1 glibc]# tar -zxvf binutils-2.32.tar.gz [r ...

随机推荐

  1. 4、python基本知识点及字符串常用方法

    查看变量内存地址   id(变量名) ni = 123 n2 = 123 ni和n2肯定是用的两份内存,但是python对于数字在-5~257之间的数字共用一份地址,范围可以修改 name = ‘李璐 ...

  2. 程序员的底色(IDE color scheme、CLI 命令行界面)

    1. IDE ⇒ Dracula(吸血鬼) IDE:PyCharm,VS2013: sublime:color scheme,Monokai: 2. CLI 命令行界面 $ setterm -inve ...

  3. Altium Designer画原理图时要紧凑

    之所以要紧凑,是为了方便打印到纸上,一般原理图也都用A4纸去画, 这样打印到纸上看起来不会太小,也不会太大.

  4. 【57.97%】【codeforces Round #380A】Interview with Oleg

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. [ACM] ural 1057 Amount of degrees (数位统计)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  6. 算法 Tricks(六)— if 条件分支的简化

    考虑下面的三分支的定义式: f=⎧⎩⎨⎪⎪a,b,a+b,x>yx<yx=y int f = 0; if (x >= y) f += a; if (x <= y) f += b ...

  7. Normal Equation of Computing Parameters Analytically

    Normal Equation Note: [8:00 to 8:44 - The design matrix X (in the bottom right side of the slide) gi ...

  8. 选择标识符(identifier)

    整数通常是标识列最好的选择,因为它们很快并且可以使用auto_increment:千万不要使用enum和set类型作为标识列:尽量避免使用字符串类型作为标识列,因为他们很消耗空间,并且通常比数字类型慢 ...

  9. springMVC注解@initbinder

    在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 比 ...

  10. centos7安装nginx的两种方法

    第一种方式:通过yum安装 直接通过 yum install nginx 肯定是不行的,因为yum没有nginx,所以首先把 nginx 的源加入 yum 中 运行下面的命令: 1.将nginx放到y ...