Click here to Skip to main content
Click here to Skip to main content

NHunspell - Hunspell for the .NET platform

, 21 Jul 2014 LGPL3
Rate this:
Please Sign up or sign in to vote.
The spell checking and hyphenation features of OpenOffice for the .NET platform.

Introduction

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:

char * NHunspell::MarshalHelper::AllocUTF8FromString(String ^value)
{
    array<Byte> ^ byteArray = Encoding::UTF8->GetBytes(value);
    int size = Marshal::SizeOf(byteArray[0]) * (byteArray->Length + 1);
    IntPtr buffer = Marshal::AllocHGlobal(size);
    Marshal::Copy(byteArray, 0, buffer, byteArray->Length);
    Marshal::WriteByte(buffer, size - 1, 0);
    return (char *) buffer.ToPointer();
}

String ^ NHunspell::MarshalHelper::AllocStringFromUTF8( char * value )
{
    int size = strlen(value);
    array<Byte> ^ byteArray = gcnew array<Byte>(size);
    Marshal::Copy(IntPtr(value), byteArray, 0, size);
    return Encoding::UTF8->GetString(byteArray);
}

Another big thing is to handle the unmanaged memory. I implement destructors and finalizers to deal with this:

NHunspell::Hunspell::~Hunspell()
{
    this->!Hunspell();
}

NHunspell::Hunspell::!Hunspell()
{
    if( handle != 0 )
    {
        delete handle;
        handle = 0;
    }
}

bool NHunspell::Hunspell::IsDisposed::get()
{
    return handle == 0;
}

NHunspell spell checking and hyphenation sample

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

Console.WriteLine("NHunspell functions and classes demo");

Console.WriteLine("");
Console.WriteLine("Spell Check with with Hunspell");

// Important: Due to the fact Hunspell will use unmanaged memory
// you have to serve the IDisposable pattern
// In this block of code this is be done
// by a using block. But you can also call hunspell.Dispose()
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 );
    }
}

Console.WriteLine("");
Console.WriteLine("Hyphenation with Hyph");

// Important: Due to the fact Hyphen will use unmanaged
// memory you have to serve the IDisposable pattern
// In this block of code this is be done by a using block.
// But you can also call hyphen.Dispose()
using (Hyphen hyphen = new Hyphen("hyph_en_us.dic"))
{
    Console.WriteLine("Get the hyphenation of the word 'Recommendation'"); 
    HyphenResult hyphenated = hyphen.Hyphenate("Recommendation");
    Console.WriteLine("'Recommendation' is hyphenated as: " + 
                      hyphenated.HyphenatedWord ); 
}

Console.WriteLine("");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

Because Hunspell is native C++ code, you must include the correct assembly for your platform. On x86 platforms (32 bit), use the NHunspell.dll from the X86 folder. On X64 platforms, use the NHunspell.dll from the X64 folder.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework. Please take a look at it: Crawler-Lib Homepage
 
I have also a YouTube channel for this: Crawler-Lib YouTube Channel

Comments and Discussions

 
QuestionCan we get grammar suggestions using this library? PinmemberNitin Sawant29-Nov-12 18:56 
AnswerRe: Can we get grammar suggestions using this library? PinmemberThomas Maierhofer18-Mar-13 11:32 
QuestionForm Resize issue PinmemberScott Logan11-Nov-11 1:07 
QuestionStrange hang Pinmemberjarofkla15-Dec-10 4:53 
I am attempting to use NHunspell 0.9.6 within an ASP.NET web application deployed underneath a SharePoint 2007 installation running on a Windows 2003 x64 server farm. I have so far gotten it to work but I have noticed that after about a few calls to the DLL, the application begins to hang. If I comment out my calls to the Hunspell object, the hang goes away. I am calling Dispose() on the Page.Unload() event and I hoped that would release the resource, but this problem won't go away. In the process explorer I see that the IE8 process status is waiting on something.
 
Has anyone else experienced this? My guess has something to do with the x64 environment, but I don't have anything to base this on.
Randy

GeneralAFF File not found Pinmemberhemaprathima24-Nov-10 20:01 
General"Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen13-Oct-10 20:24 
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberThomas Maierhofer14-Oct-10 3:37 
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen18-Oct-10 0:33 
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberThomas Maierhofer18-Oct-10 0:52 
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen18-Oct-10 1:03 
GeneralNHunspell Version 0.9.5 released PinmemberThomas Maierhofer18-Jul-10 23:05 
GeneralProblem loading the native DLL PinmemberRune Jacobsen1-Jul-10 1:08 
GeneralRe: Problem loading the native DLL PinmemberThomas Maierhofer18-Jul-10 22:50 
GeneralRe: Problem loading the native DLL PinmemberRune Jacobsen10-Aug-10 23:41 
QuestionSom question that i have PinmemberViki Vic1-Jun-10 22:34 
AnswerRe: Som question that i have PinmemberThomas Maierhofer18-Jul-10 22:59 
GeneralProblem exporting solution Pinmemberyunou13-May-10 12:59 
GeneralRe: Problem exporting solution PinmemberThomas Maierhofer18-Jul-10 23:01 
GeneralNot Working in Windows Form Pinmembermouthpiec7-Mar-10 6:29 
GeneralRe: Not Working in Windows Form Pinmembermouthpiec7-Mar-10 6:47 
GeneralRe: Not Working in Windows Form Pinmembermouthpiec7-Mar-10 7:47 
GeneralHunspell class Add method not working Pinmembermrmans0n23-Feb-10 8:20 
GeneralRe: Hunspell class Add method not working PinmemberJBoinker2-Jun-10 15:02 
GeneralRe: Hunspell class Add method not working PinmemberThomas Maierhofer18-Jul-10 23:02 
NewsJust posted an IExtenderProvider using NHunspell [modified] PinmemberWilliam Winner3-Feb-10 8:34 
GeneralNHunspell ExtenderProvider PinmemberWilliam Winner14-Jan-10 8:55 
GeneralRe: NHunspell ExtenderProvider PinmemberThomas Maierhofer14-Jan-10 21:15 
GeneralRe: NHunspell ExtenderProvider PinmemberWilliam Winner26-Jan-10 12:50 
GeneralRe: NHunspell ExtenderProvider PinmemberWilliam Winner26-Jan-10 13:15 
NewsTwo new NHunspell Articles for Version 0.9.2 and above are available PinmemberThomas Maierhofer16-Dec-09 1:54 
GeneralProblem when loading a dictionary... PinmemberKnight_Rider23-Nov-09 6:10 
GeneralRe: Problem when loading a dictionary... PinmemberThomas Maierhofer23-Nov-09 6:30 
GeneralRe: Problem when loading a dictionary... PinmemberKnight_Rider23-Nov-09 9:06 
GeneralRe: Problem when loading a dictionary... PinmemberThomas Maierhofer23-Nov-09 19:37 
GeneralRe: Problem when loading a dictionary... [modified] PinmemberAleksei Krassovskikh13-Feb-10 9:13 
GeneralNHunspell 0.9.2 is relesed - Thesaurus is available PinmemberThomas Maierhofer27-Oct-09 21:02 
GeneralNHunspell as ASP.NET App: http://www.spell-check-thesaurus.com/ PinmemberThomas Maierhofer26-Oct-09 10:09 
GeneralNHunspell Development Status Report: Stemming, Thesaurus, multi Threading, other improvements PinmemberThomas Maierhofer20-Oct-09 2:10 
GeneralDictionaries PinmemberThomas Maierhofer20-Oct-09 2:03 
QuestionSimulating OpenOffice PinmemberAnt210022-Jun-09 4:16 
AnswerRe: Simulating OpenOffice PinmemberThomas Maierhofer24-Jun-09 23:58 
GeneralRe: Simulating OpenOffice PinmemberAnt210025-Jun-09 4:21 
AnswerRe: Simulating OpenOffice [modified] PinmemberWilliam Winner3-Feb-10 9:02 
GeneralRe: Simulating OpenOffice PinmemberAnt21003-Feb-10 9:04 
GeneralNHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files PinmemberThomas Maierhofer28-May-09 22:22 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files PinmemberCCPOSTON11-Aug-09 11:39 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files PinmemberThomas Maierhofer11-Aug-09 21:51 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files PinmemberCCPOSTON12-Aug-09 4:24 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files PinmemberMember 654289929-Sep-09 21:05 
GeneralNHunspell 0.6.0 released PinmemberThomas Maierhofer27-Apr-09 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 21 Jul 2014
Article Copyright 2009 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid