scramble email addresses
[lyx.git] / src / Thesaurus.cpp
blob91aa9f649ad1315a5625532c41f757e284824913
1 /**
2 * \file Thesaurus.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author John Levon
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Thesaurus.h"
15 #include "gettext.h"
17 #include "support/lstrings.h"
19 #include "frontends/alert.h"
21 #include <algorithm>
24 namespace lyx {
26 #ifdef HAVE_LIBAIKSAURUS
27 using support::bformat;
29 using std::sort;
30 using std::string;
33 Thesaurus::Thesaurus()
34 : aik_(new Aiksaurus)
38 Thesaurus::~Thesaurus()
40 delete aik_;
44 Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
46 Meanings meanings;
48 // aiksaurus is for english text only, therefore it does not work
49 // with non-ascii strings.
50 // The interface of the Thesaurus class uses docstring because a
51 // non-english thesaurus is possible in theory.
52 if (!support::isAscii(t))
53 // to_ascii() would assert
54 return meanings;
56 string const text = to_ascii(t);
58 docstring error = from_ascii(aik_->error());
59 if (!error.empty()) {
60 static bool sent_error = false;
61 if (!sent_error) {
62 frontend::Alert::error(_("Thesaurus failure"),
63 bformat(_("Aiksaurus returned the following error:\n\n%1$s."),
64 error));
65 sent_error = true;
67 return meanings;
69 if (!aik_->find(text.c_str()))
70 return meanings;
72 // weird api, but ...
74 int prev_meaning = -1;
75 int cur_meaning;
76 docstring meaning;
78 // correct, returns "" at the end
79 string ret = aik_->next(cur_meaning);
81 while (!ret.empty()) {
82 if (cur_meaning != prev_meaning) {
83 meaning = from_ascii(ret);
84 ret = aik_->next(cur_meaning);
85 prev_meaning = cur_meaning;
86 } else {
87 if (ret != text)
88 meanings[meaning].push_back(from_ascii(ret));
91 ret = aik_->next(cur_meaning);
94 for (Meanings::iterator it = meanings.begin();
95 it != meanings.end(); ++it)
96 sort(it->second.begin(), it->second.end());
98 return meanings;
101 #else
103 Thesaurus::Thesaurus()
108 Thesaurus::~Thesaurus()
113 Thesaurus::Meanings Thesaurus::lookup(docstring const &)
115 return Meanings();
118 #endif // HAVE_LIBAIKSAURUS
120 // Global instance
121 Thesaurus thesaurus;
124 } // namespace lyx