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);
69 bool isIgnored(WordLangTuple const & wl) const;
70 /// personal word list interface
71 void remove(WordLangTuple const & wl);
72 void insert(WordLangTuple const & wl);
73 bool learned(WordLangTuple const & wl);
79 LangPersonalWordList personal_;
81 std::string user_path_;
83 /// the location below system/user directory
84 /// there the aff+dic files lookup will happen
85 const string dictDirectory(void) const { return "dicts"; }
86 int maxLookupSelector(void) const { return 4; }
87 const string HunspellDictionaryName(Language const * lang) {
88 return lang->variety().empty()
90 : lang->code() + "-" + lang->variety();
92 const string osPackageDictDirectory(void) {
93 return "/usr/share/myspell";
98 HunspellChecker::Private::Private()
100 setUserPath(lyxrc.hunspelldir_path);
104 HunspellChecker::Private::~Private()
110 void HunspellChecker::Private::setUserPath(std::string const & path)
112 if (user_path_ != lyxrc.hunspelldir_path) {
119 void HunspellChecker::Private::cleanCache()
121 Spellers::iterator it = spellers_.begin();
122 Spellers::iterator end = spellers_.end();
124 for (; it != end; ++it) {
129 LangPersonalWordList::const_iterator pdit = personal_.begin();
130 LangPersonalWordList::const_iterator pdet = personal_.end();
132 for (; pdit != pdet; ++pdit) {
133 if ( 0 == pdit->second)
135 PersonalWordList * pd = pdit->second;
142 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
144 FileName const affix(hpath + ".aff");
145 FileName const dict(hpath + ".dic");
146 return affix.isReadableFile() && dict.isReadableFile();
150 const string HunspellChecker::Private::dictPath(int selector)
154 return addName(osPackageDictDirectory(),dictDirectory());
157 return addName(package().system_support().absFileName(),dictDirectory());
160 return addName(package().user_support().absFileName(),dictDirectory());
168 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
173 LYXERR(Debug::FILES, "check hunspell path: " << hpath
174 << " for language " << (lang ? lang->lang() : "NULL" ));
176 string h_path = addName(hpath, HunspellDictionaryName(lang));
177 // first we try lang code+variety
178 if (haveLanguageFiles(h_path)) {
179 LYXERR(Debug::FILES, " found " << h_path);
183 // another try with code, '_' replaced by '-'
184 h_path = addName(hpath, subst(lang->code(), '_', '-'));
185 if (!haveLanguageFiles(h_path))
187 LYXERR(Debug::FILES, " found " << h_path);
193 bool HunspellChecker::Private::haveDictionary(Language const * lang)
197 setUserPath(lyxrc.hunspelldir_path);
198 for (int p = 0; !result && p < maxLookupSelector(); ++p) {
199 string lpath = dictPath(p);
200 result = haveDictionary(lang, lpath);
202 // FIXME: if result is false...
203 // we should indicate somehow that this language is not
204 // supported, probably by popping a warning. But we'll need to
205 // remember which warnings we've issued.
210 Hunspell * HunspellChecker::Private::speller(Language const * lang)
212 setUserPath(lyxrc.hunspelldir_path);
213 Spellers::iterator it = spellers_.find(lang->lang());
214 if (it != spellers_.end())
216 return addSpeller(lang);
220 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
222 if (!haveDictionary(lang, path)) {
223 spellers_[lang->lang()] = 0;
227 FileName const affix(path + ".aff");
228 FileName const dict(path + ".dic");
229 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
230 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
231 spellers_[lang->lang()] = h;
236 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
239 for (int p = 0; p < maxLookupSelector() && 0 == h; ++p) {
240 string lpath = dictPath(p);
241 h = addSpeller(lang, lpath);
244 string const encoding = h->get_dic_encoding();
245 PersonalWordList * pd = new PersonalWordList(lang->lang());
247 personal_[lang->lang()] = pd;
248 docstring_list::const_iterator it = pd->begin();
249 docstring_list::const_iterator et = pd->end();
250 for (; it != et; ++it) {
251 string const word_to_add = to_iconv_encoding(*it, encoding);
252 h->add(word_to_add.c_str());
259 int HunspellChecker::Private::numDictionaries() const
262 Spellers::const_iterator it = spellers_.begin();
263 Spellers::const_iterator et = spellers_.end();
265 for (; it != et; ++it)
266 result += it->second != 0;
271 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
273 IgnoreList::const_iterator it = ignored_.begin();
274 for (; it != ignored_.end(); ++it) {
275 if (it->lang()->code() != wl.lang()->code())
277 if (it->word() == wl.word())
283 /// personal word list interface
284 void HunspellChecker::Private::remove(WordLangTuple const & wl)
286 Hunspell * h = speller(wl.lang());
289 string const encoding = h->get_dic_encoding();
290 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
291 h->remove(word_to_check.c_str());
292 PersonalWordList * pd = personal_[wl.lang()->lang()];
295 pd->remove(wl.word());
299 void HunspellChecker::Private::insert(WordLangTuple const & wl)
301 Hunspell * h = speller(wl.lang());
304 string const encoding = h->get_dic_encoding();
305 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
306 h->add(word_to_check.c_str());
307 PersonalWordList * pd = personal_[wl.lang()->lang()];
310 pd->insert(wl.word());
314 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
316 PersonalWordList * pd = personal_[wl.lang()->lang()];
319 return pd->exists(wl.word());
323 HunspellChecker::HunspellChecker()
328 HunspellChecker::~HunspellChecker()
334 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
336 if (d->isIgnored(wl))
339 Hunspell * h = d->speller(wl.lang());
341 return NO_DICTIONARY;
344 string const encoding = h->get_dic_encoding();
345 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
347 LYXERR(Debug::GUI, "spellCheck: \"" <<
348 wl.word() << "\", lang = " << wl.lang()->lang()) ;
349 if (h->spell(word_to_check.c_str(), &info))
350 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
352 if (info & SPELL_COMPOUND) {
353 // FIXME: What to do with that?
354 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
356 if (info & SPELL_FORBIDDEN) {
357 // This was removed from personal dictionary
358 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
365 void HunspellChecker::advanceChangeNumber()
371 void HunspellChecker::insert(WordLangTuple const & wl)
374 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
375 advanceChangeNumber();
379 void HunspellChecker::remove(WordLangTuple const & wl)
382 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
383 advanceChangeNumber();
387 void HunspellChecker::accept(WordLangTuple const & wl)
389 d->ignored_.push_back(wl);
390 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
391 advanceChangeNumber();
395 void HunspellChecker::suggest(WordLangTuple const & wl,
396 docstring_list & suggestions)
399 Hunspell * h = d->speller(wl.lang());
402 string const encoding = h->get_dic_encoding();
403 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
404 char ** suggestion_list;
405 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
406 if (suggestion_number <= 0)
408 for (int i = 0; i != suggestion_number; ++i)
409 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
410 h->free_list(&suggestion_list, suggestion_number);
414 void HunspellChecker::stem(WordLangTuple const & wl,
415 docstring_list & suggestions)
418 Hunspell * h = d->speller(wl.lang());
421 string const encoding = h->get_dic_encoding();
422 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
423 char ** suggestion_list;
424 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
425 if (suggestion_number <= 0)
427 for (int i = 0; i != suggestion_number; ++i)
428 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
429 h->free_list(&suggestion_list, suggestion_number);
433 bool HunspellChecker::hasDictionary(Language const * lang) const
437 return d->haveDictionary(lang);
441 int HunspellChecker::numDictionaries() const
443 return d->numDictionaries();
447 docstring const HunspellChecker::error()