2019独角兽企业重金招聘Python工程师标准>>>

org.apache.lucene.search.spell 
Class SpellChecker

java.lang.Object  org.apache.lucene.search.spell.SpellChecker

Lucene拼写检查类

使用例子:

 SpellChecker spellchecker = new SpellChecker(spellIndexDirectory);// To index a field of a user index:spellchecker.indexDictionary(new LuceneDictionary(my_lucene_reader, a_field));// To index a file containing words:spellchecker.indexDictionary(new PlainTextDictionary(new File("myfile.txt")));String[] suggestions = spellchecker.suggestSimilar("misspelt", 5);

SpellChecker有三个构造方法,可以根据给定的Directory实例创建SpellChecker对象进行后续操作;

PlainTextDictionary实现了Dictionary接口,并提供3个构造方法,参数分别为:File、InputStream、Reader

上面例子中根据一个文本文件创建PlainTextDirectory字典,该文本文件的格式为每一行包含一个词,如:

word1
word2
word3

其他:FileDictionary, HighFrequencyDictionary, LuceneDictionary

SpellChecker方法:

String [] suggestSimilar(String word,int numSug)

参数:

word-需要检查的词

numSug-返回的suggest词数

其他的:String [] suggestSimilar(...),可以根据精度等进行,详情请参考官方文档;

完整代码示例:

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.spell.PlainTextDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class SpellCheckerTest {private static String filepath = "C:\\Users\\Mr_Tank_\\Desktop\\BaseTest\\dictionaryfile.txt";private Document document;private Directory directory;private IndexWriter indexWriter;private SpellChecker spellchecker;private IndexReader indexReader;private IndexSearcher indexSearcher;private IndexWriterConfig getConfig() {return new IndexWriterConfig(Version.LUCENE_43, new IKAnalyzer(true));}private IndexWriter getIndexWriter() {directory = new RAMDirectory();try {return new IndexWriter(directory, getConfig());} catch (IOException e) {e.printStackTrace();return null;}}/*** Create index for test** @param content* @throws IOException*/public void createIndex(String content) {indexWriter = getIndexWriter();document = new Document();document.add(new TextField("content", content, Field.Store.YES));try {indexWriter.addDocument(document);indexWriter.commit();indexWriter.close();} catch (IOException e) {e.printStackTrace();}}public ScoreDoc[] gethits(String content) {try {indexReader = DirectoryReader.open(directory);indexSearcher = new IndexSearcher(indexReader);QueryParser parser = new QueryParser(Version.LUCENE_43, "content", new IKAnalyzer(true));Query query = parser.parse(content);TopDocs td = indexSearcher.search(query, 1000);return td.scoreDocs;} catch (Exception e) {e.printStackTrace();return null;}}/*** @param scoreDocs* @return* @throws IOException*/public List<Document> getDocumentList(ScoreDoc[] scoreDocs) throws IOException {List<Document> documentList = null;if (scoreDocs.length >= 1) {documentList = new ArrayList<Document>();for (int i = 0; i < scoreDocs.length; i++) {documentList.add(indexSearcher.doc(scoreDocs[i].doc));}}return documentList;}public String[] search(String word, int numSug) {directory = new RAMDirectory();try {spellchecker = new SpellChecker(directory);spellchecker.indexDictionary(new PlainTextDictionary(new File(filepath)), getConfig(), true);return getSuggestions(spellchecker, word, numSug);} catch (IOException e) {e.printStackTrace();return null;}}private String[] getSuggestions(SpellChecker spellchecker, String word, int numSug) throws IOException {return spellchecker.suggestSimilar(word, numSug);}public static void main(String[] args) throws IOException {SpellCheckerTest spellCheckerTest = new SpellCheckerTest();spellCheckerTest.createIndex("开源中国-找到您想要的开源项目,分享和交流");spellCheckerTest.createIndex("CSDN-全球最大中文IT社区");String word = "开园中国";/*ScoreDoc[] scoreDocs = spellCheckerTest.gethits(word);List<Document> documentList = spellCheckerTest.getDocumentList(scoreDocs);if (documentList.size() >= 1) {for (Document d : documentList) {System.out.println("搜索结果:" + d.get("content"));}}*/String[] suggest = spellCheckerTest.search(word, 5);if (suggest != null && suggest.length >= 1) {for (String s : suggest) {System.out.println("您是不是要找:" + s);}} else {System.out.println("拼写正确");}}
}

dictionaryfile.txt:

中华人民共和国
开源中国
开源社区
Lucene
拼写检查
Lucene4.3.1

转载于:https://my.oschina.net/tanweijie/blog/194046

Lucene4.3.1 拼写检查SpellChecker相关推荐

  1. 介绍 Java 平台的 Jazzy:一种新的拼写检查器 API

    计算机擅长执行快速搜索操作,可以根据给定的搜索词,对大量存储的信息快速进行搜索.但是,拼写检查应用程序所要求的搜索能力,不仅仅是正确的字符串匹配.在这篇文章中,我将介绍搜索算法的一些历史,包括语音匹配 ...

  2. solr创建索引_Solr:创建拼写检查器

    solr创建索引 在上一篇文章中,我谈到了Solr Spellchecker的工作原理,然后向您展示了其性能的一些测试结果. 现在,我们将看到另一种拼写检查方法. 与其他方法一样,此方法使用两步过程. ...

  3. apache lucene_Apache Lucene拼写检查器的“您是不是要”功能

    apache lucene Google的"您是不是要"功能 在上一篇文章中对Lucene进行了介绍之后 ,现在是时候提高它并创建一个更复杂的应用程序了. 您肯定最熟悉Google ...

  4. Solr:创建拼写检查器

    在上一篇文章中,我谈到了Solr Spellchecker的工作原理,然后向您展示了其性能的一些测试结果. 现在,我们将看到另一种拼写检查方法. 与其他方法一样,此方法使用两步过程. 相当快速的&qu ...

  5. Apache Lucene拼写检查器的“您是不是要”功能

    Google的"您是不是要"功能 在上一篇文章中对Lucene进行了介绍之后 ,现在是时候提高它,创建一个更复杂的应用程序了. 您肯定最熟悉Google的"您是不是要&q ...

  6. LeetCode 966. 元音拼写检查器(哈希)

    1. 题目 在给定单词列表 wordlist 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词. 对于给定的查询单词 query,拼写检查器将会处理两类拼写错误: 大小写:如果查询匹配 ...

  7. 拼写检查工具是android,拼写检查工具框架  |  Android 开发者  |  Android Developers...

    Android 平台提供了拼写检查工具框架,可让您在应用中实现和使用拼写检查功能.该框架是 Android 平台提供的文本服务 API 之一. 如需在您的应用中使用该框架,您需要创建一个特殊类型的 A ...

  8. Solr拼写检查示例

    本文英文版地址:https://examples.javacodegeeks.com/enterprise-java/apache-solr/solr-spellcheck-example/ 作者姓名 ...

  9. idea spell checking(拼写检查)

    在idea中写一些没有在词库中出现的单词会报弱警告,如下: Spellchecker inspection helps locate typos and misspelling in your cod ...

最新文章

  1. 零基础入门学习Python(34) 面向对象
  2. java设计模式概述
  3. ES6中解构赋值深入解读
  4. nginx哪个版本性能好_nginx性能为什么好
  5. 餐厅数据分析报告_如何使用数据科学选择理想的餐厅设计场所
  6. UVa10006-Carmichael Numbers
  7. linux 串口信息记到日志,[linux学习笔记]之一:ubuntu ch340调试备忘
  8. for循环与each遍历的跳出循环方式
  9. java许愿墙_18.JavaScript实现许愿墙效果
  10. iOS键盘遮挡输入框,输入区域自动上移
  11. 如何root安卓手机_如何从我的字体里面提取TTF并阉割成未Root安卓手机能用的?...
  12. html当当图书榜页面,2019书排行榜_当当网图书排行榜
  13. Python正态性检验
  14. 北京科技大学计算机复试面试,北京科技大学考研复试
  15. 分别用Java应用程序和Applet程序实现星星三角形图案的绘制
  16. 一些国内可用的高质量壁纸网站,免翻~
  17. Python销售管理系统
  18. 国泰煤炭运销管理系统
  19. 【bat批处理脚本命令】2分钟看懂,一键主机windows系统远程连接控制阿里云等windows系统的云服务器(保姆级图文+实现代码)
  20. CPU负载与CPU使用率

热门文章

  1. anaconda python命令_Anaconda常用命令
  2. python进制转换内置函数_python数学运算、逻辑运算和进制转化相关的 内置函数...
  3. ds18b20温度传感器 lcd C语言,C程序控制18B20测温及LCD显示源码打包
  4. 计算机专业读mba,计算机专业的学生适合读MBA吗MBA考试_MBA-教育宝
  5. 【Go】从键盘输入字符串和数字
  6. 计算机的来源知识,如何理解计算机知识及计算机发展史
  7. 清空list_3. Python3轻食丨一个故事看懂List所有用法:1年级1班的班级生活
  8. 错误:返回局部变量数组名 \ 解决方案
  9. MyBatis 源码分析 - 缓存原理
  10. IaaS,PaaS,SaaS 的区别