Click here to Skip to main content
12,443,996 members (46,116 online)
Click here to Skip to main content
Add your own
alternative version

Stats

142K views
2K downloads
56 bookmarked
Posted

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 for Microsoft .NET and Mono. Please take a look at it:
Crawler-Lib Homepage
Crawler-Lib Blog
Crawler-Lib YouTube Channel

You may also be interested in...

Comments and Discussions

 
GeneralRe: Hunspell class Add method not working Pin
Thomas Maierhofer18-Jul-10 23:02
memberThomas Maierhofer18-Jul-10 23:02 
NewsJust posted an IExtenderProvider using NHunspell [modified] Pin
William Winner3-Feb-10 8:34
memberWilliam Winner3-Feb-10 8:34 
GeneralNHunspell ExtenderProvider Pin
William Winner14-Jan-10 8:55
memberWilliam Winner14-Jan-10 8:55 
GeneralRe: NHunspell ExtenderProvider Pin
Thomas Maierhofer14-Jan-10 21:15
memberThomas Maierhofer14-Jan-10 21:15 
GeneralRe: NHunspell ExtenderProvider Pin
William Winner26-Jan-10 12:50
memberWilliam Winner26-Jan-10 12:50 
GeneralRe: NHunspell ExtenderProvider Pin
William Winner26-Jan-10 13:15
memberWilliam Winner26-Jan-10 13:15 
NewsTwo new NHunspell Articles for Version 0.9.2 and above are available Pin
Thomas Maierhofer16-Dec-09 1:54
memberThomas Maierhofer16-Dec-09 1:54 
GeneralProblem when loading a dictionary... Pin
Knight_Rider23-Nov-09 6:10
memberKnight_Rider23-Nov-09 6:10 
GeneralRe: Problem when loading a dictionary... Pin
Thomas Maierhofer23-Nov-09 6:30
memberThomas Maierhofer23-Nov-09 6:30 
GeneralRe: Problem when loading a dictionary... Pin
Knight_Rider23-Nov-09 9:06
memberKnight_Rider23-Nov-09 9:06 
GeneralRe: Problem when loading a dictionary... Pin
Thomas Maierhofer23-Nov-09 19:37
memberThomas Maierhofer23-Nov-09 19:37 
GeneralRe: Problem when loading a dictionary... [modified] Pin
Aleksei Krassovskikh13-Feb-10 9:13
memberAleksei Krassovskikh13-Feb-10 9:13 
GeneralNHunspell 0.9.2 is relesed - Thesaurus is available Pin
Thomas Maierhofer27-Oct-09 21:02
memberThomas Maierhofer27-Oct-09 21:02 
GeneralNHunspell as ASP.NET App: http://www.spell-check-thesaurus.com/ Pin
Thomas Maierhofer26-Oct-09 10:09
memberThomas Maierhofer26-Oct-09 10:09 
GeneralNHunspell Development Status Report: Stemming, Thesaurus, multi Threading, other improvements Pin
Thomas Maierhofer20-Oct-09 2:10
memberThomas Maierhofer20-Oct-09 2:10 
GeneralDictionaries Pin
Thomas Maierhofer20-Oct-09 2:03
memberThomas Maierhofer20-Oct-09 2:03 
QuestionSimulating OpenOffice Pin
Ant210022-Jun-09 4:16
memberAnt210022-Jun-09 4:16 
AnswerRe: Simulating OpenOffice Pin
Thomas Maierhofer24-Jun-09 23:58
memberThomas Maierhofer24-Jun-09 23:58 
GeneralRe: Simulating OpenOffice Pin
Ant210025-Jun-09 4:21
memberAnt210025-Jun-09 4:21 
AnswerRe: Simulating OpenOffice [modified] Pin
William Winner3-Feb-10 9:02
memberWilliam Winner3-Feb-10 9:02 
GeneralRe: Simulating OpenOffice Pin
Ant21003-Feb-10 9:04
memberAnt21003-Feb-10 9:04 
GeneralNHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files Pin
Thomas Maierhofer28-May-09 22:22
memberThomas Maierhofer28-May-09 22:22 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files Pin
CCPOSTON11-Aug-09 11:39
memberCCPOSTON11-Aug-09 11:39 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files Pin
Thomas Maierhofer11-Aug-09 21:51
memberThomas Maierhofer11-Aug-09 21:51 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files Pin
CCPOSTON12-Aug-09 4:24
memberCCPOSTON12-Aug-09 4:24 
GeneralRe: NHunspell 0.6.2 is released - Location of the Hunspellx86.dll and Hunspellx64.dll files Pin
Member 654289929-Sep-09 21:05
memberMember 654289929-Sep-09 21:05 
GeneralNHunspell 0.6.0 released Pin
Thomas Maierhofer27-Apr-09 0:53
memberThomas Maierhofer27-Apr-09 0:53 
QuestionRe: NHunspell 0.6.0 released Pin
jazzmoney19-May-09 12:43
memberjazzmoney19-May-09 12:43 
AnswerRe: NHunspell 0.6.0 released Pin
Thomas Maierhofer24-May-09 23:50
memberThomas Maierhofer24-May-09 23:50 
QuestionRe: NHunspell 0.6.0 released Pin
dushafer28-May-09 3:35
memberdushafer28-May-09 3:35 
AnswerRe: NHunspell 0.6.0 released Pin
Thomas Maierhofer28-May-09 22:16
memberThomas Maierhofer28-May-09 22:16 
GeneralRe: NHunspell 0.6.0 released Pin
dushafer29-May-09 10:35
memberdushafer29-May-09 10:35 
QuestionThesaurus function to find synonyms? Pin
Wojciech_Duell9-Apr-09 5:48
memberWojciech_Duell9-Apr-09 5:48 
AnswerRe: Thesaurus function to find synonyms? Pin
Thomas Maierhofer27-Apr-09 0:55
memberThomas Maierhofer27-Apr-09 0:55 
QuestionError with hunspell Pin
sagun shrestha27-Mar-09 0:30
membersagun shrestha27-Mar-09 0:30 
AnswerRe: Error with hunspell Pin
Thomas Maierhofer27-Mar-09 2:53
memberThomas Maierhofer27-Mar-09 2:53 
GeneralRe: Error with hunspell Pin
conbo18-May-09 18:22
memberconbo18-May-09 18:22 
GeneralRe: Error with hunspell Pin
Thomas Maierhofer18-May-09 19:48
memberThomas Maierhofer18-May-09 19:48 
Generalok wrapper Pin
Donsw21-Mar-09 15:39
memberDonsw21-Mar-09 15:39 
GeneralGreek language [modified] Pin
zissop20-Mar-09 14:58
memberzissop20-Mar-09 14:58 
GeneralRe: Greek language Pin
Thomas Maierhofer20-Mar-09 22:57
memberThomas Maierhofer20-Mar-09 22:57 
GeneralRe: Greek language Pin
zissop20-Mar-09 23:48
memberzissop20-Mar-09 23:48 
GeneralRe: Greek language Pin
Thomas Maierhofer21-Mar-09 2:30
memberThomas Maierhofer21-Mar-09 2:30 
GeneralRefactoring of NHunspell Pin
Thomas Maierhofer15-Mar-09 23:05
memberThomas Maierhofer15-Mar-09 23:05 
GeneralProblem with spanish dictionary Pin
Eugenio Serrano14-Mar-09 5:19
memberEugenio Serrano14-Mar-09 5:19 
GeneralRe: Problem with spanish dictionary Pin
Thomas Maierhofer14-Mar-09 23:02
memberThomas Maierhofer14-Mar-09 23:02 
GeneralRe: Problem with spanish dictionary Pin
Eugenio Serrano15-Mar-09 20:15
memberEugenio Serrano15-Mar-09 20:15 
GeneralQuestion fpr usage with C# Pin
hsp6012-Mar-09 2:24
memberhsp6012-Mar-09 2:24 
GeneralRe: Question fpr usage with C# Pin
Thomas Maierhofer12-Mar-09 2:43
memberThomas Maierhofer12-Mar-09 2:43 
GeneralRe: Question fpr usage with C# Pin
hsp6013-Mar-09 5:06
memberhsp6013-Mar-09 5:06 

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.

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