Question

I have managed to incorporate spell checking into my C# project using NHunspell. What I would like to do is actually add a word to the dictionary file. There is a method to do this inside of NHunspell which I believe is as follows:

// Add the word to the dictionary and carry on
using (Hunspell hunspell = new Hunspell(@"Dictionaries/en_GB.aff", @"Dictionaries/en_GB.dic"))
{
    hunspell.Add("wordToAdd");                
}

When I use this however it does not appear to actually do anything. Would anyone be able to suggest what it is I am doing wrong?

Thanks

Was it helpful?

Solution

I did not realise that adding a word using the .Add() method only allows that word to be used while the Hunspell object is alive. The word is not actually added to the external dictionary file. The way I combated this problem was to make use of a custom dictionary file. When a word is added by the user this word is stored in the new custom dictionary file. Now when my main spell checker function is called, before any words are checked all the words which are in the custom dictionary are added using the .Add() method. Hope this helps.

OTHER TIPS

Adding a word in the dictionary is simply append new word in any text file using WriteLine() of StreamWriter.

private void button1_Click(object sender, EventArgs e)
{
    FileWriter(txtDic.Text, txtWord.Text, true);
    txtWord.Clear();
    MessageBox.Show("Success...");
}

public static void FileWriter(string filePath, string text, bool fileExists)
   {
        if (!fileExists)
        {
            FileStream aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(aFile);
            sw.WriteLine(text);
            sw.Close();
            aFile.Close();
        }
        else
        {
            FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(aFile);
            sw.WriteLine(text+"/3");
            sw.Close();
            aFile.Close();
            //System.IO.File.WriteAllText(filePath, text);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top