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"
18 #include "SpellChecker.h"
19 #include "WordLangTuple.h"
21 #include "support/FileNameList.h"
22 #include "support/Package.h"
23 #include "support/debug.h"
24 #include "support/docstring_list.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
30 #ifdef USE_EXTERNAL_MYTHES
31 #include MYTHES_H_LOCATION
34 #include "support/mythes/mythes.hxx"
37 #include "frontends/alert.h"
44 using namespace lyx::support;
45 using namespace lyx::support::os;
51 typedef std::map<docstring, MyThes *> Thesauri;
55 struct Thesaurus::Private
59 for (Thesauri::iterator it = thes_.begin();
60 it != thes_.end(); ++it) {
65 bool thesaurusAvailable(docstring const & lang) const
67 for (Thesauri::const_iterator it = thes_.begin();
68 it != thes_.end(); ++it) {
69 if (it->first == lang)
77 typedef std::pair<std::string, std::string> ThesFiles;
79 ThesFiles getThesaurus(string const & path, docstring const & lang);
80 ThesFiles getThesaurus(docstring const & lang);
81 /// add a thesaurus to the list
82 bool addThesaurus(docstring const & lang);
87 /// the location below system/user directory
88 /// there the data+idx files lookup will happen
89 const string dataDirectory(void) { return "thes"; }
94 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
97 if (!base.isDirectory()) {
98 return make_pair(string(), string());
100 FileNameList const idx_files = base.dirList("idx");
101 FileNameList const data_files = base.dirList("dat");
106 LYXERR(Debug::FILES, "thesaurus path: " << path);
107 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
108 basename = it->onlyFileNameWithoutExt();
109 if (contains(basename, to_ascii(lang))) {
110 ifstream ifs(it->absFileName().c_str());
112 // check for appropriate version of index file
113 string encoding; // first line is encoding
114 int items = 0; // second line is no. of items
115 getline(ifs,encoding);
118 LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
121 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
122 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
126 idx = it->absFileName();
127 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
132 return make_pair(string(), string());
133 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
134 if (contains(it->onlyFileName(), basename)) {
135 data = it->absFileName();
136 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
140 return make_pair(idx, data);
144 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
146 string const thes_path = external_path(lyxrc.thesaurusdir_path);
147 pair<string,string> result ;
149 if (thesaurusAvailable(lang))
150 return make_pair(string(), string());
152 if (!thes_path.empty())
153 result = getThesaurus(thes_path, lang);
154 if (result.first.empty() || result.second.empty()) {
155 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
156 result = getThesaurus(sys_path, lang);
158 if (result.first.empty() || result.second.empty()) {
159 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
160 result = getThesaurus(user_path, lang);
166 bool Thesaurus::Private::addThesaurus(docstring const & lang)
168 if (thesaurusAvailable(lang))
171 ThesFiles files = getThesaurus(lang);
172 string const idx = files.first;
173 string const data = files.second;
175 if (idx.empty() || data.empty())
178 char const * af = idx.c_str();
179 char const * df = data.c_str();
180 thes_[lang] = new MyThes(af, df);
185 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
187 return d->thesaurusAvailable(lang);
191 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
193 if (thesaurusAvailable(lang))
195 pair<string, string> files = d->getThesaurus(lang);
196 return (!files.first.empty() && !files.second.empty());
200 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
205 docstring const lang_code = from_ascii(wl.lang()->code());
206 docstring const t = wl.word();
208 if (!d->addThesaurus(lang_code))
211 for (Thesauri::const_iterator it = d->thes_.begin();
212 it != d->thes_.end(); ++it) {
213 if (it->first == lang_code) {
222 string const encoding = mythes->get_th_encoding();
225 string const text = to_iconv_encoding(support::lowercase(t), encoding);
226 int len = strlen(text.c_str());
227 int count = mythes->Lookup(text.c_str(), len, &pmean);
229 SpellChecker * speller = theSpellChecker();
232 docstring_list suggestions;
233 speller->stem(wl, suggestions);
234 for (size_t i = 0; i != suggestions.size(); ++i) {
235 string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
236 len = strlen(wordform.c_str());
237 count = mythes->Lookup(wordform.c_str(), len, &pmean);
245 // don't change value of pmean or count
246 // they are needed for the CleanUpAfterLookup routine
249 for (int i = 0; i < count; i++) {
250 vector<docstring> ret;
251 meaning = from_iconv_encoding(string(pm->defn), encoding);
253 if (support::prefixIs(meaning, '-'))
254 meaning = support::ltrim(meaning, "- ");
255 for (int j = 0; j < pm->count; j++) {
256 ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
258 meanings[meaning] = ret;
261 // now clean up all allocated memory
262 mythes->CleanUpAfterLookup(&pmean, count);
264 for (Meanings::iterator it = meanings.begin();
265 it != meanings.end(); ++it)
266 sort(it->second.begin(), it->second.end());
272 Thesaurus::Thesaurus()
273 : d(new Thesaurus::Private)
278 Thesaurus::~Thesaurus()