我已经设法使用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");
}然而,当我使用它时,它看起来并没有实际做任何事情。有人能指出我做错了什么吗?
谢谢
发布于 2012-02-27 06:26:24
我没有意识到使用.Add()方法添加单词只允许在Hunspell对象处于活动状态时使用该单词。该单词实际上并未添加到外部字典文件中。我解决这个问题的方法是使用一个定制的字典文件。当用户添加一个单词时,该单词被存储在新的自定义词典文件中。现在,当调用我的主拼写检查器函数时,在检查任何单词之前,使用.Add()方法添加自定义词典中的所有单词。希望这能有所帮助。
发布于 2012-12-05 20:53:06
在字典中添加一个单词只需使用StreamWriter的WriteLine()将新单词附加到任何文本文件中。
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);
}
}https://stackoverflow.com/questions/9393437
复制相似问题