lucene之旅(三十一)——SpellChecker上

本文介绍了Lucene中SpellChecker组件的使用方法,包括错别字库的导入、错别字列表的导出及不同距离算法的应用场景。通过实例演示了如何配置并利用SpellChecker进行拼写纠错。

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

SpellChecker是Lucene的一个新组件,至少对我这个一年内没在动Lucene的人来说。
群里有人讨论SpellChecker,便去研究了一下,还不是很难。
SpellChecker就纠错不错,但也有人用来做相关搜索,但效果应该不太理想。
好,言归正传。先来个实例:
第一步导入错别字库:

SpellChecker sc=new SpellChecker(FSDirectory.getDirectory("book.index")); sc.indexDictionary(new PlainTextDictionary(new File("spell.txt")));

第二步就是导出错别字列表

String[] strs=sc.suggestSimilar("明朝那点屎", 3); for (int i = 0; i < strs.length; i++) { System.out.println(strs[i]); }

= =!,有点恶心。不过还是看一下输出结果。

明朝那点事

忘了介绍一个文件就是错别字文件,可以用索引创建也可以用文本创建,咱们先来简单的也就是

大家看到的PlainTextDictionary,其中spell.txt的内容如下

明朝那点事 明朝五好家庭 明朝的皇帝

可能大家会输入明朝会怎么样,答案是没结果,因为

public SpellChecker(Directory spellIndex) throws IOException { this(spellIndex, new LevensteinDistance()); }

将LevensteinDistance是默认的,要想输入明朝也提示出来,那么改为JaroWinklerDistance

SpellChecker sc=new SpellChecker(FSDirectory.getDirectory("book.index"),new JaroWinklerDistance()); sc.indexDictionary(new PlainTextDictionary(new File("spell.txt")));

JaroWinklerDistance是有个类似相似度的参数,默认是0.7,可以通过setThreshold,进行设置。

那么总结一下总共三个元素SpellChecker,StringDistance,Dictionary

StringDistance接口的实现类有JaroWinklerDistance和LevensteinDistance

Disctionary接口的实现类有LuceneDictionary和PlainTextDictionary

JaroWinklerDistance主要可以用来相关搜索

LevensteinDistance用来纠错

PlainTextDictionary文本格式

LuceneDictionary索引格式

LuceneDictionary的结构如下

IndexStructure

Example

word

kings

gram3

kin, ing, ngs

gram4

king, ings

start3

kin

start4

king

end3

ngs

end4

ings

应用的话就讲到这里,其中的算法,我们在下篇再讲

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值