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 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 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)
174 LYXERR(Debug::FILES, "check hunspell path: " << hpath
175 << " for language " << (lang ? lang->lang() : "NULL" ));
177 string h_path = addName(hpath, HunspellDictionaryName(lang));
178 // first we try lang code+variety
179 if (haveLanguageFiles(h_path)) {
180 LYXERR(Debug::FILES, " found " << h_path);
184 // another try with code, '_' replaced by '-'
185 h_path = addName(hpath, subst(lang->code(), '_', '-'));
186 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()) {
219 return addSpeller(lang);
223 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
225 if (!haveDictionary(lang, path)) {
226 spellers_[lang->lang()] = 0;
230 FileName const affix(path + ".aff");
231 FileName const dict(path + ".dic");
232 Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
233 LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
234 spellers_[lang->lang()] = h;
239 Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
242 for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
243 string lpath = dictPath(p);
244 h = addSpeller(lang, lpath);
247 string const encoding = h->get_dic_encoding();
248 PersonalWordList * pd = new PersonalWordList(lang->lang());
250 personal_[lang->lang()] = pd;
251 docstring_list::const_iterator it = pd->begin();
252 docstring_list::const_iterator et = pd->end();
253 for (; it != et; ++it) {
254 string const word_to_add = to_iconv_encoding(*it, encoding);
255 h->add(word_to_add.c_str());
262 int HunspellChecker::Private::numDictionaries() const
265 Spellers::const_iterator it = spellers_.begin();
266 Spellers::const_iterator et = spellers_.end();
268 for (; it != et; ++it) {
269 result += it->second != 0;
275 bool HunspellChecker::Private::isIgnored(WordLangTuple const & wl) const
277 IgnoreList::const_iterator it = ignored_.begin();
278 for (; it != ignored_.end(); ++it) {
279 if ((*it).lang()->code() != wl.lang()->code())
281 if ((*it).word() == wl.word())
287 /// personal word list interface
288 void HunspellChecker::Private::remove(WordLangTuple const & wl)
290 Hunspell * h = speller(wl.lang());
293 string const encoding = h->get_dic_encoding();
294 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
295 h->remove(word_to_check.c_str());
296 PersonalWordList * pd = personal_[wl.lang()->lang()];
299 pd->remove(wl.word());
303 void HunspellChecker::Private::insert(WordLangTuple const & wl)
305 Hunspell * h = speller(wl.lang());
308 string const encoding = h->get_dic_encoding();
309 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
310 h->add(word_to_check.c_str());
311 PersonalWordList * pd = personal_[wl.lang()->lang()];
314 pd->insert(wl.word());
318 bool HunspellChecker::Private::learned(WordLangTuple const & wl)
320 PersonalWordList * pd = personal_[wl.lang()->lang()];
323 return pd->exists(wl.word());
327 HunspellChecker::HunspellChecker(): d(new Private)
332 HunspellChecker::~HunspellChecker()
338 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
340 if (d->isIgnored(wl))
343 Hunspell * h = d->speller(wl.lang());
345 return NO_DICTIONARY;
348 string const encoding = h->get_dic_encoding();
349 string const word_to_check = to_iconv_encoding(wl.word(), encoding);
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::FILES, "Hunspell compound word found " << word_to_check);
358 if (info & SPELL_FORBIDDEN) {
359 // This was removed from personal dictionary
360 LYXERR(Debug::FILES, "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()