0

I have to include spellcheck functionality for one of my projects and I decided on hunspell as it's an excellent spellchecker (lots of free and proprietary software's use it). I downloaded the source code and added the project libhunspell to the project. Got it to compile without any errors and also downloaded the english dictionaries form openoffice website. The following is the code I use to initialize hunspell engine and class it's spell check functionality:

    Hunspell *spellObj = (Hunspell *)hunspell_initialize("en_us.aff", "en_us.dic");

if(spellObj)
{
    int result = hunspell_spell(spellObj, "apply");
    hunspell_uninitialize(spellObj);
}

The code doesn't throw any errors but hunspell_spell always returns 0 whatever the word may be.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

2

Try this. This is what I am using in a MVC3 project

private const string AFF_FILE = "~/App_Data/en_us.aff";
private const string DICT_FILE = "~/App_Data/en_us.dic";

public ActionResult Index(string text)
{
  using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE)))
  {
    Dictionary<string, List<string>> incorrect = new Dictionary<string, List<string>>();
    text = HttpUtility.UrlDecode(text);
    string[] words = text.Split(new char[] { ' ' }, StringSplitOption.RemoveEmptyEntries);
    foreach ( string word in words)
    {
       if (!hunspell.Spell(word) && !incorrect.ContainsKey(word))
       {
          incorrect.Add(word, hunspell.Suggest(word));
       }
    }
    return Json(Incorrect);
  }
}

public ActionResult Suggest(string word)
{
   using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE)))
   {
      word = HttpUtility.UrlDecode(word);
      return Json(hunspell.Suggest(word));
   }
}
public ActionResult Add(string word)
{
  using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE)))
   {
      word = HttpUtility.UrlDecode(word);
      return Json(hunspell.Add(word));
   }
}