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
58 const string dictPath(int selector);
59 bool haveLanguageFiles(string const & hpath);
60 bool haveDictionary(Language const * lang, string & hpath);
61 bool haveDictionary(Language const * lang);
62 Hunspell * addSpeller(Language const * lang, string & hpath);
63 Hunspell * addSpeller(Language const * lang);
64 Hunspell * speller(Language const * lang);
66 bool isIgnored(WordLangTuple const & wl) const;
67 /// personal word list interface
68 void remove(WordLangTuple const & wl);
69 void insert(WordLangTuple const & wl);
70 bool learned(WordLangTuple const & wl);
76 LangPersonalWordList personal_;
78 /// the location below system/user directory
79 /// there the aff+dic files lookup will happen
80 const string dictDirectory(void) const { return "dicts"; }
81 int maxLookupSelector(void) const { return 3; }
85 HunspellChecker::Private::Private()
90 HunspellChecker::Private::~Private()
92 Spellers::iterator it = spellers_.begin();
93 Spellers::iterator end = spellers_.end();
95 for (; it != end; ++it) {
96 if ( 0 != it->second) delete it->second;
99 LangPersonalWordList::const_iterator pdit = personal_.begin();
100 LangPersonalWordList::const_iterator pdet = personal_.end();
102 for (; pdit != pdet; ++pdit) {
103 if ( 0 == pdit->second)
105 PersonalWordList * pd = pdit->second;
112 bool HunspellChecker::Private::haveLanguageFiles(string const & hpath)
114 FileName const affix(hpath + ".aff");
115 FileName const dict(hpath + ".dic");
116 return affix.isReadableFile() && dict.isReadableFile();
120 const string HunspellChecker::Private::dictPath(int selector)
124 return addName(package().system_support().absFileName(),dictDirectory());
127 return addName(package().user_support().absFileName(),dictDirectory());
130 return lyxrc.hunspelldir_path;
135 bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
141 LYXERR(Debug::FILES, "check hunspell path: " << hpath << " for language " << lang);
142 string h_path = addName(hpath, lang->code() + "-" + lang->variety());
143 // first we try lang code+variety
144 if (!lang->variety().empty() && haveLanguageFiles(h_path)) {
148 // next we try lang code only
149 h_path = addName(hpath, lang->code());
150 if (haveLanguageFiles(h_path)) {
154 // last try with '_' replaced by '-'
155 h_path = addName(hpath, subst(lang->code(), '_', '-'));
156 if (!haveLanguageFiles(h_path)) {
164 bool HunspellChecker::Private::haveDictionary(Language const * lang)
167 for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
168 string lpath = dictPath(p);
169 result = haveDictionary(lang, lpath);
171 // FIXME: if result is false...
172 // we should indicate somehow that this language is not
173 // supported, probably by popping a warning. But we'll need to
174 // remember which warnings we've issued.
179 Hunspell * HunspellChecker::Private::speller(Language const * lang)
181 Spellers::iterator it = spellers_.find(lang->id());
182 if (it != spellers_.end())
185 return addSpeller(lang);
189 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
191 if (!haveDictionary(lang, path)) {
192 spellers_[lang->id()] = 0;
196 FileName const affix(path + ".aff");
197 FileName const dict(path + ".dic");
198 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
199 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
200 spellers_[lang->id()] = h;
205 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
208 for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
209 string lpath = dictPath(p);
210 h = addSpeller(lang, lpath);
213 string const encoding = h->get_dic_encoding();
214 PersonalWordList * pd = new PersonalWordList(lang->lang());
216 personal_[lang->lang()] = pd;
217 docstring_list::const_iterator it = pd->begin();
218 docstring_list::const_iterator et = pd->end();
219 for (; it != et; ++it) {
220 string const word_to_add = to_iconv_encoding(*it, encoding);
221 h->add(word_to_add.c_str());
228 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
230 IgnoreList::const_iterator it = ignored_.begin();
231 for (; it != ignored_.end(); ++it) {
232 if ((*it).lang()->code() != wl.lang()->code())
234 if ((*it).word() == wl.word())
240 /// personal word list interface
241 void HunspellChecker::Private::remove(WordLangTuple const & wl)
243 Hunspell * h = speller(wl.lang());
246 string const encoding = h->get_dic_encoding();
247 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
248 h->remove(word_to_check.c_str());
249 PersonalWordList * pd = personal_[wl.lang()->lang()];
252 pd->remove(wl.word());
256 void HunspellChecker::Private::insert(WordLangTuple const & wl)
258 Hunspell * h = speller(wl.lang());
261 string const encoding = h->get_dic_encoding();
262 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
263 h->add(word_to_check.c_str());
264 PersonalWordList * pd = personal_[wl.lang()->lang()];
267 pd->insert(wl.word());
271 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
273 PersonalWordList * pd = personal_[wl.lang()->lang()];
276 return pd->exists(wl.word());
280 HunspellChecker::HunspellChecker(): d(new Private)
285 HunspellChecker::~HunspellChecker()
291 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
293 if (d->isIgnored(wl))
296 Hunspell * h = d->speller(wl.lang());
301 string const encoding = h->get_dic_encoding();
302 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
304 if (h->spell(word_to_check.c_str(), &info))
305 return d->learned(wl) ? LEARNED_WORD : WORD_OK;
307 if (info & SPELL_COMPOUND) {
308 // FIXME: What to do with that?
309 LYXERR(Debug::FILES, "Hunspell compound word found " << word_to_check);
311 if (info & SPELL_FORBIDDEN) {
312 // This was removed from personal dictionary
313 LYXERR(Debug::FILES, "Hunspell explicit forbidden word found " << word_to_check);
320 void HunspellChecker::advanceChangeNumber()
326 void HunspellChecker::insert(WordLangTuple const & wl)
329 LYXERR(Debug::GUI, "learn word: \"" << wl.word() << "\"") ;
330 advanceChangeNumber();
334 void HunspellChecker::remove(WordLangTuple const & wl)
337 LYXERR(Debug::GUI, "unlearn word: \"" << wl.word() << "\"") ;
338 advanceChangeNumber();
342 void HunspellChecker::accept(WordLangTuple const & wl)
344 d->ignored_.push_back(wl);
345 LYXERR(Debug::GUI, "ignore word: \"" << wl.word() << "\"") ;
346 advanceChangeNumber();
350 void HunspellChecker::suggest(WordLangTuple const & wl,
351 docstring_list & suggestions)
354 Hunspell * h = d->speller(wl.lang());
357 string const encoding = h->get_dic_encoding();
358 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
359 char ** suggestion_list;
360 int const suggestion_number = h->suggest(&suggestion_list, word_to_check.c_str());
361 if (suggestion_number <= 0)
363 for (int i = 0; i != suggestion_number; ++i)
364 suggestions.push_back(from_iconv_encoding(suggestion_list[i], encoding));
365 h->free_list(&suggestion_list, suggestion_number);
369 bool HunspellChecker::hasDictionary(Language const * lang) const
373 return (d->haveDictionary(lang));
377 docstring const HunspellChecker::error()