Work in Progress… Stay tuned and come back in a few days.
You can download the C# code samples for NHunspell at once. It is available as a zipped Visual Studio Solution and contains all the listed code samples.
NHunspell NuGet Package – this is recommended installation method.
NHunspell Downloads – NHunspell and the NHunspell samples solution
Each sample contains general information about the design and the usage of NHunspell. The samples are intend to be an introduction for programmers.
The Hunspell
class is a managed wrapper to a native spell checker object. It supports and needs the IDisposabe
interface. This is one way how Hunspell
object is constructed:
using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic")) { // Process all your words here, don't create the Hunspell object for every word. }
After the Hunspell object is created, words can be checked with the Spell()
method:
Console.WriteLine("Check if the word 'Recommendation' is spelled correct"); bool correct = hunspell.Spell("Recommendation"); Console.WriteLine("Recommendation is spelled " + (correct ? "correct" : "not correct"));
Retrieve suggestions for misspelled words with the Suggest()
method:
Console.WriteLine("Make suggestions for the misspelled 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 ); }
To find the word stem for a word, use the Stem()
method:
Console.WriteLine("Find the word stem of the word 'decompressed'"); List<string> stems = hunspell.Stem("decompressed"); foreach (string stem in stems) { Console.WriteLine("Word Stem is: " + stem); }
Declensions can be generated by providing a sample to the Generate()
method:
Console.WriteLine("Generate the plural of 'girl' by providing sample 'boys'"); List<string> generated = hunspell.Generate("girl","boys"); foreach (string stem in generated) { Console.WriteLine("Generated word is: " + stem); }
Morphology analysis of a word can be done with the Analyze()
method:
Console.WriteLine("Analyze the word 'decompressed'"); List<string> morphs = hunspell.Analyze("decompressed"); foreach (string morph in morphs) { Console.WriteLine("Morph is: " + morph); }
Words can be added from a custom text file via the Add()
method. Here is a text file (custum dictionary) loaded and the lines are added one by one as new words:
using (var hunspell = new Hunspell("en_us.aff", "en_us.dic")) { string[] lines = System.IO.File.ReadAllLines("CustomWords-en_US.txt"); foreach (var line in lines) { hunspell.Add(line); } bool correct = hunspell.Spell("MyTag"); Console.WriteLine("'MyTag' is spelled " + (correct ? "correct" : "not correct")); List<string> suggestions = hunspell.Suggest("MyTog"); foreach (string suggestion in suggestions) { Console.WriteLine("Suggestion is: " + suggestion); } }
Keep in mind that the Hunspell object is not thread safe.
The Hyphen
class is a managed wrapper to a native hyphenation object. It supports and needs the IDisposabe
interface.
The MyThes
class is a managed thesaurus implementation. It doesn’t need and support the IDisposabe
interface.