]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
transfer os::is_absolute_path() to FileName::isAbsolute().
[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 "support/gettext.h"
16
17 #include "support/lstrings.h"
18
19 #include "frontends/alert.h"
20
21 #include <algorithm>
22
23 using namespace std;
24
25 namespace lyx {
26
27 #ifdef HAVE_LIBAIKSAURUS
28 using support::bformat;
29
30
31 Thesaurus::Thesaurus()
32         : aik_(new Aiksaurus)
33 {}
34
35
36 Thesaurus::~Thesaurus()
37 {
38         delete aik_;
39 }
40
41
42 Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
43 {
44         Meanings meanings;
45
46         // aiksaurus is for english text only, therefore it does not work
47         // with non-ascii strings.
48         // The interface of the Thesaurus class uses docstring because a
49         // non-english thesaurus is possible in theory.
50         if (!support::isAscii(t))
51                 // to_ascii() would assert
52                 return meanings;
53
54         string const text = to_ascii(t);
55
56         docstring error = from_ascii(aik_->error());
57         if (!error.empty()) {
58                 static bool sent_error = false;
59                 if (!sent_error) {
60                         frontend::Alert::error(_("Thesaurus failure"),
61                                      bformat(_("Aiksaurus returned the following error:\n\n%1$s."),
62                                              error));
63                         sent_error = true;
64                 }
65                 return meanings;
66         }
67         if (!aik_->find(text.c_str()))
68                 return meanings;
69
70         // weird api, but ...
71
72         int prev_meaning = -1;
73         int cur_meaning;
74         docstring meaning;
75
76         // correct, returns "" at the end
77         string ret = aik_->next(cur_meaning);
78
79         while (!ret.empty()) {
80                 if (cur_meaning != prev_meaning) {
81                         meaning = from_ascii(ret);
82                         ret = aik_->next(cur_meaning);
83                         prev_meaning = cur_meaning;
84                 } else {
85                         if (ret != text)
86                                 meanings[meaning].push_back(from_ascii(ret));
87                 }
88
89                 ret = aik_->next(cur_meaning);
90         }
91
92         for (Meanings::iterator it = meanings.begin();
93              it != meanings.end(); ++it)
94                 sort(it->second.begin(), it->second.end());
95
96         return meanings;
97 }
98
99 #else
100
101 Thesaurus::Thesaurus()
102 {
103 }
104
105
106 Thesaurus::~Thesaurus()
107 {
108 }
109
110
111 Thesaurus::Meanings Thesaurus::lookup(docstring const &)
112 {
113         return Meanings();
114 }
115
116 #endif // HAVE_LIBAIKSAURUS
117
118 // Global instance
119 Thesaurus thesaurus;
120
121
122 } // namespace lyx