2 * \file HunspellChecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Abdelrazak Younes
8 * Full author contact details are available in file CREDITS.
13 #include "HunspellChecker.h"
14 #include "PersonalWordList.h"
17 #include "WordLangTuple.h"
19 #include "frontends/alert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
23 #include "support/filetools.h"
24 #include "support/Package.h"
25 #include "support/FileName.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29 #include "support/os.h"
31 #include <hunspell/hunspell.hxx>
38 using namespace lyx::support;
39 using namespace lyx::support::os;
45 typedef map<std::string, Hunspell *> Spellers;
46 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
48 typedef vector<WordLangTuple> IgnoreList;
53 struct HunspellChecker::Private
59 void setUserPath(std::string const & path);
60 const string dictPath(int selector);
61 bool haveLanguageFiles(string const & hpath);
62 bool haveDictionary(Language const * lang, string & hpath);
63 bool haveDictionary(Language const * lang);
64 int numDictionaries() const;
65 Hunspell * addSpeller(Language const * lang, string & hpath);
66 Hunspell * addSpeller(Language const * lang);
67 Hunspell * speller(Language const * lang);
68 Hunspell * lookup(Language const * lang);
70 bool isIgnored(WordLangTuple const & wl) const;
71 /// personal word list interface
72 void remove(WordLangTuple const & wl);
73 void insert(WordLangTuple const & wl);
74 bool learned(WordLangTuple const & wl);
80 LangPersonalWordList personal_;
82 std::string user_path_;
84 /// the location below system/user directory
85 /// there the aff+dic files lookup will happen
86 const string dictDirectory(void) const { return "dicts"; }
87 int maxLookupSelector(void) const { return 5; }
88 const string HunspellDictionaryName(Language const * lang) {
89 return lang->variety().empty()
91 : lang->code() + "-" + lang->variety();
93 const string myspellPackageDictDirectory(void) {
94 return "/usr/share/myspell";
96 const string hunspellPackageDictDirectory(void) {
97 return "/usr/share/hunspell";
102 HunspellChecker::Private::Private()
104 setUserPath(lyxrc.hunspelldir_path);
108 HunspellChecker::Private::~Private()
114 void HunspellChecker::Private::setUserPath(std::string const & path)
116 if (user_path_ != lyxrc.hunspelldir_path) {
123 void HunspellChecker::Private::cleanCache()
125 Spellers::iterator it = spellers_.begin();
126 Spellers::iterator end = spellers_.end();
128 for (; it != end; ++it) {
133 LangPersonalWordList::const_iterator pdit = personal_.begin();
134 LangPersonalWordList::const_iterator pdet = personal_.end();
136 for (; pdit != pdet; ++pdit) {
137 if ( 0 == pdit->second)
139 PersonalWordList * pd = pdit->second;
146 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
148 FileName const affix(hpath + ".aff");
149 FileName const dict(hpath + ".dic");
150 return affix.isReadableFile() && dict.isReadableFile();
154 const string HunspellChecker::Private::dictPath(int selector)
158 return hunspellPackageDictDirectory();
160 return myspellPackageDictDirectory();
162 return addName(package().system_support().absFileName(),dictDirectory());
164 return addName(package().user_support().absFileName(),dictDirectory());
171 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
173 if (hpath.empty() || !lang)
176 if (lookup(lang)) return true;
178 string d_name = HunspellDictionaryName(lang);
180 LYXERR(Debug::FILES, "check hunspell path: " << hpath
181 << " for language " << lang->lang() << " with name " << d_name);
183 string h_path = addName(hpath, d_name);
184 // first we try lang code+variety
185 if (haveLanguageFiles(h_path)) {
186 LYXERR(Debug::FILES, " found " << h_path);
190 // another try with code, '_' replaced by '-'
191 h_path = addName(hpath, subst(lang->code(), '_', '-'));
192 if (!haveLanguageFiles(h_path))
194 LYXERR(Debug::FILES, " found " << h_path);
200 bool HunspellChecker::Private::haveDictionary(Language const * lang)
204 setUserPath(lyxrc.hunspelldir_path);
205 for (int p = 0; !result && p < maxLookupSelector(); ++p) {
206 string lpath = dictPath(p);
207 result = haveDictionary(lang, lpath);
213 Hunspell * HunspellChecker::Private::speller(Language const * lang)
215 Hunspell * h = lookup(lang);
218 setUserPath(lyxrc.hunspelldir_path);
219 return addSpeller(lang);
223 Hunspell * HunspellChecker::Private::lookup(Language const * lang)
225 Spellers::iterator it = spellers_.find(lang->lang());
226 return it != spellers_.end() ? it->second : 0;
230 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
232 if (!haveDictionary(lang, path)) {
233 spellers_[lang->lang()] = 0;
237 FileName const affix(path + ".aff");
238 FileName const dict(path + ".dic");
239 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
240 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " added.");
241 spellers_[lang->lang()] = h;
246 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
249 for (int p = 0; p < maxLookupSelector() && 0 == h; ++p) {
250 string lpath = dictPath(p);
251 h = addSpeller(lang, lpath);
254 string const encoding = h->get_dic_encoding();
255 PersonalWordList * pd = new PersonalWordList(lang->lang());
257 personal_[lang->lang()] = pd;
258 docstring_list::const_iterator it = pd->begin();
259 docstring_list::const_iterator et = pd->end();
260 for (; it != et; ++it) {
261 string const word_to_add = to_iconv_encoding(*it, encoding);
262 h->add(word_to_add.c_str());
269 int HunspellChecker::Private::numDictionaries() const
272 Spellers::const_iterator it = spellers_.begin();
273 Spellers::const_iterator et = spellers_.end();
275 for (; it != et; ++it)
276 result += it->second != 0;
281 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
283 IgnoreList::const_iterator it = ignored_.begin();
284 for (; it != ignored_.end(); ++it) {
285 if (it->lang()->code() != wl.lang()->code())
287 if (it->word() == wl.word())
293 /// personal word list interface
294 void HunspellChecker::Private::remove(WordLangTuple const & wl)
296 Hunspell * h = speller(wl.lang());
299 string const encoding = h->get_dic_encoding();
300 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
301 h->remove(word_to_check.c_str());
302 PersonalWordList * pd = personal_[wl.lang()->lang()];
305 pd->remove(wl.word());
309 void HunspellChecker::Private::insert(WordLangTuple const & wl)
311 Hunspell * h = speller(wl.lang());
314 string const encoding = h->get_dic_encoding();
315 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
316 h->add(word_to_check.c_str());
317 PersonalWordList * pd = personal_[wl.lang()->lang()];
320 pd->insert(wl.word());
324 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
326 PersonalWordList * pd = personal_[wl.lang()->lang()];
329 return pd->exists(wl.word());
333 HunspellChecker::HunspellChecker()
338 HunspellChecker::~HunspellChecker()
344 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
346 if (d->isIgnored(wl))
349 Hunspell * h = d->speller(wl.lang());
351 return NO_DICTIONARY;
354 string const encoding = h->get_dic_encoding();
355 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
357 LYXERR(Debug::GUI, "spellCheck: \"" <<
358 wl.word() << "\", lang = " << wl.lang()->lang()) ;
359 if (h->spell(word_to_check.c_str(), &info))
360 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
362 if (info & SPELL_COMPOUND) {
363 // FIXME: What to do with that?
364 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
366 if (info & SPELL_FORBIDDEN) {
367 // This was removed from personal dictionary
368 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
375 void HunspellChecker::advanceChangeNumber()
381 void HunspellChecker::insert(WordLangTuple const & wl)
384 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
385 advanceChangeNumber();
389 void HunspellChecker::remove(WordLangTuple const & wl)
392 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
393 advanceChangeNumber();
397 void HunspellChecker::accept(WordLangTuple const & wl)
399 d->ignored_.push_back(wl);
400 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
401 advanceChangeNumber();
405 void HunspellChecker::suggest(WordLangTuple const & wl,
406 docstring_list & suggestions)
409 Hunspell * h = d->speller(wl.lang());
412 string const encoding = h->get_dic_encoding();
413 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
414 char ** suggestion_list;
415 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
416 if (suggestion_number <= 0)
418 for (int i = 0; i != suggestion_number; ++i)
419 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
420 h->free_list(&suggestion_list, suggestion_number);
424 void HunspellChecker::stem(WordLangTuple const & wl,
425 docstring_list & suggestions)
428 Hunspell * h = d->speller(wl.lang());
431 string const encoding = h->get_dic_encoding();
432 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
433 char ** suggestion_list;
434 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
435 if (suggestion_number <= 0)
437 for (int i = 0; i != suggestion_number; ++i)
438 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
439 h->free_list(&suggestion_list, suggestion_number);
443 bool HunspellChecker::hasDictionary(Language const * lang) const
447 return d->haveDictionary(lang);
451 int HunspellChecker::numDictionaries() const
453 return d->numDictionaries();
457 docstring const HunspellChecker::error()