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

辞書に単語を追加することは、 of を使用して任意のテキスト ファイルに新しい単語を追加するだけWriteLine()です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 に答える