Nhunspell C# 将单词添加到字典中

发布于 2025-01-08 03:42:04 字数 373 浏览 0 评论 0原文

我已成功使用 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");                
}

但是当我使用这个方法时,它似乎实际上没有做任何事情。有人能指出我做错了什么吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2025-01-15 03:42:04

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

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.

黎歌 2025-01-15 03:42:04

在字典中添加单词只需使用 StreamWriterWriteLine() 在任何文本文件中添加新单词即可。

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

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文