HunspellSharp 1.0.1

dotnet add package HunspellSharp --version 1.0.1                
NuGet\Install-Package HunspellSharp -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HunspellSharp" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HunspellSharp --version 1.0.1                
#r "nuget: HunspellSharp, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HunspellSharp as a Cake Addin
#addin nuget:?package=HunspellSharp&version=1.0.1

// Install HunspellSharp as a Cake Tool
#tool nuget:?package=HunspellSharp&version=1.0.1                

HunspellSharp

This is a C# port of Hunspell library.

Features

  • Targets .NET Framework (4.0 the lowest), .NET 6+ and .NET Standard 2.0.
  • Uses only safe managed code.
  • Querying methods (Spell, Suggest, etc) are thread-safe.

Usage

Preliminary

If your application is targeting .NET or .NET Standard and intended to work with dictionaries encoded not as ISO-8859-1 or UTF-8, install System.Text.Encoding.CodePages package and make additional encodings available by calling Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) before loading Hunspell files.

Constructors

With file paths:

var hunspell = new Hunspell("en.aff", "en.dic");

Like original Hunspell, it tries to open hzipped files (.hz) in case the plain files are not found. Optional hzip key can be added:

var hunspell = new Hunspell("en.aff", "en.dic", key);

The key argument is a byte array. Use Encoding.GetBytes of the appropriate encoding to get byte array from a string.

With streams:

var hunspell = new Hunspell(affStream, dicStream);

In case the stream is packed with hzip, wrap it in HzipStream. The HzipStream contructor also accepts optional hzip key.

Disposing

A Hunspell instance uses some disposable resources and thus is disposable itself, so do not forget to add a using statement or explicitly call hunspell.Dispose() when it is no longer needed.

Spelling

bool result = hunspell.Spell("sample");

There are also methods that provide additional info and the root word:

result = hunspell.Spell("sample", out var info);
result = hunspell.Spell("sample", out var info, out var root);

Suggestions

Generate suggestions for a misspelled word:

List<string> suggestions = hunspell.Suggest("sapmle");

Simplified XML API input is supported. See the Hunspell manual for a description.

Suggest words by applying suffix rules to the root word:

List<string> suggestions = hunspell.SuffixSuggest("sample");

Morphology

Get morphlogical description:

List<string> description = hunspell.Analyze("examples");

Generate words using morphlogical description:

List<string> results = hunspell.Generate("sample", description);

or by example:

List<string> results = hunspell.Generate("sample", "examples");

Get stem(s):

List<string> stems = hunspell.Stem("samples");

Using previous result of the morphological analysis:

List<string> stems = hunspell.Stem("samples", description);

Dictionary manipulation

[!NOTE] These methods are not thread-safe and have to be run exclusively.

Append extra dictionary, from a file path or a stream:

hunspell.AddDic("some.dic");
hunspell.AddDic(dicStream);

As in the contructors, optional hzip key or HzipStream can be used:

hunspell.AddDic("some.dic", key);
hunspell.AddDic(new HzipStream(hzDicStream, key));

Add a word to the run-time dictionary:

hunspell.Add("word");

With flags and morphological description:

hunspell.AddWithFlags("word", flags, description);

With affixes using an example word:

hunspell.AddWithAffix("word", "example");

Remove word from the run-time dictionary:

hunspell.Remove("word");

Various dictionary properties and methods

Dictionary encoding:

Encoding encoding = hunspell.DicEncoding;

Dictionary language number; enum values match the numbers in the original Hunspell:

LANG langnum = hunspell.LangNum;

Affix and dictionary file version:

string version = hunspell.Version;

Extra word characters defined in the affix file:

char[] wordchars = hunspell.Wordchars;

Input conversion according to the ICONV table specified in the affix file:

string output = hunspell.InputConv("input");

Error handling

HunspellSharp throws exceptions of the type HunspellException on severe affix/dictionary format errors. In case you need the behavior of the original Hunspell, which always just issues warnings but continues execution, set static property StrictFormat to false:

Hunspell.StrictFormat = false;

Warning handling

By default, HunspellSharp sends warning messages to System.Diagnostics.Debug. To change this, create a class implementing IHunspellWarningHandler interface and pass the reference to an instance of it to the static method SetWarningHandler:

class CustomWarningHandler : IHunspellWarningHandler
{
  public bool HandleWarning(string message)
  {
    Console.WriteLine(message);
    return true;
  }
}

...

Hunspell.SetWarningHandler(new CustomWarningHandler());

Technical notes

HunspellSharp relies on System.Globalization features when converting characters to lower- or uppercase. If language is specified in the affix file, appropriate CultureInfo is used. If not, the culture is either guessed from the encoding, or defaults to the invariant culture. In the latter case, some results may differ from the original Hunspell that uses embedded case conversion tables. For example, the invariant culture does not convert capital 'İ' to lowercase 'i', so Turkish words containing 'İ' will not be recognized as forms of lowercase dictionary words if no language is specified in the affix file and dictionary encoding is not ISO-8859-9. To avoid this, specify correct dictionary language explicitly.

N-gram suggestions may sometimes differ from the original ones, because their choice depends on the order of words in the internal hash tables, sorting algorithm and other factors, which are not the same as in the original Hunspell.

This port is rather straightforward. Non-public source code does not follow usual C# conventions deliberately, in attempt to keep resemblance to the original C++ code wherever possible.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 854 7/8/2024
1.0.0 103 7/8/2024