http://www.cnblogs.com/huangfox/archive/2012/02/14/2350891.html

lucene版本4.7.0

<dependency><groupId>com.xiaonei</groupId><artifactId>xiaonei-datasource4jade</artifactId>
</dependency>
<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>4.7.0</version></dependency>
<dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-spellchecker</artifactId><version>4.7.0</version>
</dependency>
package com.renren.lbs;import java.io.File;
import java.io.IOException;import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.spell.LuceneDictionary;
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.FSDirectory;
import org.apache.lucene.util.Version;public class SpellCheckDemo {public static void main(String[] args) throws Exception {String dicPath = SpellCheckDemo.class.getResource("/").getPath() + "/dic.txt";File dir = new File("SpellChecker");Directory directory = FSDirectory.open(dir);SpellChecker spellChecker = new SpellChecker(directory);IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, null);spellChecker.indexDictionary(new PlainTextDictionary(new File(dicPath)), config, false);String word = "麻辣酱";int suggestionsNumber = 5;String[] suggestions = spellChecker.suggestSimilar(word, suggestionsNumber);if (suggestions != null && suggestions.length > 0) {for (String suggestion : suggestions) {System.out.println("Did you mean : " + suggestion);}}else {System.out.println("No suggestions found for word : " + word);}}
}

其中dic.txt文件内容为:

麻辣烫
中文测试
麻辣酱
麻辣火锅
中国人
中华人民共和国

运行结果:

Did you mean : 麻辣烫
Did you mean : 麻辣火锅

SpellChecker类有两个目录,一个是字典目录,一个是建立索引的目录。当然这里的代码只是全部使用了默认的相似度。

Did you mean feature with Apache Lucene Spell-Checker相关推荐

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

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

  2. 《深入理解Elasticsearch(原书第2版)》——第2章 查询DSL进阶 2.1 Apache Lucene默认评分公式解释...

    本节书摘来自华章计算机<深入理解Elasticsearch(原书第2版)>一书中的第2章,第2.1节,作者 [美]拉斐尔·酷奇(Rafal Ku)马雷克·罗戈任斯基(Marek Rogoz ...

  3. 【手把手教你全文检索】Apache Lucene初探

    讲解之前,先来分享一些资料 首先呢,学习任何一门新的亦或是旧的开源技术,百度其中一二是最简单的办法,先了解其中的大概,思想等等.这里就贡献一个讲解很到位的ppt.已经被我转成了PDF,便于搜藏. 其次 ...

  4. A SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene40' does not exist.

    简单的建立索引和查询索引并不难,关键在于他的二次开发,让他适合你自己的需求 既然要二次开发就必须查看源码 首先看看索引过程中的核心类吧: IndexWriter 这个是核心组件, 建立和打开索引,以及 ...

  5. 关于org.apache.lucene.queryParser.ParseException: Encountered 解决方法

    现象: org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, col ...

  6. java hibernate错误org/dom4j/DocumentException、org/jboss/logging/BasicLogger、org/apache/lucene/index...

    很久没有看java的,今天运行一个以前的hibernate程序: 先报错,org/dom4j/DocumentException: 根据资料,原因是没有导入 dom4j-1.6.1.jar:下载此ja ...

  7. 使用 Apache Lucene 搜索文本——轻松为应用程序构建搜索和索引功能

    简介: 本文将探讨 Apache Lucene -- 性能卓越.功能全面的文本搜索引擎库.我们将学习 Lucene 架构及其核心 API.学习如何使用 Lucene 进行跨平台全文本搜索.建立索引.显 ...

  8. Apache Lucene与Lucene.Net——全文检索服务器

    lucene学习教程 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来就能用,它只是提供了一 ...

  9. lucene 索引出错 no segments* file found in org.apache.lucene.store.MMapDirectory

    创建第一个lucene索引后,搜索出错: org.apache.lucene.index.IndexNotFoundException: no segments* file found in org. ...

最新文章

  1. android 当前置顶的控件,Android利用RecyclerView实现全选、置顶和拖拽功能示例
  2. 苗旺:因果推断,观察性研究和 2021 年诺贝尔经济学奖
  3. OpenCV实现图像金字塔
  4. 杜恩德的新博客,都来看看
  5. ABAP中有关于换行符,TAB KEY等
  6. nodejs 利用zip-local模块压缩文件夹
  7. Java ServletContextListener监听器的使用
  8. VS2013 使用QCustomPlot等三方库如何配置
  9. Oracle 语句连接字符,oracle拼接字符串当sql语句
  10. linux git添加密钥,centos – 无法在Gitlab上创建项目或添加ssh密钥
  11. gcc 编译多个源文件-转
  12. jdbcdbcpc3p0
  13. webrtc2sip项目说明
  14. 去掉左边0_TiDB 4.0 在 VIPKID 的应用实践
  15. 利用图片指纹检测高相似度图片--相似图片搜索的原理
  16. 中公计算机岗位知识与技能,知识与技能的分类
  17. 一个纸杯子的测试用例
  18. 航空气象:气压与高度观测
  19. python3 scrapy爬虫_Python3 Scrapy爬虫框架(Scrapy/scrapy-redis)
  20. 上海财经应用统计考python_19上海财经大学应用统计专硕考研初试复试帖

热门文章

  1. 宽带的服务器未响应,宽带拨号服务器未响应
  2. Maven从入门到放弃
  3. [HNOI2005]狡猾的商人(差分约束)
  4. 一个屌丝程序猿的人生(一百零五)
  5. 算法:从25匹马中选出最快的三匹马
  6. Linux关机重启命令说明
  7. Android : 破解apk
  8. 教师的常用教学软件_数学教学软件不知道怎么选择?来看看这些软件
  9. Java波斯王子时之沙攻略_《波斯王子:时之砂》剧情攻略
  10. 为什么需要卷积?为什么下采样过程中channels不断增加?