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"
31 #include MYTHES_H_LOCATION
33 #include "frontends/alert.h"
40 using namespace lyx::support;
41 using namespace lyx::support::os;
47 typedef std::map<docstring, MyThes *> Thesauri;
51 struct Thesaurus::Private
55 for (Thesauri::iterator it = thes_.begin();
56 it != thes_.end(); ++it) {
61 bool thesaurusAvailable(docstring const & lang) const
63 for (Thesauri::const_iterator it = thes_.begin();
64 it != thes_.end(); ++it) {
65 if (it->first == lang)
73 typedef std::pair<std::string, std::string> ThesFiles;
75 ThesFiles getThesaurus(string const & path, docstring const & lang);
76 ThesFiles getThesaurus(docstring const & lang);
77 /// add a thesaurus to the list
78 bool addThesaurus(docstring const & lang);
83 /// the location below system/user directory
84 /// there the data+idx files lookup will happen
85 const string dataDirectory(void) { return "thes"; }
90 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
93 if (!base.isDirectory()) {
94 return make_pair(string(), string());
96 FileNameList const idx_files = base.dirList("idx");
97 FileNameList const data_files = base.dirList("dat");
102 LYXERR(Debug::FILES, "thesaurus path: " << path);
103 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
104 basename = it->onlyFileNameWithoutExt();
105 if (contains(basename, to_ascii(lang))) {
106 // do not use more specific dicts.
107 if (contains(basename, '_') && !contains(lang, '_'))
109 if (contains(basename, '-') && !contains(lang, '-'))
111 ifstream ifs(it->absFileName().c_str());
113 // check for appropriate version of index file
114 string encoding; // first line is encoding
115 int items = 0; // second line is no. of items
116 getline(ifs,encoding);
119 LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
122 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
123 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
127 idx = it->absFileName();
128 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
133 // try with a more general dictionary
135 if (contains(lang, '_')) {
136 split(lang, shortcode, '_');
137 LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
138 << lang << ". Trying with " << shortcode);
139 return getThesaurus(path, shortcode);
141 else if (contains(lang, '-')) {
142 split(lang, shortcode, '-');
143 LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
144 << lang << ". Trying with " << shortcode);
145 return getThesaurus(path, shortcode);
147 return make_pair(string(), string());
149 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
150 if (contains(it->onlyFileName(), basename)) {
151 data = it->absFileName();
152 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
156 return make_pair(idx, data);
160 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
162 string const thes_path = external_path(lyxrc.thesaurusdir_path);
163 pair<string,string> result ;
165 if (thesaurusAvailable(lang))
166 return make_pair(string(), string());
168 if (!thes_path.empty())
169 result = getThesaurus(thes_path, lang);
170 if (result.first.empty() || result.second.empty()) {
171 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
172 result = getThesaurus(sys_path, lang);
174 if (result.first.empty() || result.second.empty()) {
175 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
176 result = getThesaurus(user_path, lang);
182 bool Thesaurus::Private::addThesaurus(docstring const & lang)
184 if (thesaurusAvailable(lang))
187 ThesFiles files = getThesaurus(lang);
188 string const idx = files.first;
189 string const data = files.second;
191 if (idx.empty() || data.empty())
194 char const * af = idx.c_str();
195 char const * df = data.c_str();
196 thes_[lang] = new MyThes(af, df);
201 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
203 return d->thesaurusAvailable(lang);
207 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
209 if (thesaurusAvailable(lang))
211 pair<string, string> files = d->getThesaurus(lang);
212 return (!files.first.empty() && !files.second.empty());
216 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
219 MyThes * mythes = nullptr;
221 docstring const lang_code = from_ascii(wl.lang()->code());
222 docstring const t = wl.word();
224 if (!d->addThesaurus(lang_code))
227 for (Thesauri::const_iterator it = d->thes_.begin();
228 it != d->thes_.end(); ++it) {
229 if (it->first == lang_code) {
238 string const encoding = mythes->get_th_encoding();
241 string const text = to_iconv_encoding(support::lowercase(t), encoding);
242 int len = strlen(text.c_str());
243 int count = mythes->Lookup(text.c_str(), len, &pmean);
245 SpellChecker * speller = theSpellChecker();
248 docstring_list suggestions;
249 speller->stem(wl, suggestions);
250 for (size_t i = 0; i != suggestions.size(); ++i) {
251 string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
252 len = strlen(wordform.c_str());
253 count = mythes->Lookup(wordform.c_str(), len, &pmean);
261 // don't change value of pmean or count
262 // they are needed for the CleanUpAfterLookup routine
265 for (int i = 0; i < count; i++) {
266 vector<docstring> ret;
267 meaning = from_iconv_encoding(string(pm->defn), encoding);
269 if (support::prefixIs(meaning, '-'))
270 meaning = support::ltrim(meaning, "- ");
271 for (int j = 0; j < pm->count; j++) {
272 ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
274 meanings[meaning] = ret;
277 // now clean up all allocated memory
278 mythes->CleanUpAfterLookup(&pmean, count);
280 for (Meanings::iterator it = meanings.begin();
281 it != meanings.end(); ++it)
282 sort(it->second.begin(), it->second.end());
288 Thesaurus::Thesaurus()
289 : d(new Thesaurus::Private)
294 Thesaurus::~Thesaurus()