lucene拼写检查模块

Lucene是Apache发布的开源搜索引擎开发工具包,不仅提供了核心的搜索功能,还提供了许多其他功能插件,例如:拼写检查功能模块。

搜索拼写检查模块实现类在lucene-suggest-x.xx.x.jar包中,package名为org.apache.lucene.search.spell,其中拼写检查功能的核心实现有3个类,

分别为:SpellCheckerDirectSpellCheckerWordBreakSpellChecker;

3个类提供了不同的拼写检查方式,区别如下:

SpellChecker:提供了原始的拼写检查功能,在拼写检查前需要重新建立索引(根据txt字典文件建立索引或者已有索引文件的某个字段建立索引),然后才可以进行拼写检查;

SpellChecker源码分析查看如下网站:http://www.tuicool.com/articles/naIBjm

DirectSpellChecker:提供了改进的拼写检查功能,可以直接利用已有索引文件进行拼写检查,不需要重新建立索引(solr系统默认采用此种方式进行拼写检查);

WordBreakSpellChecker:也不需要重新建索引,可以利用已有索引进行拼写检查。

SpellChecker使用:

建立索引有三种方式:

PlainTextDictionary:用txt文件初始化索引

LuceneDictionary:用现有索引的某一个字段初始化索引

HighFrequencyDictionary:用现有索引的某个字段初始化索引,但每个索引条目必须满足一定的出现率

 1 //新索引目录
 2 String spellIndexPath = “D:\newPath”;
 3 //已有索引目录
 4 String oriIndexPath = "D:\oriPath";
 5 //字典文件
 6 String dicFilePath = “D:\txt\dic.txt”;
 7 
 8 //目录
 9 Directory directory = FSDirectory.open((new File(spellIndexPath)).toPath());
10 
11 SpellChecker spellChecker = new SpellChecker(directory);
12 
13 //以下几步用来初始化索引
14 IndexReader reader = DirectoryReader.open(FSDirectory.open((new File(oriIndexPath)).toPath()));
15 //利用已有索引
16 Dictionary dictionary = new LuceneDictionary(reader, fieldName);
17 //或者利用txt字典文件
18 //Dictionary dictionary = new PlainTextDictionary((new File(dicFilePath)).toPath());
19 IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
20 spellChecker.indexDictionary(dictionary, config, true);
21 
22 String queryWord = "beijink";
23 int numSug = 10;
24 //拼写检查
25 String[] suggestions = spellChecker.suggestSimilar(queryWord, numSug);
26 
27 reader.close();
28 spellChecker.close();
29 directory.close();

DirectSpellChecker使用:

1 DirectSpellChecker checker = new DirectSpellChecker();
2 String readerPath = "D:\path";
3 IndexReader reader = DirectoryReader.open(FSDirectory.open(
4                     (new File(readerPath)).toPath()));
5 Term term = new Term("fieldname", "querytext");
6 int numSug = 10;
7 SuggestWord[] suggestions = checker.suggestSimilar(term, numSug, reader);
原文地址:https://www.cnblogs.com/blog-zuo/p/4819983.html