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 5; }
87 const string HunspellDictionaryName(Language const * lang) {
88 return lang->variety().empty()
90 : lang->code() + "-" + lang->variety();
92 const string myspellPackageDictDirectory(void) {
93 return "/usr/share/myspell";
95 const string hunspellPackageDictDirectory(void) {
96 return "/usr/share/hunspell";
101 HunspellChecker::Private::Private()
103 setUserPath(lyxrc.hunspelldir_path);
107 HunspellChecker::Private::~Private()
113 void HunspellChecker::Private::setUserPath(std::string const & path)
115 if (user_path_ != lyxrc.hunspelldir_path) {
122 void HunspellChecker::Private::cleanCache()
124 Spellers::iterator it = spellers_.begin();
125 Spellers::iterator end = spellers_.end();
127 for (; it != end; ++it) {
132 LangPersonalWordList::const_iterator pdit = personal_.begin();
133 LangPersonalWordList::const_iterator pdet = personal_.end();
135 for (; pdit != pdet; ++pdit) {
136 if ( 0 == pdit->second)
138 PersonalWordList * pd = pdit->second;
145 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
147 FileName const affix(hpath + ".aff");
148 FileName const dict(hpath + ".dic");
149 return affix.isReadableFile() && dict.isReadableFile();
153 const string HunspellChecker::Private::dictPath(int selector)
157 return hunspellPackageDictDirectory();
159 return myspellPackageDictDirectory();
161 return addName(package().system_support().absFileName(),dictDirectory());
163 return addName(package().user_support().absFileName(),dictDirectory());
170 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
175 LYXERR(Debug::FILES, "check hunspell path: " << hpath
176 << " for language " << (lang ? lang->lang() : "NULL" ));
178 string h_path = addName(hpath, HunspellDictionaryName(lang));
179 // first we try lang code+variety
180 if (haveLanguageFiles(h_path)) {
181 LYXERR(Debug::FILES, " found " << h_path);
185 // another try with code, '_' replaced by '-'
186 h_path = addName(hpath, subst(lang->code(), '_', '-'));
187 if (!haveLanguageFiles(h_path))
189 LYXERR(Debug::FILES, " found " << h_path);
195 bool HunspellChecker::Private::haveDictionary(Language const * lang)
199 setUserPath(lyxrc.hunspelldir_path);
200 for (int p = 0; !result && p < maxLookupSelector(); ++p) {
201 string lpath = dictPath(p);
202 result = haveDictionary(lang, lpath);
204 // FIXME: if result is false...
205 // we should indicate somehow that this language is not
206 // supported, probably by popping a warning. But we'll need to
207 // remember which warnings we've issued.
212 Hunspell * HunspellChecker::Private::speller(Language const * lang)
214 setUserPath(lyxrc.hunspelldir_path);
215 Spellers::iterator it = spellers_.find(lang->lang());
216 if (it != spellers_.end())
218 return addSpeller(lang);
222 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
224 if (!haveDictionary(lang, path)) {
225 spellers_[lang->lang()] = 0;
229 FileName const affix(path + ".aff");
230 FileName const dict(path + ".dic");
231 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
232 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
233 spellers_[lang->lang()] = h;
238 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
241 for (int p = 0; p < maxLookupSelector() && 0 == h; ++p) {
242 string lpath = dictPath(p);
243 h = addSpeller(lang, lpath);
246 string const encoding = h->get_dic_encoding();
247 PersonalWordList * pd = new PersonalWordList(lang->lang());
249 personal_[lang->lang()] = pd;
250 docstring_list::const_iterator it = pd->begin();
251 docstring_list::const_iterator et = pd->end();
252 for (; it != et; ++it) {
253 string const word_to_add = to_iconv_encoding(*it, encoding);
254 h->add(word_to_add.c_str());
261 int HunspellChecker::Private::numDictionaries() const
264 Spellers::const_iterator it = spellers_.begin();
265 Spellers::const_iterator et = spellers_.end();
267 for (; it != et; ++it)
268 result += it->second != 0;
273 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
275 IgnoreList::const_iterator it = ignored_.begin();
276 for (; it != ignored_.end(); ++it) {
277 if (it->lang()->code() != wl.lang()->code())
279 if (it->word() == wl.word())
285 /// personal word list interface
286 void HunspellChecker::Private::remove(WordLangTuple const & wl)
288 Hunspell * h = speller(wl.lang());
291 string const encoding = h->get_dic_encoding();
292 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
293 h->remove(word_to_check.c_str());
294 PersonalWordList * pd = personal_[wl.lang()->lang()];
297 pd->remove(wl.word());
301 void HunspellChecker::Private::insert(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->add(word_to_check.c_str());
309 PersonalWordList * pd = personal_[wl.lang()->lang()];
312 pd->insert(wl.word());
316 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
318 PersonalWordList * pd = personal_[wl.lang()->lang()];
321 return pd->exists(wl.word());
325 HunspellChecker::HunspellChecker()
330 HunspellChecker::~HunspellChecker()
336 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
338 if (d->isIgnored(wl))
341 Hunspell * h = d->speller(wl.lang());
343 return NO_DICTIONARY;
346 string const encoding = h->get_dic_encoding();
347 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
349 LYXERR(Debug::GUI, "spellCheck: \"" <<
350 wl.word() << "\", lang = " << wl.lang()->lang()) ;
351 if (h->spell(word_to_check.c_str(), &info))
352 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
354 if (info & SPELL_COMPOUND) {
355 // FIXME: What to do with that?
356 LYXERR(Debug::GUI, "Hunspell compound word found " << word_to_check);
358 if (info & SPELL_FORBIDDEN) {
359 // This was removed from personal dictionary
360 LYXERR(Debug::GUI, "Hunspell explicit forbidden word found " << word_to_check);
367 void HunspellChecker::advanceChangeNumber()
373 void HunspellChecker::insert(WordLangTuple const & wl)
376 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
377 advanceChangeNumber();
381 void HunspellChecker::remove(WordLangTuple const & wl)
384 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
385 advanceChangeNumber();
389 void HunspellChecker::accept(WordLangTuple const & wl)
391 d->ignored_.push_back(wl);
392 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
393 advanceChangeNumber();
397 void HunspellChecker::suggest(WordLangTuple const & wl,
398 docstring_list & suggestions)
401 Hunspell * h = d->speller(wl.lang());
404 string const encoding = h->get_dic_encoding();
405 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
406 char ** suggestion_list;
407 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
408 if (suggestion_number <= 0)
410 for (int i = 0; i != suggestion_number; ++i)
411 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
412 h->free_list(&suggestion_list, suggestion_number);
416 void HunspellChecker::stem(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 char ** suggestion_list;
426 int const suggestion_number = h->stem(&suggestion_list, word_to_check.c_str());
427 if (suggestion_number <= 0)
429 for (int i = 0; i != suggestion_number; ++i)
430 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
431 h->free_list(&suggestion_list, suggestion_number);
435 bool HunspellChecker::hasDictionary(Language const * lang) const
439 return d->haveDictionary(lang);
443 int HunspellChecker::numDictionaries() const
445 return d->numDictionaries();
449 docstring const HunspellChecker::error()