Spellcheck是Lucene新版本的功能,在介绍spellcheck之前,我们需要弄清楚Spellcheck支持几种数据源。Spellcheck构造函数需要传入Dictionary接口:

  

package org.apache.lucene.search.spell;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ import java.io.IOException; import org.apache.lucene.search.suggest.InputIterator; /**
* A simple interface representing a Dictionary. A Dictionary
* here is a list of entries, where every entry consists of
* term, weight and payload.
*
*/
public interface Dictionary { /**
* Returns an iterator over all the entries
* @return Iterator
*/
InputIterator getEntryIterator() throws IOException;
}

  常用的Dictionary主要有以下几种,常用的主要有基于文本型的和基于lucene索引构建的:

  

  下面是我测试用的一段代码,代码包括索引构建和索引查询:

  

package com.tianditu.com.search;

import java.io.File;
import java.io.IOException; import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.spell.LuceneDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.MMapDirectory;
import org.apache.lucene.util.Version; public class GlobalSuggest { //拼写检查构建的索引
private final String SPELL_CHECK_FOLDER = "c:\\spellcheck\\"; //根据已有的索引
private final String GLOBAL_PINYIN_SUGGEST = "O:\\searchwork_custom\\data_index\\pinyin2008\\"; //构建索引
public void testIndexPinyin2008() throws IOException{ long start = System.currentTimeMillis();
//北京吉威时代软件股份有限公司
//String indexDir ="O:\\searchwork_custom\\data_index\\GlobalIndex\\";
Directory direct = new MMapDirectory(new File(GLOBAL_PINYIN_SUGGEST)); LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name"); ld.getEntryIterator(); Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER)); SpellChecker sc = new SpellChecker(spd);
//sc.in IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null); //往spellcheck目录下写索引--------------
sc.indexDictionary(ld, iwc, true); sc.close(); long end = System.currentTimeMillis();
System.out.println("索引完毕,耗时:"+(end-start)+"ms");
} public void testIndex() throws IOException{
long start = System.currentTimeMillis();
//北京吉威时代软件股份有限公司
String indexDir ="O:\\searchwork_custom\\data_index\\GlobalIndex\\";
Directory direct = new MMapDirectory(new File(indexDir)); LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name"); ld.getEntryIterator(); Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER)); SpellChecker sc = new SpellChecker(spd);
//sc.in IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null); sc.indexDictionary(ld, iwc, true); sc.close(); long end = System.currentTimeMillis();
System.out.println("索引完毕,耗时:"+(end-start)+"ms");
} public void testSearch(String wd) throws IOException{ //构建Directory
Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER)); //实例化 spellcheck组件 SpellChecker sc = new SpellChecker(spd); //根据输入关键字 获得N条最相近的几率 第三个鄙视精确度 越大越匹配 安装实际需要调整
String[] suggests = sc.suggestSimilar(wd, 10,0.6f);
if(suggests!=null){
for(String word:suggests){
System.out.println("Dou you mean:"+word);
}
}
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException { GlobalSuggest spellcheck = new GlobalSuggest();
//spellcheck.testIndexPinyin2008();
spellcheck.testSearch("beijing京鸭"); //spellcheck.testSearch("beijng"); } }

  其中索引构建处代码:

  

	//构建索引
public void testIndexPinyin2008() throws IOException{ long start = System.currentTimeMillis();
//北京吉威时代软件股份有限公司
//String indexDir ="O:\\searchwork_custom\\data_index\\GlobalIndex\\";
Directory direct = new MMapDirectory(new File(GLOBAL_PINYIN_SUGGEST)); LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name"); ld.getEntryIterator(); Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER)); SpellChecker sc = new SpellChecker(spd);
//sc.in IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null); //往spellcheck目录下写索引--------------
sc.indexDictionary(ld, iwc, true); sc.close(); long end = System.currentTimeMillis();
System.out.println("索引完毕,耗时:"+(end-start)+"ms");
}

  此处代码,就是根据已有的索引来构建Spellcheck所需的索引。

Spellcheck查询索引代码片段如下:

  

//构建Directory
Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER)); //实例化 spellcheck组件 SpellChecker sc = new SpellChecker(spd); //根据输入关键字 获得N条最相近的几率 第三个鄙视精确度 越大越匹配 安装实际需要调整
String[] suggests = sc.suggestSimilar(wd, 10,0.6f);
if(suggests!=null){
for(String word:suggests){
System.out.println("Dou you mean:"+word);
}
}

 相关算法:默认是 LevensteinDistance 。

     

 查询样例:

    1、查询汉字,有错别字情况:

    

    2、查询拼音:

    

    3、拼音汉字夹杂:

    

(备注:发现问题了,拼音和汉字夹杂的情况不行,如果想使用,需要进行某种处理。)

    4、如果处理一长串汉字,中间夹杂错别字:

    

  总结:看来spellcheck能力还是有限,如果需要用还可能改造。

      

  

  

  

    

Lucene 4.x Spellcheck使用说明的更多相关文章

  1. lucene字典实现原理

    http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到该te ...

  2. lucene字典实现原理——FST

    转自:http://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找到 ...

  3. Elasticsearch .Net Client NEST使用说明 2.x

    Elasticsearch .net client NEST使用说明 2.x Elasticsearch.Net与NEST是Elasticsearch为C#提供的一套客户端驱动,方便C#调用Elast ...

  4. Lucene 02 - Lucene的入门程序(Java API的简单使用)

    目录 1 准备环境 2 准备数据 3 创建工程 3.1 创建Maven Project(打包方式选jar即可) 3.2 配置pom.xml, 导入依赖 4 编写基础代码 4.1 编写图书POJO 4. ...

  5. Elasticsearch .net client NEST使用说明 2.x -更新版

    Elasticsearch .net client NEST使用说明 目录: Elasticsearch .net client NEST 5.x 使用总结 elasticsearch_.net_cl ...

  6. solr5.3的spellcheck功能

    1.增加schema.xml中的检查字段. <field name="title" type="text_cn" indexed="true&q ...

  7. solr特点四: SpellCheck(拼写检查)

    接下来,我将介绍如何向应用程序添加 “您是不是要找……”(拼写检查). 提供拼写建议 Lucene 和 Solr 很久以前就开始提供拼写检查功能了,但直到添加了 SearchComponent架构之后 ...

  8. lucene字典实现原理(转)

    原文:https://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找 ...

  9. Atitit.项目修改补丁打包工具 使用说明

    Atitit.项目修改补丁打包工具 使用说明 1.1. 打包工具已经在群里面.打包工具.bat1 1.2. 使用方法:放在项目主目录下,执行即可1 1.3. 打包工具的原理以及要打包的项目列表1 1. ...

随机推荐

  1. Wrangle – 响应式的,触摸友好的多选插件

    Wrangle 是一个响应式,触摸友好的选择插件,支持 jQuery 以及 Zepto.Wrangle 为多项选择提供了一个独特的方法:通过画一条贯穿项目的线条来选择项目.它给你的应用程序的一种新的方 ...

  2. SVG Drawing Animation - SVG 绘制动画

    一个小实验,探索 SVG 线图动画的使用情况,以前沿的展示形式呈现图形或网站元素的外观,模拟它们的加载.SVG 真的很强大,有许多创造性和可能性,使用 SVG 可以制作各种有趣的网站交互效果.今天这篇 ...

  3. 向 Web 开发人员推荐35款 JavaScript 图形图表库

    图表是数据图形化的表示,通过形象的图表来展示数据,比如条形图,折线图,饼图等等.可视化图表可以帮助开发者更容易理解复杂的数据,提高生产的效率和 Web 应用和项目的可靠性. 在这篇文章中,我们收集了3 ...

  4. 用纯CSS创建一个三角形

    原理:把上.左.右三条边隐藏掉(颜色设为 transparent) #demo { width:; height:; border-width: 20px; border-style: solid; ...

  5. Window对象

    Window对象:         Window 对象表示浏览器中打开的窗口,如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框 ...

  6. 原创:phoenix4.6.0连接hbase1.1.2(不使用phoenix-4.6.0-HBase-1.1-client.jar)

    官网上面的例子是在phoenix-4.6.0-HBase-1.1-client.jar完成的,这个jar包含了phoenix4.6连接hbase1.1.2所有的依赖,真是包罗万象(里面竟然还包括了se ...

  7. JavaScript学习08 Cookie对象

    JavaScript学习08 Cookie对象 JavaScript Cookie Cookie对象: Cookie是一种以文件的形式保存在客户端硬盘的Cookies文件夹中的用户数据信息(Cooki ...

  8. iOS开发之多媒体API (转载)

    视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类.尽管后者在播放的稳定性和播放画面质量上可能没有前者 优秀,但网络流媒体影像视频的广泛传播性使之正被广泛应用于视频 ...

  9. c中的数据类型、常量、变量

    一. 数据 1. 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文 ...

  10. 使用Android Annotations开发

    使用Android Annotations框架gradle配置1.修改Module下的build.gradle apply plugin: 'com.android.application' appl ...