C# Lucene.Net 拼写检查器

标签 c# lucene.net spell-checking

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

最佳答案

您将需要搜索它,它无法执行此操作,因为拼写检查工作在一个单独的索引上,该索引未链接到您创建建议的主索引。

这很容易做到:

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 拼写检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586212/

相关文章:

c# - 显式检索 XML 元素的内容作为字符串

java - 最好的 Java 拼写检查 API

lucene - 为 solr 构建案例

mongodb - 请告知mongoDB中全文搜索的最佳解决方案

intellij-idea - 如何在 intellij 中更快地纠正拼写错误?

ios - UITextView 自定义拼写和自动更正

c# - 当 URL 包含缩略图时,Blob 触发器绑定(bind)应排除

C# - 等待复制操作完成

c# - 具有单个数据源的多个组合框

c# - Lucene:对同一文档多次调用 UpdateDocument 会导致分数不断增加