Tuesday, February 24, 2009

Hunspell (Open Office’s Spell Checker) wrapped for .Net = NHunspell – Your LGPL spell checker, hyphenation library for .Net

CodeProject - NHunspell - Hunspell for the .NET plattform

“…

I was looking for a good spell checker and hyphenation library for .NET, and I found the free (LGPL licensed) Hunspell spell checker and Hyphen libraries used in OpenOffice. Hunspell wasn't available for the .NET platform. So, I decided to write a wrapper/port. It is quite nice that a lot of the OpenOffice dictionaries are LGPL licensed too and can be used in proprietary applications.

Interop code to the native Hunspell functions

I used Managed C++ to write the wrapper/port, because I could use the original source code of Hunspell and Hyphen. It was quite nice to write the interop code between managed classes and the unmanaged Hunspell and Hyphen libraries. The original source code is almost unchanged, so that new versions of Hunspell or Hyphen can be easily adopted. Hunspell and Hyphen use unmanaged memory functions, so I had to implement the IDisposable interface and used this pattern to free unmanaged memory early.

Because Hunspell uses UTF8 coding, I had to provide conversion functions from/to UTF8:

NHunspell spell checking and hyphenation sample

This is a short demo of how to use NHunspell for spell checking, suggestions, and hyphenation:

using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic"))
{
Console.WriteLine("Check if the word 'Recommendation' is spelled correct");
bool correct = hunspell.Spell("Recommendation");
Console.WriteLine("Recommendation is spelled " +
(correct ? "correct" : "not correct"));

Console.WriteLine("");
Console.WriteLine("Make suggestions for the word 'Recommendatio'");
List<string> suggestions = hunspell.Suggest("Recommendatio");
Console.WriteLine("There are " + suggestions.Count.ToString() +
" suggestions" );
foreach (string suggestion in suggestions)
{
Console.WriteLine("Suggestion is: " + suggestion );
}
}


…”

I don’t remember the exact project, but I remember looking for a good, free, easy to deploy spell checker for .Net and I think I’ve now just found it. :)

4 comments:

Thomas Maierhofer - MSE-iT said...

Hello Greg, thank you for your post, i will try to add more functionality to NHunspell. The project itself is hostet on Sourceforge at:
http:://nhunspell.sourceforge.net

Kind Regards, Thomas

Unknown said...

Hello Greg!
I have downoaded nhunspell and tried to implement .dll file in C#
by adding reference. I encountered the following problem plz help me

Could not load file or assembly 'NHunspell, Version=0.5.2.42123, Culture=neutral, PublicKeyToken=null' or one of its dependencies. This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)

waiting for your reply

Greg said...

@miru
I've not yet had a chance to add this to one of my projects so I have to say that the best bet to get support for this would probably be to contact the project team directly via the CodeProject Page or the Project's SourceForge site...

Anonymous said...

Download latest version from sourceforge and add reference to dll NHunspell.dll also put Hunspellx86.dll in your execution folder for 32 bit or Hunspellx86.dll
in your execution folder for 64 bit