2012-02-22 92 views
4

我設法使用NHunspell將拼寫檢查合併到我的C#項目中。我想要做的是實際上爲詞典文件添加一個詞。還有就是要做到這一點NHunspell裏面我相信這是如下的方法:Nhunspell C#將單詞添加到字典

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

當我使用這個但它似乎並沒有真正做任何事情。任何人都可以建議我做錯了什麼?

由於

回答

9

我沒有意識到,添加使用。新增()方法一個字只允許同時的hunspell對象是活要使用這個詞。該詞實際上並未添加到外部詞典文件中。我解決這個問題的方法是利用自定義字典文件。當用戶添加單詞時,該單詞被存儲在新的自定義詞典文件中。現在,當我的主要拼寫檢查函數被調用時,在檢查任何單詞之前,使用.Add()方法添加自定義字典中的所有單詞。希望這可以幫助。

+0

我添加了一個代碼示例如何加載自定義詞典的NHunspell樣本: http://www.crawler-lib.net/csharp-code-samples-nhunspell – 2014-09-15 10:52:36

+0

這是真的很有趣, pyhunspell(一個與hunspell的Python接口)的工作方式是一樣的。 add()根本不會向字典文件添加任何內容。 – 2015-08-10 09:05:17

1

在字典中添加單詞在使用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); 
     } 
    }