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 typedef vector<WordLangTuple> IgnoreList;
46 docstring remap_result(docstring const & s)
48 // substitute RIGHT SINGLE QUOTATION MARK
50 return subst(s, 0x2019, 0x0027);
56 struct HunspellChecker::Private
62 void setUserPath(std::string const & path);
63 const string dictPath(int selector);
64 bool haveLanguageFiles(string const & hpath);
65 bool haveDictionary(Language const * lang, string & hpath);
66 bool haveDictionary(Language const * lang);
67 int numDictionaries() const;
68 Hunspell * addSpeller(Language const * lang, string & hpath);
69 Hunspell * addSpeller(Language const * lang);
70 Hunspell * speller(Language const * lang);
71 Hunspell * lookup(Language const * lang);
73 bool isIgnored(WordLangTuple const & wl) const;
74 /// personal word list interface
75 void remove(WordLangTuple const & wl);
76 void insert(WordLangTuple const & wl);
77 bool learned(WordLangTuple const & wl);
83 LangPersonalWordList personal_;
85 std::string user_path_;
87 /// the location below system/user directory
88 /// there the aff+dic files lookup will happen
89 const string dictDirectory(void) const { return "dicts"; }
90 int maxLookupSelector(void) const { return 5; }
91 const string HunspellDictionaryName(Language const * lang) {
92 return lang->variety().empty()
94 : lang->code() + "-" + lang->variety();
96 const string myspellPackageDictDirectory(void) {
97 return "/usr/share/myspell";
99 const string hunspellPackageDictDirectory(void) {
100 return "/usr/share/hunspell";
105 HunspellChecker::Private::Private()
107 setUserPath(lyxrc.hunspelldir_path);
111 HunspellChecker::Private::~Private()
117 void HunspellChecker::Private::setUserPath(std::string const & path)
119 if (user_path_ != lyxrc.hunspelldir_path) {
126 void HunspellChecker::Private::cleanCache()
128 Spellers::iterator it = spellers_.begin();
129 Spellers::iterator end = spellers_.end();
131 for (; it != end; ++it) {
133 it->second = nullptr;
136 LangPersonalWordList::const_iterator pdit = personal_.begin();
137 LangPersonalWordList::const_iterator pdet = personal_.end();
139 for (; pdit != pdet; ++pdit) {
140 if (pdit->second == nullptr)
142 PersonalWordList * pd = pdit->second;
149 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
151 FileName const affix(hpath + ".aff");
152 FileName const dict(hpath + ".dic");
153 return affix.isReadableFile() && dict.isReadableFile();
157 const string HunspellChecker::Private::dictPath(int selector)
161 return hunspellPackageDictDirectory();
163 return myspellPackageDictDirectory();
165 return addName(package().system_support().absFileName(),dictDirectory());
167 return addName(package().user_support().absFileName(),dictDirectory());
174 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
176 if (hpath.empty() || !lang)
179 if (lookup(lang)) return true;
181 string d_name = HunspellDictionaryName(lang);
183 LYXERR(Debug::FILES, "check hunspell path: " << hpath
184 << " for language " << lang->lang() << " with name " << d_name);
186 string h_path = addName(hpath, d_name);
187 // first we try lang code+variety
188 if (haveLanguageFiles(h_path)) {
189 LYXERR(Debug::FILES, " found " << h_path);
193 // another try with code, '_' replaced by '-'
194 h_path = addName(hpath, subst(lang->code(), '_', '-'));
195 if (!haveLanguageFiles(h_path))
197 LYXERR(Debug::FILES, " found " << h_path);
203 bool HunspellChecker::Private::haveDictionary(Language const * lang)
207 setUserPath(lyxrc.hunspelldir_path);
208 for (int p = 0; !result && p < maxLookupSelector(); ++p) {
209 string lpath = dictPath(p);
210 result = haveDictionary(lang, lpath);
216 Hunspell * HunspellChecker::Private::speller(Language const * lang)
218 Hunspell * h = lookup(lang);
221 setUserPath(lyxrc.hunspelldir_path);
222 return addSpeller(lang);
226 Hunspell * HunspellChecker::Private::lookup(Language const * lang)
228 Spellers::iterator it = spellers_.find(lang->lang());
229 return it != spellers_.end() ? it->second : nullptr;
233 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang, string & path)
235 if (!haveDictionary(lang, path)) {
236 spellers_[lang->lang()] = nullptr;
240 FileName const affix(path + ".aff");
241 FileName const dict(path + ".dic");
242 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
243 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " added.");
244 spellers_[lang->lang()] = h;
249 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
251 Hunspell * h = nullptr;
252 for (int p = 0; p < maxLookupSelector() && nullptr == h; ++p) {
253 string lpath = dictPath(p);
254 h = addSpeller(lang, lpath);
257 string const encoding = h->get_dic_encoding();
258 PersonalWordList * pd = new PersonalWordList(lang->lang());
260 personal_[lang->lang()] = pd;
261 docstring_list::const_iterator it = pd->begin();
262 docstring_list::const_iterator et = pd->end();
263 for (; it != et; ++it) {
264 string const word_to_add = to_iconv_encoding(*it, encoding);
265 h->add(word_to_add.c_str());
272 int HunspellChecker::Private::numDictionaries() const
275 Spellers::const_iterator it = spellers_.begin();
276 Spellers::const_iterator et = spellers_.end();
278 for (; it != et; ++it)
279 result += it->second != nullptr;
284 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
286 IgnoreList::const_iterator it = ignored_.begin();
287 for (; it != ignored_.end(); ++it) {
288 if (it->lang()->code() != wl.lang()->code())
290 if (it->word() == wl.word())
296 /// personal word list interface
297 void HunspellChecker::Private::remove(WordLangTuple const & wl)
299 Hunspell * h = speller(wl.lang());
302 string const encoding = h->get_dic_encoding();
303 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
304 h->remove(word_to_check.c_str());
305 PersonalWordList * pd = personal_[wl.lang()->lang()];
308 pd->remove(wl.word());
312 void HunspellChecker::Private::insert(WordLangTuple const & wl)
314 Hunspell * h = speller(wl.lang());
317 string const encoding = h->get_dic_encoding();
318 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
319 h->add(word_to_check.c_str());
320 PersonalWordList * pd = personal_[wl.lang()->lang()];
323 pd->insert(wl.word());
327 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
329 PersonalWordList * pd = personal_[wl.lang()->lang()];
332 return pd->exists(wl.word());
336 HunspellChecker::HunspellChecker()
341 HunspellChecker::~HunspellChecker()
347 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
349 if (d->isIgnored(wl))
352 Hunspell * h = d->speller(wl.lang());
354 return NO_DICTIONARY;
357 string const encoding = h->get_dic_encoding();
358 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
360 LYXERR(Debug::GUI, "spellCheck: \"" <<
361 wl.word() << "\", lang = " << wl.lang()->lang()) ;
362 #ifdef HAVE_HUNSPELL_CXXABI
363 if (h->spell(word_to_check, &info))
365 if (h->spell(word_to_check.c_str(), &info))
367 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
369 if (info & SPELL_COMPOUND) {
370 // FIXME: What to do with that?
371 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
373 if (info & SPELL_FORBIDDEN) {
374 // This was removed from personal dictionary
375 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
382 void HunspellChecker::advanceChangeNumber()
388 void HunspellChecker::insert(WordLangTuple const & wl)
391 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
392 advanceChangeNumber();
396 void HunspellChecker::remove(WordLangTuple const & wl)
399 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
400 advanceChangeNumber();
404 void HunspellChecker::accept(WordLangTuple const & wl)
406 d->ignored_.push_back(wl);
407 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
408 advanceChangeNumber();
412 void HunspellChecker::suggest(WordLangTuple const & wl,
413 docstring_list & suggestions)
416 Hunspell * h = d->speller(wl.lang());
419 string const encoding = h->get_dic_encoding();
420 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
421 #ifdef HAVE_HUNSPELL_CXXABI
422 vector<string> wlst = h->suggest(word_to_check);
423 for (auto const & s : wlst)
424 suggestions.push_back(remap_result(from_iconv_encoding(s, encoding)));
426 char ** suggestion_list;
427 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
428 if (suggestion_number <= 0)
430 for (int i = 0; i != suggestion_number; ++i)
431 suggestions.push_back(remap_result(from_iconv_encoding(suggestion_list[i], encoding)));
432 h->free_list(&suggestion_list, suggestion_number);
437 void HunspellChecker::stem(WordLangTuple const & wl,
438 docstring_list & suggestions)
441 Hunspell * h = d->speller(wl.lang());
444 string const encoding = h->get_dic_encoding();
445 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
446 #ifdef HAVE_HUNSPELL_CXXABI
447 vector<string> wlst = h->stem(word_to_check);
448 for (auto const & s : wlst)
449 suggestions.push_back(from_iconv_encoding(s, encoding));
451 char ** suggestion_list;
452 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
453 if (suggestion_number <= 0)
455 for (int i = 0; i != suggestion_number; ++i)
456 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
457 h->free_list(&suggestion_list, suggestion_number);
462 bool HunspellChecker::hasDictionary(Language const * lang) const
466 return d->haveDictionary(lang);
470 int HunspellChecker::numDictionaries() const
472 return d->numDictionaries();
476 docstring const HunspellChecker::error()