Giter Club home page Giter Club logo

hunspellsharp's Introduction

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.

hunspellsharp's People

Contributors

snnz avatar

Stargazers

Richard Chamorro avatar

Watchers

 avatar

Forkers

todoexpertos

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.