4

我已经设法使用 NHunspell 将拼写检查合并到我的 C# 项目中。我想做的实际上是在字典文件中添加一个单词。在 NHunspell 内部有一种方法可以做到这一点,我相信如下:

// 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");                
}

但是,当我使用它时,它似乎实际上并没有做任何事情。谁能建议我做错了什么?

谢谢

4

2 回答 2

9

我没有意识到使用 .Add() 方法添加一个词只允许在 Hunspell 对象存在时使用该词。该词实际上并未添加到外部词典文件中。我解决这个问题的方法是使用自定义字典文件。当用户添加一个词时,该词将存储在新的自定义词典文件中。现在,当调用我的主要拼写检查器函数时,在检查任何单词之前,使用 .Add() 方法添加自定义词典中的所有单词。希望这可以帮助。

于 2012-02-26T22:26:24.203 回答
1

在字典中添加一个单词只是在任何文本文件中使用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);
        }
    }
于 2012-12-05T12:53:06.483 回答