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

NHunspell - Hunspell for the .NET platform

By , 24 Feb 2009
 

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.

Links

History

  • 2009-02-24:- Release of 0.5.1 Beta

License

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

About the Author

Thomas Maierhofer
CEO MSE-iT
Germany Germany
I'm the CEO of MSE-iT Reisebürosoftware, a software development company in Germany making travel agency accounting software.
 
We release some library stuff under GPL or LGPL, so everybody can use it. These are our Open-Source projects so far:
 
Spell Checking, Hyphenation and Thesaurus for .NET

.NET spell checker, hyphenation and thesaurus based on the Open Offlice spell checker Hunspell. Here is a life Demo: Spell check, hyphenation and thesaurus reference project for NHunspell on ASP.NET.
 
jQuery Plugins

jQuery Background Canvas Plugin Inserts a HTML5 CANVAS element behind any HTML element and allows to draw on it with JavaScript.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCan we get grammar suggestions using this library?memberNitin Sawant29-Nov-12 18:56 
AnswerRe: Can we get grammar suggestions using this library?memberThomas Maierhofer18-Mar-13 11:32 
QuestionForm Resize issuememberScott Logan11-Nov-11 1:07 
QuestionStrange hangmemberjarofkla15-Dec-10 4:53 
GeneralAFF File not foundmemberhemaprathima24-Nov-10 20:01 
General"Stack empty" error every now and then with v0.9.6memberRune Jacobsen13-Oct-10 20:24 
GeneralRe: "Stack empty" error every now and then with v0.9.6memberThomas Maierhofer14-Oct-10 3:37 
GeneralRe: "Stack empty" error every now and then with v0.9.6memberRune Jacobsen18-Oct-10 0:33 
GeneralRe: "Stack empty" error every now and then with v0.9.6memberThomas Maierhofer18-Oct-10 0:52 
GeneralRe: "Stack empty" error every now and then with v0.9.6memberRune Jacobsen18-Oct-10 1:03 
GeneralNHunspell Version 0.9.5 releasedmemberThomas Maierhofer18-Jul-10 23:05 
GeneralProblem loading the native DLLmemberRune Jacobsen1-Jul-10 1:08 
GeneralRe: Problem loading the native DLLmemberThomas Maierhofer18-Jul-10 22:50 
GeneralRe: Problem loading the native DLLmemberRune Jacobsen10-Aug-10 23:41 
QuestionSom question that i havememberViki Vic1-Jun-10 22:34 
AnswerRe: Som question that i havememberThomas Maierhofer18-Jul-10 22:59 
GeneralProblem exporting solutionmemberyunou13-May-10 12:59 
GeneralRe: Problem exporting solutionmemberThomas Maierhofer18-Jul-10 23:01 
GeneralNot Working in Windows Formmembermouthpiec7-Mar-10 6:29 
GeneralRe: Not Working in Windows Formmembermouthpiec7-Mar-10 6:47 
GeneralRe: Not Working in Windows Formmembermouthpiec7-Mar-10 7:47 
GeneralHunspell class Add method not workingmembermrmans0n23-Feb-10 8:20 
GeneralRe: Hunspell class Add method not workingmemberJBoinker2-Jun-10 15:02 
GeneralRe: Hunspell class Add method not workingmemberThomas Maierhofer18-Jul-10 23:02 
NewsJust posted an IExtenderProvider using NHunspell [modified]memberWilliam Winner3-Feb-10 8:34 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 24 Feb 2009
Article Copyright 2009 by Thomas Maierhofer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid