C#Lucene.Net拼写检查器 [英] C# Lucene.Net spellchecker

查看:57
本文介绍了C#Lucene.Net拼写检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向用户提供数据的网站.我想使用Lucene.Net进行自动填充.问题是我希望能够返回纠正拼写错误的结果.我发现Lucene.Net具有拼写检查器功能,可以建议其他单词.但是它返回了单词,我需要ID才能获得该项目的更多信息.从拼写检查器得到结果后,我是否必须对常规索引进行其他查询?还是有更好的方法呢???

解决方案

您将需要搜索它,但是它不能做到这一点,因为拼写检查适用于一个单独的索引,该索引未链接到您从中创建建议的主要索引./p>

这很容易做到:

  RAMDirectory dir = new RAMDirectory();IndexWriter iw =新的IndexWriter(dir,新的StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),IndexWriter.MaxFieldLength.UNLIMITED);文档d =新Document();Field textField = new Field("text",",Field.Store.YES,Field.Index.ANALYZED);d.Add(textField);字段idField = new Field("id",",Field.Store.YES,Field.Index.NOT_ANALYZED);d.Add(idField);textField.SetValue(这是一个带有一些单词的文档");idField.SetValue("42");iw.AddDocument(d);iw.Commit();IndexReader reader = iw.GetReader();SpellChecker.Net.Search.Spell.SpellChecker拼写=新的SpellChecker.Net.Search.Spell.SpellChecker(新的RAMDirectory());speller.IndexDictionary(new LuceneDictionary(reader,"text"));字符串[]建议= speller.SuggestSimilar("dcument",5);IndexSearcher searcher =新的IndexSearcher(阅读器);foreach(建议中的字符串建议){TopDocs docs = searcher.Search(new TermQuery(new Term("text",建议)),null,Int32.MaxValue);foreach(docs.ScoreDocs中的var doc){Console.WriteLine(searcher.Doc(doc.Doc).Get("id"));}}reader.Dispose();iw.Dispose(); 

I have a site that give data to the user. I want to use Lucene.Net for my autocomplete. The thing is I want to be able to return results that correct spelling errors. I see that Lucene.Net has a spellchecker functionality that suggest other words. But it returns the words and I need the Ids in order to get more info of that item. Do I have to do another query on the regular index after I get results from the spellchecker or is there a better way???

解决方案

You will need to search for it, it cannot do it since spellchecking works on a separate index that is not linked to you main index your created suggestions from.

Its easy to do tho:

RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);

Document d = new Document();
Field textField = new Field("text", "", Field.Store.YES, Field.Index.ANALYZED);
d.Add(textField);
Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
d.Add(idField);

textField.SetValue("this is a document with a some words");
idField.SetValue("42");
iw.AddDocument(d);

iw.Commit();
IndexReader reader = iw.GetReader();

SpellChecker.Net.Search.Spell.SpellChecker speller = new SpellChecker.Net.Search.Spell.SpellChecker(new RAMDirectory());
speller.IndexDictionary(new LuceneDictionary(reader, "text"));
string [] suggestions = speller.SuggestSimilar("dcument", 5);


IndexSearcher searcher = new IndexSearcher(reader);
foreach (string suggestion in suggestions)
{
    TopDocs docs = searcher.Search(new TermQuery(new Term("text", suggestion)), null, Int32.MaxValue);
    foreach (var doc in docs.ScoreDocs)
    {
        Console.WriteLine(searcher.Doc(doc.Doc).Get("id"));
    }
}

reader.Dispose();
iw.Dispose();

这篇关于C#Lucene.Net拼写检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆