3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "Thesaurus.h"
16 #include "support/debug.h"
17 #include "support/gettext.h"
20 #include "support/FileNameList.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
24 #include "support/unicode.h"
26 #include "frontends/alert.h"
29 #include MYTHES_H_LOCATION
31 #ifdef HAVE_LIBAIKSAURUS
32 #include AIKSAURUS_H_LOCATION
33 #endif // HAVE_LIBAIKSAURUS
34 #endif // !HAVE_LIBMYTHES
40 using namespace lyx::support;
41 using namespace lyx::support::os;
49 string const to_iconv_encoding(docstring const & s, string const & encoding)
51 std::vector<char> const encoded =
52 ucs4_to_eightbit(s.data(), s.length(), encoding);
53 return string(encoded.begin(), encoded.end());
57 docstring const from_iconv_encoding(string const & s, string const & encoding)
59 std::vector<char_type> const ucs4 =
60 eightbit_to_ucs4(s.data(), s.length(), encoding);
61 return docstring(ucs4.begin(), ucs4.end());
64 typedef std::map<docstring, MyThes *> Thesauri;
69 struct Thesaurus::Private
73 for (Thesauri::iterator it = thes_.begin();
74 it != thes_.end(); ++it) {
79 bool thesaurusAvailable(docstring const & lang) const
81 for (Thesauri::const_iterator it = thes_.begin();
82 it != thes_.end(); ++it) {
83 if (it->first == lang)
90 /// add a thesaurus to the list
91 bool addThesaurus(docstring const & lang);
97 bool Thesaurus::Private::addThesaurus(docstring const & lang)
99 string const thes_path = external_path(lyxrc.thesaurusdir_path);
100 LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
101 if (thes_path.empty())
104 if (thesaurusAvailable(lang))
107 FileNameList const idx_files = FileName(thes_path).dirList("idx");
108 FileNameList const data_files = FileName(thes_path).dirList("dat");
112 for (FileNameList::const_iterator it = idx_files.begin();
113 it != idx_files.end(); ++it) {
114 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
115 if (contains(it->onlyFileName(), to_ascii(lang))) {
116 idx = it->absFilename();
117 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
122 for (support::FileNameList::const_iterator it = data_files.begin();
123 it != data_files.end(); ++it) {
124 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
125 if (contains(it->onlyFileName(), to_ascii(lang))) {
126 data = it->absFilename();
127 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
132 if (idx.empty() || data.empty())
135 char const * af = idx.c_str();
136 char const * df = data.c_str();
137 thes_[lang] = new MyThes(af, df);
142 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
144 return d->thesaurusAvailable(lang);
148 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
153 if (!d->addThesaurus(lang))
156 for (Thesauri::const_iterator it = d->thes_.begin();
157 it != d->thes_.end(); ++it) {
158 if (it->first == lang) {
167 string const encoding = mythes->get_th_encoding();
170 string const text = to_iconv_encoding(support::lowercase(t), encoding);
171 int len = strlen(text.c_str());
172 int count = mythes->Lookup(text.c_str(), len, &pmean);
176 // don't change value of pmean or count
177 // they are needed for the CleanUpAfterLookup routine
181 for (int i = 0; i < count; i++) {
182 meaning = from_iconv_encoding(string(pm->defn), encoding);
184 if (support::prefixIs(meaning, '-'))
185 meaning = support::ltrim(meaning, "- ");
186 for (int j = 0; j < pm->count; j++) {
187 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
189 meanings[meaning].push_back(ret);
192 // now clean up all allocated memory
193 mythes->CleanUpAfterLookup(&pmean, count);
195 for (Meanings::iterator it = meanings.begin();
196 it != meanings.end(); ++it)
197 sort(it->second.begin(), it->second.end());
202 #else // HAVE_LIBMYTHES
203 #ifdef HAVE_LIBAIKSAURUS
205 struct Thesaurus::Private
207 Private(): thes_(new Aiksaurus) {}
211 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const &)
215 // aiksaurus is for english text only, therefore it does not work
216 // with non-ascii strings.
217 // The interface of the Thesaurus class uses docstring because a
218 // non-english thesaurus is possible in theory.
219 if (!support::isAscii(t))
220 // to_ascii() would assert
223 string const text = to_ascii(t);
225 docstring error = from_ascii(d->thes_->error());
226 if (!error.empty()) {
227 static bool sent_error = false;
229 frontend::Alert::error(_("Thesaurus failure"),
230 bformat(_("Aiksaurus returned the following error:\n\n%1$s."),
236 if (!d->thes_->find(text.c_str()))
239 // weird api, but ...
241 int prev_meaning = -1;
245 // correct, returns "" at the end
246 string ret = d->thes_->next(cur_meaning);
248 while (!ret.empty()) {
249 if (cur_meaning != prev_meaning) {
250 meaning = from_ascii(ret);
251 ret = d->thes_->next(cur_meaning);
252 prev_meaning = cur_meaning;
255 meanings[meaning].push_back(from_ascii(ret));
258 ret = d->thes_->next(cur_meaning);
261 for (Meanings::iterator it = meanings.begin();
262 it != meanings.end(); ++it)
263 sort(it->second.begin(), it->second.end());
269 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
271 // we support English only
272 return prefixIs(lang, from_ascii("en_"));
275 #else // HAVE_LIBAIKSAURUS
277 struct Thesaurus::Private
282 Thesaurus::Meanings Thesaurus::lookup(docstring const &, docstring const &)
288 bool Thesaurus::thesaurusAvailable(docstring const &) const
293 #endif // HAVE_LIBAIKSAURUS
294 #endif // HAVE_LIBMYTHES
296 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
301 Thesaurus::~Thesaurus()