]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
Changed a '1.4.4' to '1.4.5'
[lyx.git] / src / Thesaurus.cpp
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.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Thesaurus.h"
14
15 #include "gettext.h"
16
17 #include "support/lstrings.h"
18
19 #include "frontends/alert.h"
20
21 #include <algorithm>
22
23
24 namespace lyx {
25
26 #ifdef HAVE_LIBAIKSAURUS
27 using support::bformat;
28
29 using std::sort;
30 using std::string;
31
32
33 Thesaurus::Thesaurus()
34         : aik_(new Aiksaurus)
35 {}
36
37
38 Thesaurus::~Thesaurus()
39 {
40         delete aik_;
41 }
42
43
44 Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
45 {
46         Meanings meanings;
47
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;
55
56         string const text = to_ascii(t);
57
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;
66                 }
67                 return meanings;
68         }
69         if (!aik_->find(text.c_str()))
70                 return meanings;
71
72         // weird api, but ...
73
74         int prev_meaning = -1;
75         int cur_meaning;
76         docstring meaning;
77
78         // correct, returns "" at the end
79         string ret = aik_->next(cur_meaning);
80
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));
89                 }
90
91                 ret = aik_->next(cur_meaning);
92         }
93
94         for (Meanings::iterator it = meanings.begin();
95              it != meanings.end(); ++it)
96                 sort(it->second.begin(), it->second.end());
97
98         return meanings;
99 }
100
101 #else
102
103 Thesaurus::Thesaurus()
104 {
105 }
106
107
108 Thesaurus::~Thesaurus()
109 {
110 }
111
112
113 Thesaurus::Meanings Thesaurus::lookup(docstring const &)
114 {
115         return Meanings();
116 }
117
118 #endif // HAVE_LIBAIKSAURUS
119
120 // Global instance
121 Thesaurus thesaurus;
122
123
124 } // namespace lyx