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());
134 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
135 if (contains(it->onlyFileName(), basename)) {
136 data = it->absFileName();
137 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
141 return make_pair(idx, data);
145 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
147 string const thes_path = external_path(lyxrc.thesaurusdir_path);
148 pair<string,string> result ;
150 if (thesaurusAvailable(lang))
151 return make_pair(string(), string());
153 if (!thes_path.empty()) {
154 result = getThesaurus(thes_path, lang);
156 if (result.first.empty() || result.second.empty()) {
157 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
158 result = getThesaurus(sys_path, lang);
160 if (result.first.empty() || result.second.empty()) {
161 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
162 result = getThesaurus(user_path, lang);
168 bool Thesaurus::Private::addThesaurus(docstring const & lang)
170 if (thesaurusAvailable(lang))
173 ThesFiles files = getThesaurus(lang);
174 string const idx = files.first;
175 string const data = files.second;
177 if (idx.empty() || data.empty())
180 char const * af = idx.c_str();
181 char const * df = data.c_str();
182 thes_[lang] = new MyThes(af, df);
187 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
189 return d->thesaurusAvailable(lang);
193 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
195 if (thesaurusAvailable(lang))
197 pair<string, string> files = d->getThesaurus(lang);
198 return (!files.first.empty() && !files.second.empty());
202 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
207 docstring const lang_code = from_ascii(wl.lang()->code());
208 docstring const t = wl.word();
210 if (!d->addThesaurus(lang_code))
213 for (Thesauri::const_iterator it = d->thes_.begin();
214 it != d->thes_.end(); ++it) {
215 if (it->first == lang_code) {
224 string const encoding = mythes->get_th_encoding();
227 string const text = to_iconv_encoding(support::lowercase(t), encoding);
228 int len = strlen(text.c_str());
229 int count = mythes->Lookup(text.c_str(), len, &pmean);
231 SpellChecker * speller = theSpellChecker();
234 docstring_list suggestions;
235 speller->stem(wl, suggestions);
236 for (size_t i = 0; i != suggestions.size(); ++i) {
237 string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
238 len = strlen(wordform.c_str());
239 count = mythes->Lookup(wordform.c_str(), len, &pmean);
247 // don't change value of pmean or count
248 // they are needed for the CleanUpAfterLookup routine
251 for (int i = 0; i < count; i++) {
252 vector<docstring> ret;
253 meaning = from_iconv_encoding(string(pm->defn), encoding);
255 if (support::prefixIs(meaning, '-'))
256 meaning = support::ltrim(meaning, "- ");
257 for (int j = 0; j < pm->count; j++) {
258 ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
260 meanings[meaning] = ret;
263 // now clean up all allocated memory
264 mythes->CleanUpAfterLookup(&pmean, count);
266 for (Meanings::iterator it = meanings.begin();
267 it != meanings.end(); ++it)
268 sort(it->second.begin(), it->second.end());
274 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
279 Thesaurus::~Thesaurus()