Apps

Spell-check .NET applications with NHunspell

NHunspell provides full-featured spell-checking, thesaurus, and hyphenation features for .NET applications. Here's how to get and use NHunspell.

The open source community continues to produce impressive products for the .NET Framework. NHunspell is the latest example to gain my attention, although it has been around for a couple of years. NHunspell provides a spell-checker, a thesaurus, and hyphenation to the .NET Framework, all rolled into a nice GPL/LGPL/MPL tri-license model. Here's a snapshot of what NHunspell has to offer.

Broad adoption and acceptance

As its SourceForge page says, Hunspell is the spell-checker that is used in a variety of popular products, including Mac OS X, Adobe InDesign, Google Chrome, Opera, Firefox/Thunderbird, OpenOffice.org, and many more. In addition, it is available for those developing in other languages such as Perl, Python, Ruby, and Java. This level of usage should give you confidence about integrating it in your development project.

NHunspell wraps the native Hunspell and Hyphen libraries (unmanaged DLL files) for use with .NET code. It also includes a fully managed version of the thesaurus feature called MyThes.

Getting NHunspell

NHunspell is available via NuGet. You can locate and install it within the Extensions And Updates window in Visual Studio 2012 (or Extension Manager in Visual Studio 2010) as shown in Figure A. Select the Online tab and search for NHunspell. Click the Download button to install it. Figure A

Locating NHunspell in the Visual Studio 2012 Extensions And Updates window. (Click the image to enlarge.)
Figure B shows the Extensions And Updates window after NHunspell is installed. Notice a restart of Visual Studio is necessary to complete the installation. Figure B

Restart Visual Studio after the NHunspell installation is complete. (Click the image to enlarge.)
Or, you can install NHunspell via the Package Manager Console (via Library Package Manager menu) as shown in Figure C. Packages for a solution are managed via the Library Package Manager menu as well -- choose Manage NuGet Packages for Solution. Figure C

Installing NHunspell via the NuGet Package Manager Console. (Click the image to enlarge.)

The notes state the NHunspell package installs a PostBuild event, which copies the unmanaged Hunspell dlls into the bin folder. Also, the notes say dictionaries are not installed, but dictionaries are available online. As an example, I downloaded the standard US English Spell Checking Dictionary. The file uses the OpenOffice dictionary file extension (.oxt).

When added to a solution, the NHunspell.dll file is placed in a packages subdirectory within the solution folder. For my sample project, it was placed in the following location:

C:\Visual Studio 2012\Projects\TR_NHunspell\packages\NHunspell.1.1.0\lib\net\NHunspell.dll

Also, 64-bit and x86 DLL files are in the lib directory of the NHunspell installation directory:

C:\Visual Studio 2012\Projects\TR_NHunspell\packages\NHunspell.1.1.0\native\ Hunspellx64.dll
C:\Visual Studio 2012\Projects\ TR_NHunspell\packages\NHunspell.1.1.0\native\ Hunspellx86.dll

Once the application is compiled, the NHunspell dll files are placed in the application's bin directory:

C:\Visual Studio 2012\Projects\TR_NHunspell\bin\
NHunspell.dll
Hunspellx64.dll
Hunspellx86.dll
NHunspell.config

Integrating in your code

With NHunspell added to your solution, you can use the numerous features available in your application code. The following C# console application snippet checks the spelling of a word and signals if it is spelled correctly. Notice the NHunspell package is referenced in the third using statement -- this brings the NHunspell features to the code. An instance of the Hunspell object is created with the dictionary file passed to it (the dictionary file is placed in the same directory as DLL). The Spell method is used to check the spelling of a word passed to it. It returns a Boolean value signaling whether the spelling used is correct.

using System;

using System.Collections.Generic;

using NHunspell;

namespace TRc_NHunspell {

class Program  {

static void Main(string[] args)  {

using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.oxt"))  {

Console.WriteLine("Let's determine if technical is spelled correctly.");

bool check = hunspell.Spell("technical");

Console.WriteLine("The word (technical) is " + (check ? "right." : "wrong."));
}  }  }  }

This simple example can be extended to list alternate word suggestions via the Suggest method with a word passed as the sole parameter. The method returns a List object containing the alternate word suggestions (if any).

Console.WriteLine("Let's look at some alternatives for the word.");

List<string> alternatives = hunspell.Suggest("technical");

Console.WriteLine("There are " + alternatives.Count.ToString() + " options.");

foreach (string alternate in alternatives)  {

Console.WriteLine("Hunspell proposes: " + alternate);

}

Make sure it is correct

The NHunspell port to .NET allows you to use a mature (aka, old) tool for spell-checking, thesaurus, and hyphenation features. While you can use other tools offered by Microsoft for these tasks (e.g., WPF has a built-in spell-checker), NHunspell has many features other tools do not. I'll report back if my opinion of NHunspell changes after extended use.

What thesaurus or spell-checking tools/add-ons do you use or recommend? Let us know in the discussion.

Keep your engineering skills up to date by signing up for TechRepublic's free Software Engineer newsletter, delivered each Tuesday.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

1 comments
tmaierhofer
tmaierhofer

Thank you for this great article. If you have any suggestions or improvements please contact me. Best regards Thomas

Editor's Picks