最近公司在做一个题库的功能,需要用到 中文分词和公式分词的工具,最开始用 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
依赖analyzer
的tokenstream
是final
方法,但是公式分词用到的tokensteam
方法是抽象方法。两者冲突了,所以考虑去做兼容。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/moneyshi/article/details/78645590
相关文章
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 48
-
2025-05-27 82
-
瞄准Linux系统!攻击者改造渗透测试工具Cobalt Strike兼容Linux信标
2025-05-27 32 -
阿里云和华为云自助建站的扩展性:未来业务增长的需求能否满足?
2025-05-25 36 -
2025-05-26 80