0

私のプロジェクトの 1 つにスペルチェック機能を組み込む必要があり、hunspell は優れたスペルチェッカーであるため (多くのフリーおよびプロプライエタリ ソフトウェアで使用されています)、hunspell を使用することにしました。ソース コードをダウンロードし、プロジェクト libhunspell をプロジェクトに追加しました。エラーなしでコンパイルし、openoffice Web サイトから英語の辞書をダウンロードしました。以下は、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 を返します。

4

1 に答える 1

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));
   }
}
于 2013-01-29T12:59:10.930 に答える