Click here to Skip to main content
Licence LGPL3
First Posted 24 Feb 2009
Views 73,061
Downloads 495
Bookmarked 37 times

NHunspell - Hunspell for the .NET platform

By | 24 Feb 2009 | Article
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.

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

Member

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionForm Resize issue PinmemberScott Logan1:07 11 Nov '11  
QuestionStrange hang Pinmemberjarofkla4:53 15 Dec '10  
GeneralAFF File not found Pinmemberhemaprathima20:01 24 Nov '10  
General"Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen20:24 13 Oct '10  
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberThomas Maierhofer3:37 14 Oct '10  
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen0:33 18 Oct '10  
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberThomas Maierhofer0:52 18 Oct '10  
GeneralRe: "Stack empty" error every now and then with v0.9.6 PinmemberRune Jacobsen1:03 18 Oct '10  
GeneralNHunspell Version 0.9.5 released PinmemberThomas Maierhofer23:05 18 Jul '10  
GeneralProblem loading the native DLL PinmemberRune Jacobsen1:08 1 Jul '10  
GeneralRe: Problem loading the native DLL PinmemberThomas Maierhofer22:50 18 Jul '10  
GeneralRe: Problem loading the native DLL PinmemberRune Jacobsen23:41 10 Aug '10  
QuestionSom question that i have PinmemberViki Vic22:34 1 Jun '10  
AnswerRe: Som question that i have PinmemberThomas Maierhofer22:59 18 Jul '10  
GeneralProblem exporting solution Pinmemberyunou12:59 13 May '10  
GeneralRe: Problem exporting solution PinmemberThomas Maierhofer23:01 18 Jul '10  
GeneralNot Working in Windows Form Pinmembermouthpiec6:29 7 Mar '10  
GeneralRe: Not Working in Windows Form Pinmembermouthpiec6:47 7 Mar '10  
GeneralRe: Not Working in Windows Form Pinmembermouthpiec7:47 7 Mar '10  
GeneralHunspell class Add method not working Pinmembermrmans0n8:20 23 Feb '10  
GeneralRe: Hunspell class Add method not working PinmemberJBoinker15:02 2 Jun '10  
GeneralRe: Hunspell class Add method not working PinmemberThomas Maierhofer23:02 18 Jul '10  
NewsJust posted an IExtenderProvider using NHunspell [modified] PinmemberWilliam Winner8:34 3 Feb '10  
GeneralNHunspell ExtenderProvider PinmemberWilliam Winner8:55 14 Jan '10  
GeneralRe: NHunspell ExtenderProvider PinmemberThomas Maierhofer21:15 14 Jan '10  

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.

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