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;
50 docstring remap_result(docstring const s)
52 // substitute RIGHT SINGLE QUOTATION MARK
54 return subst(s, 0x2019, 0x0027);
60 struct HunspellChecker::Private
66 void setUserPath(std::string const & path);
67 const string dictPath(int selector);
68 bool haveLanguageFiles(string const & hpath);
69 bool haveDictionary(Language const * lang, string & hpath);
70 bool haveDictionary(Language const * lang);
71 int numDictionaries() const;
72 Hunspell * addSpeller(Language const * lang, string & hpath);
73 Hunspell * addSpeller(Language const * lang);
74 Hunspell * speller(Language const * lang);
75 Hunspell * lookup(Language const * lang);
77 bool isIgnored(WordLangTuple const & wl) const;
78 /// personal word list interface
79 void remove(WordLangTuple const & wl);
80 void insert(WordLangTuple const & wl);
81 bool learned(WordLangTuple const & wl);
87 LangPersonalWordList personal_;
89 std::string user_path_;
91 /// the location below system/user directory
92 /// there the aff+dic files lookup will happen
93 const string dictDirectory(void) const { return "dicts"; }
94 int maxLookupSelector(void) const { return 5; }
95 const string HunspellDictionaryName(Language const * lang) {
96 return lang->variety().empty()
98 : lang->code() + "-" + lang->variety();
100 const string myspellPackageDictDirectory(void) {
101 return "/usr/share/myspell";
103 const string hunspellPackageDictDirectory(void) {
104 return "/usr/share/hunspell";
109 HunspellChecker::Private::Private()
111 setUserPath(lyxrc.hunspelldir_path);
115 HunspellChecker::Private::~Private()
121 void HunspellChecker::Private::setUserPath(std::string const & path)
123 if (user_path_ != lyxrc.hunspelldir_path) {
130 void HunspellChecker::Private::cleanCache()
132 Spellers::iterator it = spellers_.begin();
133 Spellers::iterator end = spellers_.end();
135 for (; it != end; ++it) {
140 LangPersonalWordList::const_iterator pdit = personal_.begin();
141 LangPersonalWordList::const_iterator pdet = personal_.end();
143 for (; pdit != pdet; ++pdit) {
144 if ( 0 == pdit->second)
146 PersonalWordList * pd = pdit->second;
153 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
155 FileName const affix(hpath + ".aff");
156 FileName const dict(hpath + ".dic");
157 return affix.isReadableFile() && dict.isReadableFile();
161 const string HunspellChecker::Private::dictPath(int selector)
165 return hunspellPackageDictDirectory();
167 return myspellPackageDictDirectory();
169 return addName(package().system_support().absFileName(),dictDirectory());
171 return addName(package().user_support().absFileName(),dictDirectory());
178 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
180 if (hpath.empty() || !lang)
183 if (lookup(lang)) return true;
185 string d_name = HunspellDictionaryName(lang);
187 LYXERR(Debug::FILES, "check hunspell path: " << hpath
188 << " for language " << lang->lang() << " with name " << d_name);
190 string h_path = addName(hpath, d_name);
191 // first we try lang code+variety
192 if (haveLanguageFiles(h_path)) {
193 LYXERR(Debug::FILES, " found " << h_path);
197 // another try with code, '_' replaced by '-'
198 h_path = addName(hpath, subst(lang->code(), '_', '-'));
199 if (!haveLanguageFiles(h_path))
201 LYXERR(Debug::FILES, " found " << h_path);
207 bool HunspellChecker::Private::haveDictionary(Language const * lang)
211 setUserPath(lyxrc.hunspelldir_path);
212 for (int p = 0; !result && p < maxLookupSelector(); ++p) {
213 string lpath = dictPath(p);
214 result = haveDictionary(lang, lpath);
220 Hunspell * HunspellChecker::Private::speller(Language const * lang)
222 Hunspell * h = lookup(lang);
225 setUserPath(lyxrc.hunspelldir_path);
226 return addSpeller(lang);
230 Hunspell * HunspellChecker::Private::lookup(Language const * lang)
232 Spellers::iterator it = spellers_.find(lang->lang());
233 return it != spellers_.end() ? it->second : 0;
237 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
239 if (!haveDictionary(lang, path)) {
240 spellers_[lang->lang()] = 0;
244 FileName const affix(path + ".aff");
245 FileName const dict(path + ".dic");
246 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
247 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " added.");
248 spellers_[lang->lang()] = h;
253 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
256 for (int p = 0; p < maxLookupSelector() && 0 == h; ++p) {
257 string lpath = dictPath(p);
258 h = addSpeller(lang, lpath);
261 string const encoding = h->get_dic_encoding();
262 PersonalWordList * pd = new PersonalWordList(lang->lang());
264 personal_[lang->lang()] = pd;
265 docstring_list::const_iterator it = pd->begin();
266 docstring_list::const_iterator et = pd->end();
267 for (; it != et; ++it) {
268 string const word_to_add = to_iconv_encoding(*it, encoding);
269 h->add(word_to_add.c_str());
276 int HunspellChecker::Private::numDictionaries() const
279 Spellers::const_iterator it = spellers_.begin();
280 Spellers::const_iterator et = spellers_.end();
282 for (; it != et; ++it)
283 result += it->second != 0;
288 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
290 IgnoreList::const_iterator it = ignored_.begin();
291 for (; it != ignored_.end(); ++it) {
292 if (it->lang()->code() != wl.lang()->code())
294 if (it->word() == wl.word())
300 /// personal word list interface
301 void HunspellChecker::Private::remove(WordLangTuple const & wl)
303 Hunspell * h = speller(wl.lang());
306 string const encoding = h->get_dic_encoding();
307 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
308 h->remove(word_to_check.c_str());
309 PersonalWordList * pd = personal_[wl.lang()->lang()];
312 pd->remove(wl.word());
316 void HunspellChecker::Private::insert(WordLangTuple const & wl)
318 Hunspell * h = speller(wl.lang());
321 string const encoding = h->get_dic_encoding();
322 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
323 h->add(word_to_check.c_str());
324 PersonalWordList * pd = personal_[wl.lang()->lang()];
327 pd->insert(wl.word());
331 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
333 PersonalWordList * pd = personal_[wl.lang()->lang()];
336 return pd->exists(wl.word());
340 HunspellChecker::HunspellChecker()
345 HunspellChecker::~HunspellChecker()
351 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
353 if (d->isIgnored(wl))
356 Hunspell * h = d->speller(wl.lang());
358 return NO_DICTIONARY;
361 string const encoding = h->get_dic_encoding();
362 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
364 LYXERR(Debug::GUI, "spellCheck: \"" <<
365 wl.word() << "\", lang = " << wl.lang()->lang()) ;
366 #ifdef HAVE_HUNSPELL_CXXABI
367 if (h->spell(word_to_check, &info))
369 if (h->spell(word_to_check.c_str(), &info))
371 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
373 if (info & SPELL_COMPOUND) {
374 // FIXME: What to do with that?
375 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
377 if (info & SPELL_FORBIDDEN) {
378 // This was removed from personal dictionary
379 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
386 void HunspellChecker::advanceChangeNumber()
392 void HunspellChecker::insert(WordLangTuple const & wl)
395 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
396 advanceChangeNumber();
400 void HunspellChecker::remove(WordLangTuple const & wl)
403 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
404 advanceChangeNumber();
408 void HunspellChecker::accept(WordLangTuple const & wl)
410 d->ignored_.push_back(wl);
411 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
412 advanceChangeNumber();
416 void HunspellChecker::suggest(WordLangTuple const & wl,
417 docstring_list & suggestions)
420 Hunspell * h = d->speller(wl.lang());
423 string const encoding = h->get_dic_encoding();
424 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
425 #ifdef HAVE_HUNSPELL_CXXABI
426 vector<string> wlst = h->suggest(word_to_check);
427 for (auto const & s : wlst)
428 suggestions.push_back(remap_result(from_iconv_encoding(s, encoding)));
430 char ** suggestion_list;
431 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
432 if (suggestion_number <= 0)
434 for (int i = 0; i != suggestion_number; ++i)
435 suggestions.push_back(remap_result(from_iconv_encoding(suggestion_list[i], encoding)));
436 h->free_list(&suggestion_list, suggestion_number);
441 void HunspellChecker::stem(WordLangTuple const & wl,
442 docstring_list & suggestions)
445 Hunspell * h = d->speller(wl.lang());
448 string const encoding = h->get_dic_encoding();
449 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
450 #ifdef HAVE_HUNSPELL_CXXABI
451 vector<string> wlst = h->stem(word_to_check);
452 for (auto const & s : wlst)
453 suggestions.push_back(from_iconv_encoding(s, encoding));
455 char ** suggestion_list;
456 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
457 if (suggestion_number <= 0)
459 for (int i = 0; i != suggestion_number; ++i)
460 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
461 h->free_list(&suggestion_list, suggestion_number);
466 bool HunspellChecker::hasDictionary(Language const * lang) const
470 return d->haveDictionary(lang);
474 int HunspellChecker::numDictionaries() const
476 return d->numDictionaries();
480 docstring const HunspellChecker::error()