lucene java maven_Lucene 6.5.0整合IKAnalyzer至Maven

本文介绍了如何将IKAnalyzer 6.5.0与Lucene 6.5.0整合到Maven项目中,包括下载IKAnalyzer jar包,通过命令行将其安装到本地Maven仓库,以及在pom.xml中添加依赖。同时,针对Lucene 4.6.0以上版本可能出现的TokenStream异常,提供了修复代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.   下载IKAnalyzer6.5.0.jar

度盘链接:http://pan.baidu.com/s/1jH4NY66

2.   打开cmd, 执行以下命令手动将jar包安装到本地maven仓库:

mvn install:install-file -Dfile=C:\Users\Karn\Desktop\大三下\信息检索\Project\IKAnalyzer6.5\IKAnalyzer6.5.0.jar-DgroupId=com.lucene -DartifactId=ikAnalyzer -Dversion=6.5.0 -Dpackaging=jar-DgeneratePom=true

其中,-Dfile=jar包的完整路径。我的jar包存放路径是C:\Users\Karn\Desktop\大三下\信息检索\Project\IKAnalyzer6.5\IKAnalyzer6.5.0.jar

3.   安装完成后,在pom.xml里加上以下依赖代码即可:

com.lucene

ikAnalyzer

6.5.0

注意:

在lucene 4.6.0以上版本使用IKAnalyzer时可能会出现以下异常:

java.lang.illegalstateexception:tokenstream contract violation: reset()/close() call missing, reset() calledmultiple times, or subclass does not call super.reset(). please see javadocs oftokenstream class for more information about the correct consuming workflow.

发现是lucene从4.6.0开始tokenstream使用方法更改的问题,在使用incrementtoken方法前必须调用reset方法,详见api http://lucene.apache.org/core/4_6_0/core/index.html

theworkflow of the new tokenstream api is as follows:

1.     instantiationof tokenstream/tokenfilters which add/get attributes to/fromthe attributesource.

2.     the consumercalls reset().

3.     the consumer retrievesattributes from the stream and stores local references to all attributes itwants to access.

4.     the consumercalls incrementtoken() until it returns false consuming theattributes after each call.

5.     the consumercalls end() so that any end-of-stream operations can be performed.

6.     the consumercalls close() to release any resource when finished usingthe tokenstream.

更改代码为如下运行正常

IKAnalyzer analyzer = newIKAnalyzer(true);

StringReader reader = new StringReader(news);

TokenStream ts = analyzer.tokenStream("", reader);

CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);

ts.reset();

while(ts.incrementToken()){

System.out.print(term.toString()+"|");

}

analyzer.close();

reader.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值