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.h"
25 #include "support/docstring_list.h"
26 #include "support/filetools.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
31 #include MYTHES_H_LOCATION
38 using namespace lyx::support;
39 using namespace lyx::support::os;
45 typedef std::map<docstring, MyThes *> Thesauri;
49 struct Thesaurus::Private
53 for (Thesauri::iterator it = thes_.begin();
54 it != thes_.end(); ++it) {
59 bool thesaurusAvailable(docstring const & lang) const
61 for (Thesauri::const_iterator it = thes_.begin();
62 it != thes_.end(); ++it) {
63 if (it->first == lang)
71 typedef std::pair<std::string, std::string> ThesFiles;
73 ThesFiles getThesaurus(string const & path, docstring const & lang);
74 ThesFiles getThesaurus(docstring const & lang);
75 /// add a thesaurus to the list
76 bool addThesaurus(docstring const & lang);
81 /// the location below system/user directory
82 /// there the data+idx files lookup will happen
83 const string dataDirectory(void) { return "thes"; }
88 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
91 if (!base.isDirectory()) {
92 return make_pair(string(), string());
94 FileNameList const idx_files = base.dirList("idx");
95 FileNameList const data_files = base.dirList("dat");
100 LYXERR(Debug::FILES, "thesaurus path: " << path);
101 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
102 basename = it->onlyFileNameWithoutExt();
103 if (contains(basename, to_ascii(lang))) {
104 // do not use more specific dicts.
105 if (contains(basename, '_') && !contains(lang, '_'))
107 if (contains(basename, '-') && !contains(lang, '-'))
109 ifstream ifs(it->absFileName().c_str());
111 // check for appropriate version of index file
112 string encoding; // first line is encoding
113 int items = 0; // second line is no. of items
114 getline(ifs,encoding);
117 LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
120 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
121 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
125 idx = it->absFileName();
126 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
131 // try with a more general dictionary
133 if (contains(lang, '_')) {
134 split(lang, shortcode, '_');
135 LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
136 << lang << ". Trying with " << shortcode);
137 return getThesaurus(path, shortcode);
139 else if (contains(lang, '-')) {
140 split(lang, shortcode, '-');
141 LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
142 << lang << ". Trying with " << shortcode);
143 return getThesaurus(path, shortcode);
145 return make_pair(string(), string());
147 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
148 if (contains(it->onlyFileName(), basename)) {
149 data = it->absFileName();
150 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
154 return make_pair(idx, data);
158 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
160 string const thes_path = external_path(lyxrc.thesaurusdir_path);
161 pair<string,string> result ;
163 if (thesaurusAvailable(lang))
164 return make_pair(string(), string());
166 if (!thes_path.empty())
167 result = getThesaurus(thes_path, lang);
168 if (result.first.empty() || result.second.empty()) {
169 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
170 result = getThesaurus(sys_path, lang);
172 if (result.first.empty() || result.second.empty()) {
173 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
174 result = getThesaurus(user_path, lang);
180 bool Thesaurus::Private::addThesaurus(docstring const & lang)
182 if (thesaurusAvailable(lang))
185 ThesFiles files = getThesaurus(lang);
186 string const idx = files.first;
187 string const data = files.second;
189 if (idx.empty() || data.empty())
192 char const * af = idx.c_str();
193 char const * df = data.c_str();
194 thes_[lang] = new MyThes(af, df);
199 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
201 return d->thesaurusAvailable(lang);
205 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
207 if (thesaurusAvailable(lang))
209 pair<string, string> files = d->getThesaurus(lang);
210 return (!files.first.empty() && !files.second.empty());
214 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
217 MyThes * mythes = nullptr;
219 docstring const lang_code = from_ascii(wl.lang()->code());
220 docstring const & t = wl.word();
222 if (!d->addThesaurus(lang_code))
225 for (Thesauri::const_iterator it = d->thes_.begin();
226 it != d->thes_.end(); ++it) {
227 if (it->first == lang_code) {
236 string const encoding = mythes->get_th_encoding();
239 string const text = to_iconv_encoding(support::lowercase(t), encoding);
240 int len = strlen(text.c_str());
241 int count = mythes->Lookup(text.c_str(), len, &pmean);
243 SpellChecker * speller = theSpellChecker();
246 docstring_list suggestions;
247 speller->stem(wl, suggestions);
248 for (size_t i = 0; i != suggestions.size(); ++i) {
249 string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
250 len = strlen(wordform.c_str());
251 count = mythes->Lookup(wordform.c_str(), len, &pmean);
259 // don't change value of pmean or count
260 // they are needed for the CleanUpAfterLookup routine
263 for (int i = 0; i < count; i++) {
264 vector<docstring> ret;
265 meaning = from_iconv_encoding(string(pm->defn), encoding);
267 if (support::prefixIs(meaning, '-'))
268 meaning = support::ltrim(meaning, "- ");
269 ret.reserve(pm->count);
270 for (int j = 0; j < pm->count; j++) {
271 ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
273 meanings[meaning] = ret;
276 // now clean up all allocated memory
277 mythes->CleanUpAfterLookup(&pmean, count);
279 for (Meanings::iterator it = meanings.begin();
280 it != meanings.end(); ++it)
281 sort(it->second.begin(), it->second.end());
287 Thesaurus::Thesaurus()
288 : d(new Thesaurus::Private)
293 Thesaurus::~Thesaurus()