2012-07-23 72 views
0

我必須爲我的一個項目包含拼寫檢查功能,並且我決定使用hunspell,因爲它是一個出色的拼寫檢查程序(大量免費和專有軟件都使用它)。我下載了源代碼並將項目libhunspell添加到項目中。得到它沒有任何錯誤編譯,也下載了英文字典形式openoffice網站。以下是我用來初始化中的hunspell引擎類的代碼它的拼寫檢查功能:Hunspell代碼在Visual Studio 2010中不工作

Hunspell *spellObj = (Hunspell *)hunspell_initialize("en_us.aff", "en_us.dic"); 

if(spellObj) 
{ 
    int result = hunspell_spell(spellObj, "apply"); 
    hunspell_uninitialize(spellObj); 
} 

代碼不拋出任何錯誤,但hunspell_spell總是返回0無論這個詞可能。

回答

2

試試這個。這是我在一個MVC3項目中使用

private const string AFF_FILE = "~/App_Data/en_us.aff"; 
private const string DICT_FILE = "~/App_Data/en_us.dic"; 

public ActionResult Index(string text) 
{ 
    using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE))) 
    { 
    Dictionary<string, List<string>> incorrect = new Dictionary<string, List<string>>(); 
    text = HttpUtility.UrlDecode(text); 
    string[] words = text.Split(new char[] { ' ' }, StringSplitOption.RemoveEmptyEntries); 
    foreach (string word in words) 
    { 
     if (!hunspell.Spell(word) && !incorrect.ContainsKey(word)) 
     { 
      incorrect.Add(word, hunspell.Suggest(word)); 
     } 
    } 
    return Json(Incorrect); 
    } 
} 

public ActionResult Suggest(string word) 
{ 
    using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE))) 
    { 
     word = HttpUtility.UrlDecode(word); 
     return Json(hunspell.Suggest(word)); 
    } 
} 
public ActionResult Add(string word) 
{ 
    using(NHunspell.Hunspell hunspell = new NHunspell.Hunspell(Server.MapPath(AFF_FILE), Server.MapPath(DICT_FILE))) 
    { 
     word = HttpUtility.UrlDecode(word); 
     return Json(hunspell.Add(word)); 
    } 
} 
+0

這工作對我來說,謝謝。 – eplewis89 2013-07-29 22:09:58

相關問題