简体   繁体   English

Nhunspell C#将词添加到词典中

[英]Nhunspell C# Adding Word to Dictionary

I have managed to incorporate spell checking into my C# project using NHunspell. 我已经设法使用NHunspell将拼写检查合并到我的C#项目中。 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: 在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");                
}

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 谢谢

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. 我没有意识到使用.Add()方法添加单词只允许在Hunspell对象处于活动状态时使用该单词。 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. 现在,当我的主拼写检查功能被调用时,在检查任何单词之前,使用.Add()方法添加自定义词典中的所有单词。 Hope this helps. 希望这可以帮助。

Adding a word in the dictionary is simply append new word in any text file using WriteLine() of StreamWriter . 在字典中添加单词只需使用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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM