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;
45 #ifdef HAVE_LIBAIKSAURUS
47 struct Thesaurus::Private
49 Private(): thes_(new Aiksaurus) {}
53 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const &)
57 // aiksaurus is for english text only, therefore it does not work
58 // with non-ascii strings.
59 // The interface of the Thesaurus class uses docstring because a
60 // non-english thesaurus is possible in theory.
61 if (!support::isAscii(t))
62 // to_ascii() would assert
65 string const text = to_ascii(t);
67 docstring error = from_ascii(d->thes_->error());
69 static bool sent_error = false;
71 frontend::Alert::error(_("Thesaurus failure"),
72 bformat(_("Aiksaurus returned the following error:\n\n%1$s."),
78 if (!d->thes_->find(text.c_str()))
83 int prev_meaning = -1;
87 // correct, returns "" at the end
88 string ret = d->thes_->next(cur_meaning);
90 while (!ret.empty()) {
91 if (cur_meaning != prev_meaning) {
92 meaning = from_ascii(ret);
93 ret = d->thes_->next(cur_meaning);
94 prev_meaning = cur_meaning;
97 meanings[meaning].push_back(from_ascii(ret));
100 ret = d->thes_->next(cur_meaning);
103 for (Meanings::iterator it = meanings.begin();
104 it != meanings.end(); ++it)
105 sort(it->second.begin(), it->second.end());
111 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
113 // we support English only
114 return prefixIs(lang, from_ascii("en_"));
117 #else // HAVE_LIBAIKSAURUS
118 #ifdef HAVE_LIBMYTHES
122 string const to_iconv_encoding(docstring const & s, string const & encoding)
124 std::vector<char> const encoded =
125 ucs4_to_eightbit(s.data(), s.length(), encoding);
126 return string(encoded.begin(), encoded.end());
130 docstring const from_iconv_encoding(string const & s, string const & encoding)
132 std::vector<char_type> const ucs4 =
133 eightbit_to_ucs4(s.data(), s.length(), encoding);
134 return docstring(ucs4.begin(), ucs4.end());
137 typedef std::map<docstring, MyThes *> Thesauri;
142 struct Thesaurus::Private
144 ~Private(Thesaurus & parent): parent_(parent)
146 for (Thesauri::iterator it = thes_.begin();
147 it != thes_.end(); ++it) {
155 /// add a thesaurus to the list
156 bool addThesaurus(docstring const & lang);
162 bool Thesaurus::Private::addThesaurus(docstring const & lang)
164 string const thes_path = external_path(lyxrc.thesaurusdir_path);
165 LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
166 if (thes_path.empty())
169 if (parent_.thesaurusAvailable(lang))
172 FileNameList const idx_files = FileName(thes_path).dirList("idx");
173 FileNameList const data_files = FileName(thes_path).dirList("dat");
177 for (FileNameList::const_iterator it = idx_files.begin();
178 it != idx_files.end(); ++it) {
179 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
180 if (contains(it->onlyFileName(), to_ascii(lang))) {
181 idx = it->absFilename();
182 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
187 for (support::FileNameList::const_iterator it = data_files.begin();
188 it != data_files.end(); ++it) {
189 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
190 if (contains(it->onlyFileName(), to_ascii(lang))) {
191 data = it->absFilename();
192 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
197 if (idx.empty() || data.empty())
200 char const * af = idx.c_str();
201 char const * df = data.c_str();
202 thes_[lang] = new MyThes(af, df);
207 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
209 for (Thesauri::const_iterator it = d->thes_.begin();
210 it != d->thes_.end(); ++it) {
211 if (it->first == lang)
220 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
225 if (!d->addThesaurus(lang))
228 for (Thesauri::const_iterator it = d->thes_.begin();
229 it != d->thes_.end(); ++it) {
230 if (it->first == lang) {
239 string const encoding = mythes->get_th_encoding();
242 string const text = to_iconv_encoding(support::lowercase(t), encoding);
243 int len = strlen(text.c_str());
244 int count = mythes->Lookup(text.c_str(), len, &pmean);
248 // don't change value of pmean or count
249 // they are needed for the CleanUpAfterLookup routine
253 for (int i = 0; i < count; i++) {
254 meaning = from_iconv_encoding(string(pm->defn), encoding);
256 if (support::prefixIs(meaning, '-'))
257 meaning = support::ltrim(meaning, "- ");
258 for (int j = 0; j < pm->count; j++) {
259 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
261 meanings[meaning].push_back(ret);
264 // now clean up all allocated memory
265 mythes->CleanUpAfterLookup(&pmean, count);
267 for (Meanings::iterator it = meanings.begin();
268 it != meanings.end(); ++it)
269 sort(it->second.begin(), it->second.end());
276 struct Thesaurus::Private
281 Thesaurus::Meanings Thesaurus::lookup(docstring const &, docstring const &)
287 bool Thesaurus::thesaurusAvailable(docstring const &) const
292 #endif // HAVE_LIBMYTHES
293 #endif // HAVE_LIBAIKSAURUS
295 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
300 Thesaurus::~Thesaurus()