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 "support/debug.h"
20 #include "support/docstring_list.h"
21 #include "support/filetools.h"
22 #include "support/Package.h"
23 #include "support/FileName.h"
24 #include "support/lassert.h"
25 #include "support/lstrings.h"
27 #include <hunspell/hunspell.hxx>
34 using namespace lyx::support;
35 using namespace lyx::support::os;
41 typedef map<std::string, Hunspell *> Spellers;
42 typedef map<std::string, PersonalWordList *> LangPersonalWordList;
44 docstring remap_result(docstring const & s)
46 // substitute RIGHT SINGLE QUOTATION MARK
48 return subst(s, 0x2019, 0x0027);
54 struct HunspellChecker::Private
60 void setUserPath(std::string const & path);
61 const string dictPath(int selector);
62 bool haveLanguageFiles(string const & hpath);
63 bool haveDictionary(Language const * lang, string & hpath);
64 bool haveDictionary(Language const * lang);
65 int numDictionaries() const;
66 Hunspell * addSpeller(Language const * lang, string & hpath);
67 Hunspell * addSpeller(Language const * lang);
68 Hunspell * speller(Language const * lang);
69 Hunspell * lookup(Language const * lang);
71 bool isIgnored(WordLangTuple const & wl) const;
72 /// personal word list interface
73 void remove(WordLangTuple const & wl);
74 void insert(WordLangTuple const & wl);
78 WordLangTable ignored_;
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) {
130 it->second = nullptr;
133 LangPersonalWordList::const_iterator pdit = personal_.begin();
134 LangPersonalWordList::const_iterator pdet = personal_.end();
136 for (; pdit != pdet; ++pdit) {
137 if (pdit->second == nullptr)
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 : nullptr;
230 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang, string & path)
232 if (!haveDictionary(lang, path)) {
233 spellers_[lang->lang()] = nullptr;
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)
248 Hunspell * h = nullptr;
249 for (int p = 0; p < maxLookupSelector() && nullptr == 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->includes_begin();
259 docstring_list::const_iterator et = pd->includes_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());
264 it = pd->excludes_begin();
265 et = pd->excludes_end();
266 for (; it != et; ++it) {
267 string const word_to_add = to_iconv_encoding(*it, encoding);
268 h->remove(word_to_add.c_str());
275 int HunspellChecker::Private::numDictionaries() const
278 Spellers::const_iterator it = spellers_.begin();
279 Spellers::const_iterator et = spellers_.end();
281 for (; it != et; ++it)
282 result += it->second != nullptr;
287 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
289 WordLangTable::const_iterator it = ignored_.begin();
290 for (; it != ignored_.end(); ++it) {
291 if (it->lang()->code() != wl.lang()->code())
293 if (it->word() == wl.word())
299 /// personal word list interface
300 void HunspellChecker::Private::remove(WordLangTuple const & wl)
302 Hunspell * h = speller(wl.lang());
305 string const encoding = h->get_dic_encoding();
306 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
307 h->remove(word_to_check.c_str());
308 PersonalWordList * pd = personal_[wl.lang()->lang()];
311 pd->remove(wl.word());
315 void HunspellChecker::Private::insert(WordLangTuple const & wl)
317 Hunspell * h = speller(wl.lang());
320 string const encoding = h->get_dic_encoding();
321 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
322 h->add(word_to_check.c_str());
323 PersonalWordList * pd = personal_[wl.lang()->lang()];
326 pd->insert(wl.word());
330 HunspellChecker::HunspellChecker()
335 HunspellChecker::~HunspellChecker()
341 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl,
342 vector<WordLangTuple> const & docdict)
344 if (d->isIgnored(wl))
347 WordLangTable::const_iterator it = docdict.begin();
348 for (; it != docdict.end(); ++it) {
349 if (it->lang()->code() != wl.lang()->code())
351 if (it->word() == wl.word())
352 return DOCUMENT_LEARNED_WORD;
355 Hunspell * h = d->speller(wl.lang());
357 return NO_DICTIONARY;
360 string const encoding = h->get_dic_encoding();
361 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
363 LYXERR(Debug::GUI, "spellCheck: \"" <<
364 wl.word() << "\", lang = " << wl.lang()->lang()) ;
365 #ifdef HAVE_HUNSPELL_CXXABI
366 if (h->spell(word_to_check, &info))
368 if (h->spell(word_to_check.c_str(), &info))
372 if (info & SPELL_COMPOUND) {
373 // FIXME: What to do with that?
374 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
376 if (info & SPELL_FORBIDDEN) {
377 // This was removed from personal dictionary
378 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
385 void HunspellChecker::advanceChangeNumber()
391 void HunspellChecker::insert(WordLangTuple const & wl)
394 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
395 advanceChangeNumber();
399 void HunspellChecker::remove(WordLangTuple const & wl)
402 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
403 advanceChangeNumber();
407 void HunspellChecker::accept(WordLangTuple const & wl)
409 d->ignored_.push_back(wl);
410 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
411 advanceChangeNumber();
415 void HunspellChecker::suggest(WordLangTuple const & wl,
416 docstring_list & suggestions)
419 Hunspell * h = d->speller(wl.lang());
422 string const encoding = h->get_dic_encoding();
423 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
424 #ifdef HAVE_HUNSPELL_CXXABI
425 vector<string> wlst = h->suggest(word_to_check);
426 for (auto const & s : wlst)
427 suggestions.push_back(remap_result(from_iconv_encoding(s, encoding)));
429 char ** suggestion_list;
430 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
431 if (suggestion_number <= 0)
433 for (int i = 0; i != suggestion_number; ++i)
434 suggestions.push_back(remap_result(from_iconv_encoding(suggestion_list[i], encoding)));
435 h->free_list(&suggestion_list, suggestion_number);
440 void HunspellChecker::stem(WordLangTuple const & wl,
441 docstring_list & suggestions)
444 Hunspell * h = d->speller(wl.lang());
447 string const encoding = h->get_dic_encoding();
448 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
449 #ifdef HAVE_HUNSPELL_CXXABI
450 vector<string> wlst = h->stem(word_to_check);
451 for (auto const & s : wlst)
452 suggestions.push_back(from_iconv_encoding(s, encoding));
454 char ** suggestion_list;
455 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
456 if (suggestion_number <= 0)
458 for (int i = 0; i != suggestion_number; ++i)
459 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
460 h->free_list(&suggestion_list, suggestion_number);
465 bool HunspellChecker::hasDictionary(Language const * lang) const
469 return d->haveDictionary(lang);
473 int HunspellChecker::numDictionaries() const
475 return d->numDictionaries();
479 docstring const HunspellChecker::error()