2

I want to add some custom words to dictionay by using Hunspell:

I load from dictionary in the constructor:

private readonly Hunspell _hunspell;
public NhunspellHelper()
{
    _hunspell = new Hunspell(
        HttpContext.Current.Server.MapPath("~/App_Data/en_US.aff"),
        HttpContext.Current.Server.MapPath("~/App_Data/en_US.dic"));
}

This function adds a new word to the dictionary:

public void AddToDictionary(string word)
{
    _hunspell.Add(word); // or _hunspell.AddWithAffix(word, "aaa");
}

After I add a word to the dictionary, if I spell this word in the same request:

_hunspell.Spell(word)

It returns true, but if I spell this word in another request, it returns false

I check both files .aff and .dic, I see it does not change after _hunspell.Add(word);, so when another request is sent, the constructor creates a new Hunspell instance from the original dictionary.

My question is: Does the Nhunspell add the new word to the dictionary and save it back to the physical file (*.aff or *.dic), or does it just add it to memory and do nothing with the dictionary file?

Did I do something wrong to add a new word to the dictionary?

Stiger
  • 1,189
  • 2
  • 14
  • 29
  • 1
    I did some sleuthing in the hunspell c++, it doesn't look like it has something that saves the file back. I could be wrong. [C++ Manager Headers](http://nhunspell.svn.sourceforge.net/viewvc/nhunspell/trunk/Hunspell/hunspell/affixmgr.hxx?revision=87&view=markup) doesn't appear to have a save anywhere – Prescott Jun 13 '14 at 08:50
  • Hi @Prescott, I work in C# not C++ – Stiger Jun 13 '14 at 16:47
  • 1
    NHunspell is a wrapper around the C++ native dll. While researching the .Add() method to determine if it modified the file itself (vs only the internal, in memory dictionary), I had to dig into the C++ because the C# wrapper calls through to that. – Prescott Jun 13 '14 at 18:04
  • Oh, so the anwer is NHunspell does not save anything back to the dictionary? – Stiger Jun 14 '14 at 01:37
  • You use Hunspell on a web server. It is highly recommended to use the SpellFactory class (http://help.crawler-lib.net/NHunspell/html/T_NHunspell_SpellFactory.htm) in multi threaded environments. (http://www.codeproject.com/Articles/43769/Spell-Check-Hyphenation-and-Thesaurus-for-NET-wi) – Thomas Maierhofer Jul 24 '14 at 07:07

1 Answers1

1

Finally, with Prescott comment, I found this information from the author (Thomas Maierhofer) in CodeProject:

You can use Add() and AddWithAffix() to add your words into a already created Hunspell object. The Dictionary files are not modified, so this addition must be done every time you create a Hunspell object. You can store your own dictionary whereever you want and add the words from your dictionary after you create a Hunspell object. After that you can spell check with your own words in the dictionary.

It's mean nothing save back to dictionary file, so I change my Nhunspell class to singleton to keep the Hunspell object.

public class NhunspellHelper
{
    private readonly Hunspell _hunspell;
    private NhunspellHelper()
    {
        _hunspell = new Hunspell(
                        HttpContext.Current.Server.MapPath("~/App_Data/en_US.aff"),
                        HttpContext.Current.Server.MapPath("~/App_Data/en_US.dic"));
    }

    private static NhunspellHelper _instance;
    public static NhunspellHelper Instance
    {
        get { return _instance ?? (_instance = new NhunspellHelper()); }
    }

    public bool Spell(string word)
    {
        return _hunspell.Spell(word);
    }
}

I can spell word every where with this line:

 var isCorrect = NhunspellHelper.Instance.Spell("word");
Stiger
  • 1,189
  • 2
  • 14
  • 29
  • 3
    please keep in mind that a Hunspell object is not thread safe. If you use the singleton pattern you must ensure that the object isn't used by two threads the same time. – Thomas Maierhofer Jul 24 '14 at 07:03