Click here to Skip to main content
15,887,450 members
Articles / Programming Languages / C#

NHunspell - Hunspell for the .NET platform

Rate me:
Please Sign up or sign in to vote.
4.73/5 (23 votes)
21 Jul 2014LGPL31 min read 271.7K   2.9K   60   107
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:

MC++
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:

MC++
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:

C#
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)


Written By
CEO
Germany Germany
I'm a Senior Software Consultant
Thomas Maierhofer Consulting

Comments and Discussions

 
GeneralRe: Greek language Pin
Thomas Maierhofer (Tom)20-Mar-09 22:57
Thomas Maierhofer (Tom)20-Mar-09 22:57 
GeneralRe: Greek language Pin
zissop20-Mar-09 23:48
zissop20-Mar-09 23:48 
GeneralRe: Greek language Pin
Thomas Maierhofer (Tom)21-Mar-09 2:30
Thomas Maierhofer (Tom)21-Mar-09 2:30 
GeneralRefactoring of NHunspell Pin
Thomas Maierhofer (Tom)15-Mar-09 23:05
Thomas Maierhofer (Tom)15-Mar-09 23:05 
GeneralProblem with spanish dictionary Pin
Eugenio Serrano14-Mar-09 5:19
Eugenio Serrano14-Mar-09 5:19 
GeneralRe: Problem with spanish dictionary Pin
Thomas Maierhofer (Tom)14-Mar-09 23:02
Thomas Maierhofer (Tom)14-Mar-09 23:02 
GeneralRe: Problem with spanish dictionary Pin
Eugenio Serrano15-Mar-09 20:15
Eugenio Serrano15-Mar-09 20:15 
GeneralQuestion fpr usage with C# Pin
hsp6012-Mar-09 2:24
hsp6012-Mar-09 2:24 
Hi Thomas,

I've been trying to use your NHunspell with my C# application (a tiny and simple word processor) under Visual Studio Express 2008. I copied the dlls to my project path and added both dlls as dependencies to that project. I did not install the redistributable you provided.

I can translate everything, and running it there is no exception.
But stepping through in the debugger I noticed that all the words I checks are returned as not correct even if the y are correct (i.e people, if, ....). Also I never get any suggestion. An empty list is returned.

I checked that the dlls are correctly copied to the bin-directory where my exe can be found.

Do I need the redistributable? Does Hunspell hsp = new Hunspell(....); find the libraries? I tried obviously incorrect filenames and get the same results. Where does Hunspell search for the library files? Can I supply a realtive path like .\Dictionaries\en_US.xxx in the filenames?

BTW: Thank your for sharing your work.

Heinrich
GeneralRe: Question fpr usage with C# Pin
Thomas Maierhofer (Tom)12-Mar-09 2:43
Thomas Maierhofer (Tom)12-Mar-09 2:43 
GeneralRe: Question fpr usage with C# Pin
hsp6013-Mar-09 5:06
hsp6013-Mar-09 5:06 
GeneralNHunspell 0.5.4. beta is released - download from Sourceforge Pin
Thomas Maierhofer (Tom)3-Mar-09 8:39
Thomas Maierhofer (Tom)3-Mar-09 8:39 
GeneralRe: NHunspell 0.5.4. beta is released - download from Sourceforge Pin
Paul Selormey8-Mar-09 6:00
Paul Selormey8-Mar-09 6:00 
GeneralRe: NHunspell 0.5.4. beta is released - download from Sourceforge Pin
Thomas Maierhofer (Tom)8-Mar-09 22:22
Thomas Maierhofer (Tom)8-Mar-09 22:22 
GeneralRe: NHunspell 0.5.4. beta is released - download from Sourceforge Pin
Paul Selormey8-Mar-09 23:11
Paul Selormey8-Mar-09 23:11 
GeneralRe: NHunspell 0.5.4. beta is released - download from Sourceforge Pin
Thomas Maierhofer (Tom)9-Mar-09 4:11
Thomas Maierhofer (Tom)9-Mar-09 4:11 
GeneralRe: NHunspell 0.5.4. beta is released - download from Sourceforge Pin
Paul Selormey9-Mar-09 4:31
Paul Selormey9-Mar-09 4:31 
QuestionPls help Pin
Patel Maulik2-Mar-09 19:43
Patel Maulik2-Mar-09 19:43 
AnswerRe: Pls help Pin
Thomas Maierhofer (Tom)2-Mar-09 21:32
Thomas Maierhofer (Tom)2-Mar-09 21:32 
GeneralRe: Pls help Pin
Patel Maulik2-Mar-09 22:48
Patel Maulik2-Mar-09 22:48 
GeneralRe: Pls help Pin
Thomas Maierhofer (Tom)3-Mar-09 5:01
Thomas Maierhofer (Tom)3-Mar-09 5:01 
GeneralRe: Pls help Pin
Thomas Maierhofer (Tom)3-Mar-09 8:36
Thomas Maierhofer (Tom)3-Mar-09 8:36 
Answer[Message Deleted] Pin
Patel Maulik4-Mar-09 0:43
Patel Maulik4-Mar-09 0:43 
GeneralRe: Pls help Pin
Thomas Maierhofer (Tom)4-Mar-09 0:46
Thomas Maierhofer (Tom)4-Mar-09 0:46 
GeneralRe: Pls help Pin
Patel Maulik4-Mar-09 0:49
Patel Maulik4-Mar-09 0:49 
GeneralGood stuff. Pin
Sike Mullivan25-Feb-09 9:28
Sike Mullivan25-Feb-09 9:28 

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

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