2009-07-12 22:46:01 +00:00
|
|
|
/**
|
2009-08-01 17:24:13 +00:00
|
|
|
* \file HunspellChecker.cpp
|
2009-07-12 22:46:01 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
#include "HunspellChecker.h"
|
2009-07-12 22:46:01 +00:00
|
|
|
|
|
|
|
#include "LyXRC.h"
|
|
|
|
#include "WordLangTuple.h"
|
|
|
|
|
2009-11-25 16:52:45 +00:00
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
2009-07-12 22:46:01 +00:00
|
|
|
#include "support/debug.h"
|
2009-08-02 09:17:32 +00:00
|
|
|
#include "support/docstring_list.h"
|
2009-12-05 03:55:03 +00:00
|
|
|
#include "support/filetools.h"
|
2009-08-08 17:05:31 +00:00
|
|
|
#include "support/FileName.h"
|
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lassert.h"
|
2009-11-25 16:52:45 +00:00
|
|
|
#include "support/lstrings.h"
|
2009-08-08 17:05:31 +00:00
|
|
|
#include "support/os.h"
|
2009-07-12 22:46:01 +00:00
|
|
|
|
|
|
|
#include <hunspell/hunspell.hxx>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2010-01-21 12:47:45 +00:00
|
|
|
#include <vector>
|
2009-07-12 22:46:01 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2009-08-08 17:05:31 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
using namespace lyx::support::os;
|
2009-07-12 22:46:01 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
namespace {
|
2009-08-01 18:03:26 +00:00
|
|
|
|
2009-07-12 22:46:01 +00:00
|
|
|
typedef map<std::string, Hunspell *> Spellers;
|
2010-01-21 12:47:45 +00:00
|
|
|
typedef vector<WordLangTuple> IgnoreList;
|
2009-07-12 22:46:01 +00:00
|
|
|
|
2009-08-01 18:03:26 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
struct HunspellChecker::Private
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
2009-08-02 09:17:32 +00:00
|
|
|
Private() {}
|
|
|
|
|
|
|
|
~Private();
|
|
|
|
|
2010-02-10 09:13:58 +00:00
|
|
|
bool haveDictionary(string const & lang, string & hpath);
|
2009-08-02 09:17:32 +00:00
|
|
|
Hunspell * addSpeller(string const & lang);
|
|
|
|
Hunspell * speller(string const & lang);
|
2010-01-21 12:47:45 +00:00
|
|
|
/// ignored words
|
|
|
|
bool isIgnored(WordLangTuple const & wl) const;
|
2009-08-02 09:17:32 +00:00
|
|
|
|
2009-07-12 22:46:01 +00:00
|
|
|
/// the spellers
|
|
|
|
Spellers spellers_;
|
2010-01-21 12:47:45 +00:00
|
|
|
///
|
|
|
|
IgnoreList ignored_;
|
2009-07-12 22:46:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
HunspellChecker::Private::~Private()
|
|
|
|
{
|
|
|
|
Spellers::iterator it = spellers_.begin();
|
|
|
|
Spellers::iterator end = spellers_.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 16:52:45 +00:00
|
|
|
namespace {
|
|
|
|
bool haveLanguageFiles(string const & hpath)
|
2009-08-02 09:17:32 +00:00
|
|
|
{
|
2009-11-25 16:52:45 +00:00
|
|
|
FileName const affix(hpath + ".aff");
|
|
|
|
FileName const dict(hpath + ".dic");
|
2009-08-08 17:05:31 +00:00
|
|
|
if (!affix.isReadableFile()) {
|
|
|
|
// FIXME: We should indicate somehow that this language is not
|
|
|
|
// supported.
|
|
|
|
LYXERR(Debug::FILES, "Hunspell affix file " << affix << " does not exist");
|
2009-11-25 16:52:45 +00:00
|
|
|
return false;
|
2009-08-08 17:05:31 +00:00
|
|
|
}
|
|
|
|
if (!dict.isReadableFile()) {
|
|
|
|
LYXERR(Debug::FILES, "Hunspell dictionary file " << dict << " does not exist");
|
2009-11-25 16:52:45 +00:00
|
|
|
return false;
|
2009-08-08 17:05:31 +00:00
|
|
|
}
|
2009-11-25 16:52:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-10 09:13:58 +00:00
|
|
|
bool HunspellChecker::Private::haveDictionary(string const & lang, string & hunspell_path)
|
2009-11-25 16:52:45 +00:00
|
|
|
{
|
2009-12-05 03:55:03 +00:00
|
|
|
LYXERR(Debug::FILES, "hunspell path: " << external_path(hunspell_path));
|
2009-11-25 16:52:45 +00:00
|
|
|
if (hunspell_path.empty()) {
|
2009-12-04 17:38:30 +00:00
|
|
|
// FIXME We'd like to issue a better error message here, but there seems
|
|
|
|
// to be a problem about thread safety, or something of the sort. If
|
|
|
|
// we issue the message using frontend::Alert, then the code comes
|
|
|
|
// back through here while the box is waiting, and causes some kind
|
|
|
|
// of crash.
|
2009-11-25 16:52:45 +00:00
|
|
|
static bool warned = false;
|
|
|
|
if (!warned) {
|
|
|
|
warned = true;
|
2009-12-04 17:38:30 +00:00
|
|
|
LYXERR0("Hunspell path not set.");
|
|
|
|
//frontend::Alert::error(_("Hunspell Path Not Found"),
|
|
|
|
// _("You must set the Hunspell dictionary path in Tools>Preferences>Paths."));
|
2009-11-25 16:52:45 +00:00
|
|
|
}
|
2010-02-10 08:10:31 +00:00
|
|
|
return false;
|
2009-11-25 16:52:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-05 03:55:03 +00:00
|
|
|
hunspell_path = external_path(addName(hunspell_path, lang));
|
2009-11-25 16:52:45 +00:00
|
|
|
if (!haveLanguageFiles(hunspell_path)) {
|
|
|
|
// try with '_' replaced by '-'
|
|
|
|
hunspell_path = subst(hunspell_path, '_', '-');
|
|
|
|
if (!haveLanguageFiles(hunspell_path)) {
|
|
|
|
// FIXME: We should indicate somehow that this language is not
|
|
|
|
// supported, probably by popping a warning. But we'll need to
|
|
|
|
// remember which warnings we've issued.
|
2010-02-10 08:10:31 +00:00
|
|
|
return false;
|
2009-11-25 16:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-10 08:10:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
|
|
|
|
{
|
|
|
|
string hunspell_path = lyxrc.hunspelldir_path;
|
|
|
|
|
2010-02-10 09:13:58 +00:00
|
|
|
if (!haveDictionary(lang, hunspell_path))
|
2010-02-10 08:10:31 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-11-25 16:52:45 +00:00
|
|
|
FileName const affix(hunspell_path + ".aff");
|
|
|
|
FileName const dict(hunspell_path + ".dic");
|
2010-04-21 01:19:09 +00:00
|
|
|
Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
|
2009-08-08 17:05:31 +00:00
|
|
|
spellers_[lang] = h;
|
|
|
|
return h;
|
2009-08-02 09:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Hunspell * HunspellChecker::Private::speller(string const & lang)
|
|
|
|
{
|
|
|
|
Spellers::iterator it = spellers_.find(lang);
|
|
|
|
if (it != spellers_.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return addSpeller(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-21 12:47:45 +00:00
|
|
|
bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
|
|
|
|
{
|
|
|
|
IgnoreList::const_iterator it = ignored_.begin();
|
|
|
|
for (; it != ignored_.end(); ++it) {
|
2010-02-09 11:26:49 +00:00
|
|
|
if ((*it).lang()->code() != wl.lang()->code())
|
2010-01-21 12:47:45 +00:00
|
|
|
continue;
|
|
|
|
if ((*it).word() == wl.word())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
HunspellChecker::HunspellChecker(): d(new Private)
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
HunspellChecker::~HunspellChecker()
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
2010-01-21 12:47:45 +00:00
|
|
|
if (d->isIgnored(wl))
|
|
|
|
return OK;
|
|
|
|
|
2010-02-09 11:26:49 +00:00
|
|
|
Hunspell * h = d->speller(wl.lang()->code());
|
2009-08-08 17:05:31 +00:00
|
|
|
if (!h)
|
|
|
|
return OK;
|
2009-08-02 09:17:32 +00:00
|
|
|
int info;
|
2010-02-10 10:24:10 +00:00
|
|
|
|
|
|
|
string const encoding = h->get_dic_encoding();
|
|
|
|
string const word_to_check = to_iconv_encoding(wl.word(), encoding);
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
if (h->spell(word_to_check.c_str(), &info))
|
|
|
|
return OK;
|
2009-08-08 17:05:31 +00:00
|
|
|
|
|
|
|
if (info & SPELL_COMPOUND) {
|
|
|
|
// FIXME: What to do with that?
|
|
|
|
LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
|
|
|
|
}
|
|
|
|
if (info & SPELL_FORBIDDEN) {
|
|
|
|
// FIXME: What to do with that?
|
|
|
|
LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
|
2009-08-02 09:17:32 +00:00
|
|
|
}
|
2009-08-08 17:05:31 +00:00
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
return UNKNOWN_WORD;
|
2009-07-12 22:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
void HunspellChecker::insert(WordLangTuple const & wl)
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
2009-08-02 09:17:32 +00:00
|
|
|
string const word_to_check = to_utf8(wl.word());
|
2010-02-09 11:26:49 +00:00
|
|
|
Hunspell * h = d->speller(wl.lang()->code());
|
2009-08-08 17:05:31 +00:00
|
|
|
if (!h)
|
|
|
|
return;
|
2009-08-02 09:17:32 +00:00
|
|
|
h->add(word_to_check.c_str());
|
2009-07-12 22:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-21 12:47:45 +00:00
|
|
|
void HunspellChecker::accept(WordLangTuple const & wl)
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
2010-01-21 12:47:45 +00:00
|
|
|
d->ignored_.push_back(wl);
|
2009-07-12 22:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
void HunspellChecker::suggest(WordLangTuple const & wl,
|
|
|
|
docstring_list & suggestions)
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
2009-08-02 09:17:32 +00:00
|
|
|
suggestions.clear();
|
2010-02-09 11:26:49 +00:00
|
|
|
Hunspell * h = d->speller(wl.lang()->code());
|
2009-08-08 17:05:31 +00:00
|
|
|
if (!h)
|
|
|
|
return;
|
2010-02-10 10:24:10 +00:00
|
|
|
string const encoding = h->get_dic_encoding();
|
|
|
|
string const word_to_check = to_iconv_encoding(wl.word(), encoding);
|
2009-08-09 13:43:58 +00:00
|
|
|
char ** suggestion_list;
|
|
|
|
int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
|
|
|
|
if (suggestion_number <= 0)
|
2009-08-02 09:17:32 +00:00
|
|
|
return;
|
2009-08-09 13:43:58 +00:00
|
|
|
for (int i = 0; i != suggestion_number; ++i)
|
2010-02-10 10:24:10 +00:00
|
|
|
suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
|
2009-08-09 13:43:58 +00:00
|
|
|
h->free_list(&suggestion_list, suggestion_number);
|
2009-07-12 22:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-10 08:10:31 +00:00
|
|
|
bool HunspellChecker::hasDictionary(Language const * lang) const
|
|
|
|
{
|
|
|
|
if (!lang)
|
|
|
|
return false;
|
2010-02-10 09:13:58 +00:00
|
|
|
string hunspell_path = lyxrc.hunspelldir_path;
|
|
|
|
return (d->haveDictionary(lang->code(), hunspell_path));
|
2010-02-10 08:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
docstring const HunspellChecker::error()
|
2009-07-12 22:46:01 +00:00
|
|
|
{
|
|
|
|
return docstring();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|