Lucene.NET Did you mean? - easy how to…
Sunday, May 10th, 2009This is actually very simple to do and you can get it done in about 20 mins! I did a bit of Googling and could not find much about it so I thought that I should share my findings.
First of all you need an additional assembly called SpellChecker.Net. This assembly is a part of lucene.net and can be found in the contrib directory. I checked it out from SVN here: https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/C#/contrib/SpellChecker.Net
Just open the solution and then build it in release configuration. That’s it - you now have the SpellChecker dll you need.
Now all you have to do is add a reference to this in the project that you have lucene etc (you get what I am on about I am sure).
Ok so now for the fun bit - actually using it! This is simpler than you might expect. In my case I added it to my SearchEngine class. This may be the right or wrong thing to do but for now it felt right - I perform a search on the engine - why not ask it for the suggestions too?
Ok so here is the code:
private void createSuggestionList()
{
var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(_searcher.Reader.Directory());
spellChecker.IndexDictionary(new LuceneDictionary(_searcher.Reader, "title"));
_suggestions = spellChecker.SuggestSimilar(_originalSearchTerm, 5);
}
Ok so let me explain:
- Firstly we create an instance of the spell checker and pass in a directory. ‘_searcher’ is an instance of ‘IndexSearcher’ (you should be aware of one of these are if you are using Lucene)
- Then we tell the spellChecker which index to read and which field to gather its spelling suggestions from
- All we have to do now is grab an array of strings from a search term
I told you it was easy - enjoy….