IKAnalyzer使用不同版本中文分词的切词方式实现相同功能效果

2025-05-29 0 58

最近公司在做一个题库的功能,需要用到 中文分词和公式分词的工具,最开始用 ikanalyzer 2012f 版本 + lunece 6.5.1做了一版中文分词工具。

具体如下:

一、ikanalyzer 2012f + lunece 6.5.1 实现中文分词

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28
public static list<string> analysisbyik(analyzer analyzer,string field, string content){

if(stringutils.isnullorempty(content)){

return null;

}

tokenstream ts = null;

try {

ts = analyzer.tokenstream(field, new stringreader(content));

chartermattribute term = ts.addattribute(chartermattribute.class);

ts.reset();

list<string> vocabularies = new arraylist<>();

while (ts.incrementtoken()) {

vocabularies.add(term.tostring());

}

ts.end();

return vocabularies;

} catch (exception e) {

logger.error(e.getmessage(), e);

} finally {

if (ts != null) {

try {

ts.close();

} catch (ioexception e) {

e.printstacktrace();

}

}

}

return null;

}

调用方式:

?

1

2

3

4
string str = "已知三角形abc中,角a等于角b加角c,那么三角形abc是 a、锐角三角形 b、直角三角形 c、钝角三角形 d、不能确定";

analyzer analyzer = new ikanalyzer(true);

iklist = analysisbyik(analyzer, "myfield", str);

listanalyzer.addall(iklist);

输出结果listanalyzerd

[已知, 三角形, abc, 中, 角, a, 等于, 角, b, 加, 角, c, 那么, 三角形, abc, 是, a, 锐角三角形, b, 直角三角形, c, 钝角三角形, d, 不能, 确定]

但是由于公式切词是 原来公司大牛写的,在满足公式切词的条件下,中文切词的ikanalyzer 2012f与其不兼容。于是尝试其他版本,最终决定用 ikanalyzer 3.2.8 实现了兼容。

二、ikanalyzer 3.2.8 + lunece 3.1.0 兼容版本

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
public static list<string> analysisbyik3point2(analyzer analyzer,string field, string content) throws exception{

if(stringutils.isnullorempty(content)){

return null;

}

list<string> list = new arraylist<>();

reader reader = new stringreader(content);

tokenstream stream = (tokenstream)analyzer.tokenstream(field, reader);

//添加工具类 注意:以下这些与之前lucene2.x版本不同的地方

termattribute termatt = (termattribute)stream.addattribute(termattribute.class);

offsetattribute offatt = (offsetattribute)stream.addattribute(offsetattribute.class);

// 循环打印出分词的结果,及分词出现的位置

while(stream.incrementtoken()){

list.add(termatt.term());

// system.out.println(termatt.term());

}

return list;

}

调用方式:

?

1

2

3

4
string str = "已知三角形abc中,角a等于角b加角c,那么三角形abc是 a、锐角三角形 b、直角三角形 c、钝角三角形 d、不能确定";

analyzer analyzer = new ikanalyzer(true);

iklist = analysisbyik3point2(analyzer, "myfield", str);

listanalyzer.addall(iklist);

输出结果:

[已知, 三角形, abc, 中, 角, a, 等于, 角, b, 加, 角, c, 那么, 三角形, abc, 是, a, 锐角三角形, b, 直角三角形, c, 钝角三角形, d, 不能, 确定]

即使用不同版本实现相同功能效果。 主要是 因为ikanalyzer 2012f 依赖analyzertokenstreamfinal方法,但是公式分词用到的tokensteam方法是抽象方法。两者冲突了,所以考虑去做兼容。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/78645590

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 IKAnalyzer使用不同版本中文分词的切词方式实现相同功能效果 https://www.kuaiidc.com/110569.html

相关文章

发表评论
暂无评论