2012-07-23 75 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

相关问题