C# Lucene.Net spellchecker

核能气质少年 提交于 2021-02-07 10:17:34

问题


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???


回答1:


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();


来源:https://stackoverflow.com/questions/16586212/c-sharp-lucene-net-spellchecker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!