2003-03-26 01:20:25 +00:00
|
|
|
/**
|
2009-08-01 17:24:13 +00:00
|
|
|
* \file AspellChecker.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2003-03-26 01:20:25 +00:00
|
|
|
*
|
|
|
|
* \author Kevin Atkinson
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-03-26 01:20:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
#include "AspellChecker.h"
|
2007-12-17 16:51:23 +00:00
|
|
|
#include "LyXRC.h"
|
2003-03-26 01:20:25 +00:00
|
|
|
#include "WordLangTuple.h"
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2009-08-01 17:48:33 +00:00
|
|
|
#include "support/debug.h"
|
2009-08-02 09:17:32 +00:00
|
|
|
#include "support/docstring_list.h"
|
2009-08-01 17:48:33 +00:00
|
|
|
|
|
|
|
#include <aspell.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2003-09-12 07:41:09 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2003-10-07 13:32:17 +00:00
|
|
|
|
2006-10-21 07:26:07 +00:00
|
|
|
namespace lyx {
|
2003-09-05 16:31:30 +00:00
|
|
|
|
2009-08-01 17:48:33 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Speller {
|
|
|
|
AspellSpeller * speller;
|
|
|
|
AspellConfig * config;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<std::string, Speller> Spellers;
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
struct AspellChecker::Private
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2009-08-02 09:17:32 +00:00
|
|
|
Private(): spell_error_object(0) {}
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2009-08-01 17:48:33 +00:00
|
|
|
~Private();
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2010-01-18 17:02:05 +00:00
|
|
|
/// add a speller of the given language and variety
|
|
|
|
AspellSpeller * addSpeller(string const & lang,
|
|
|
|
string const & variety = string());
|
2009-08-02 09:17:32 +00:00
|
|
|
|
|
|
|
///
|
2010-01-18 17:02:05 +00:00
|
|
|
AspellSpeller * speller(string const & lang,
|
|
|
|
string const & variety);
|
|
|
|
|
|
|
|
/// create a unique ID from lang code and variety
|
|
|
|
string const spellerID(string const & lang,
|
|
|
|
string const & variety);
|
2009-08-01 17:48:33 +00:00
|
|
|
|
|
|
|
/// the spellers
|
|
|
|
Spellers spellers_;
|
|
|
|
|
|
|
|
/// FIXME
|
|
|
|
AspellCanHaveError * spell_error_object;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
AspellChecker::Private::~Private()
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
|
|
|
if (spell_error_object) {
|
|
|
|
delete_aspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Spellers::iterator it = spellers_.begin();
|
|
|
|
Spellers::iterator end = spellers_.end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
aspell_speller_save_all_word_lists(it->second.speller);
|
|
|
|
delete_aspell_speller(it->second.speller);
|
|
|
|
delete_aspell_config(it->second.config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-18 17:02:05 +00:00
|
|
|
AspellSpeller * AspellChecker::Private::addSpeller(string const & lang,
|
|
|
|
string const & variety)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
|
|
|
AspellConfig * config = new_aspell_config();
|
2010-01-18 17:02:05 +00:00
|
|
|
// Aspell supports both languages and varieties (such as German
|
|
|
|
// old vs. new spelling). The respective naming convention is
|
|
|
|
// lang_REGION-variety (e.g. de_DE-alt).
|
|
|
|
aspell_config_replace(config, "lang", lang.c_str());
|
|
|
|
if (!variety.empty())
|
|
|
|
aspell_config_replace(config, "variety", variety.c_str());
|
2006-12-12 08:17:22 +00:00
|
|
|
// Set the encoding to utf-8.
|
|
|
|
// aspell does also understand "ucs-4", so we would not need a
|
|
|
|
// conversion in theory, but if this is used it expects all
|
|
|
|
// char const * arguments to be a cast from uint const *, and it
|
|
|
|
// seems that this uint is not compatible with our char_type on some
|
|
|
|
// platforms (cygwin, OS X). Therefore we use utf-8, that does
|
|
|
|
// always work.
|
2006-12-11 20:39:15 +00:00
|
|
|
aspell_config_replace(config, "encoding", "utf-8");
|
2008-11-15 20:06:06 +00:00
|
|
|
if (lyxrc.spellchecker_accept_compound)
|
2007-12-17 16:51:23 +00:00
|
|
|
// Consider run-together words as legal compounds
|
|
|
|
aspell_config_replace(config, "run-together", "true");
|
|
|
|
else
|
|
|
|
// Report run-together words as errors
|
|
|
|
aspell_config_replace(config, "run-together", "false");
|
2003-03-26 01:20:25 +00:00
|
|
|
AspellCanHaveError * err = new_aspell_speller(config);
|
|
|
|
if (spell_error_object)
|
|
|
|
delete_aspell_can_have_error(spell_error_object);
|
|
|
|
spell_error_object = 0;
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
if (aspell_error_number(err) != 0) {
|
|
|
|
// FIXME: We should we indicate somehow that this language is not
|
|
|
|
// supported.
|
2003-03-26 01:20:25 +00:00
|
|
|
spell_error_object = err;
|
2009-08-02 09:17:32 +00:00
|
|
|
return 0;
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
2009-08-02 09:17:32 +00:00
|
|
|
Speller m;
|
|
|
|
m.speller = to_aspell_speller(err);
|
|
|
|
m.config = config;
|
2010-01-18 17:02:05 +00:00
|
|
|
spellers_[spellerID(lang, variety)] = m;
|
2009-08-02 09:17:32 +00:00
|
|
|
return m.speller;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-18 17:02:05 +00:00
|
|
|
AspellSpeller * AspellChecker::Private::speller(string const & lang,
|
|
|
|
string const & variety)
|
2009-08-02 09:17:32 +00:00
|
|
|
{
|
2010-01-18 17:02:05 +00:00
|
|
|
Spellers::iterator it = spellers_.find(spellerID(lang, variety));
|
2009-08-02 09:17:32 +00:00
|
|
|
if (it != spellers_.end())
|
|
|
|
return it->second.speller;
|
|
|
|
|
2010-01-18 17:02:05 +00:00
|
|
|
return addSpeller(lang, variety);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const AspellChecker::Private::spellerID(string const & lang,
|
|
|
|
string const & variety)
|
|
|
|
{
|
|
|
|
if (variety.empty())
|
|
|
|
return lang;
|
|
|
|
return lang + "-" + variety;
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:48:33 +00:00
|
|
|
AspellChecker::AspellChecker(): d(new Private)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AspellChecker::~AspellChecker()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SpellChecker::Result AspellChecker::check(WordLangTuple const & word)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2010-01-18 17:02:05 +00:00
|
|
|
|
|
|
|
AspellSpeller * m =
|
|
|
|
d->speller(word.lang_code(), word.lang_variety());
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
if (!m)
|
|
|
|
return OK;
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2009-06-22 20:59:56 +00:00
|
|
|
if (word.word().empty())
|
|
|
|
// MSVC compiled Aspell doesn't like it.
|
|
|
|
return OK;
|
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(word_ok != -1, /**/);
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
return (word_ok) ? OK : UNKNOWN_WORD;
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
void AspellChecker::insert(WordLangTuple const & word)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2010-01-18 17:02:05 +00:00
|
|
|
Spellers::iterator it = d->spellers_.find(
|
|
|
|
d->spellerID(word.lang_code(), word.lang_variety()));
|
2009-08-01 17:48:33 +00:00
|
|
|
if (it != d->spellers_.end())
|
2006-12-08 19:46:16 +00:00
|
|
|
aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
void AspellChecker::accept(WordLangTuple const & word)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2010-01-18 17:02:05 +00:00
|
|
|
Spellers::iterator it = d->spellers_.find(
|
|
|
|
d->spellerID(word.lang_code(), word.lang_variety()));
|
2009-08-01 17:48:33 +00:00
|
|
|
if (it != d->spellers_.end())
|
2006-12-08 19:46:16 +00:00
|
|
|
aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
void AspellChecker::suggest(WordLangTuple const & wl,
|
|
|
|
docstring_list & suggestions)
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
2009-08-02 09:17:32 +00:00
|
|
|
suggestions.clear();
|
2010-01-18 17:02:05 +00:00
|
|
|
AspellSpeller * m =
|
|
|
|
d->speller(wl.lang_code(), wl.lang_variety());
|
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
if (!m)
|
|
|
|
return;
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
AspellWordList const * sugs =
|
|
|
|
aspell_speller_suggest(m, to_utf8(wl.word()).c_str(), -1);
|
|
|
|
LASSERT(sugs != 0, /**/);
|
|
|
|
AspellStringEnumeration * els = aspell_word_list_elements(sugs);
|
|
|
|
if (!els || aspell_word_list_empty(sugs))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char const * str = aspell_string_enumeration_next(els);
|
|
|
|
if (!str)
|
|
|
|
break;
|
|
|
|
suggestions.push_back(from_utf8(str));
|
|
|
|
}
|
2005-01-05 20:21:27 +00:00
|
|
|
|
2009-08-02 09:17:32 +00:00
|
|
|
delete_aspell_string_enumeration(els);
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-01 17:24:13 +00:00
|
|
|
docstring const AspellChecker::error()
|
2003-03-26 01:20:25 +00:00
|
|
|
{
|
|
|
|
char const * err = 0;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2009-08-01 17:48:33 +00:00
|
|
|
if (d->spell_error_object && aspell_error_number(d->spell_error_object) != 0)
|
|
|
|
err = aspell_error_message(d->spell_error_object);
|
2003-03-26 01:20:25 +00:00
|
|
|
|
2006-12-08 19:46:16 +00:00
|
|
|
// FIXME UNICODE: err is not in UTF8, but probably the locale encoding
|
2006-10-21 00:16:43 +00:00
|
|
|
return (err ? from_utf8(err) : docstring());
|
2003-03-26 01:20:25 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|