【问题标题】:Hunspell code not working in Visual Studio 2010Hunspell 代码在 Visual Studio 2010 中不起作用
【发布时间】:2015-09-17 10:56:54
【问题描述】:

我必须为我的一个项目添加拼写检查功能,我决定使用 hunspell,因为它是一款出色的拼写检查器(许多免费和专有软件都在使用它)。我下载了源代码并将项目 libhunspell 添加到项目中。让它编译没有任何错误,还从 openoffice 网站下载了英文词典。以下是我用来初始化 hunspell 引擎和类它的拼写检查功能的代码:

    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。

【问题讨论】:

    标签: visual-studio-2010 spell-checking hunspell


    【解决方案1】:

    试试这个。这就是我在 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));
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多