2001-07-29 10:42:11 +00:00
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file Thesaurus.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-07-29 10:42:11 +00:00
|
|
|
|
*
|
|
|
|
|
* \author John Levon
|
2007-05-28 07:43:15 +00:00
|
|
|
|
* \author J<EFBFBD>rgen Spitzm<EFBFBD>ller
|
2003-08-23 00:17:00 +00:00
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2001-07-29 10:42:11 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2001-12-20 15:11:51 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
|
#include "Thesaurus.h"
|
|
|
|
|
|
2007-05-28 07:43:15 +00:00
|
|
|
|
#include "debug.h"
|
2006-12-10 11:52:46 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2007-05-28 07:43:15 +00:00
|
|
|
|
#include "support/unicode.h"
|
2006-12-10 11:52:46 +00:00
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
|
#include <algorithm>
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
using std::sort;
|
2006-12-10 11:52:46 +00:00
|
|
|
|
using std::string;
|
2007-05-28 07:43:15 +00:00
|
|
|
|
using std::endl;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2007-05-28 07:43:15 +00:00
|
|
|
|
#ifndef HAVE_LIBMYTHES
|
|
|
|
|
#ifdef HAVE_LIBAIKSAURUS
|
2001-07-29 10:42:11 +00:00
|
|
|
|
Thesaurus::Thesaurus()
|
2007-05-28 07:43:15 +00:00
|
|
|
|
: thes_(new Aiksaurus)
|
2003-11-03 17:47:28 +00:00
|
|
|
|
{}
|
2001-07-29 10:42:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thesaurus::~Thesaurus()
|
|
|
|
|
{
|
2007-05-28 07:43:15 +00:00
|
|
|
|
delete thes_;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-10 11:52:46 +00:00
|
|
|
|
Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
|
2001-07-29 10:42:11 +00:00
|
|
|
|
{
|
2002-01-13 01:46:33 +00:00
|
|
|
|
Meanings meanings;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
|
2006-12-10 11:52:46 +00:00
|
|
|
|
// aiksaurus is for english text only, therefore it does not work
|
|
|
|
|
// with non-ascii strings.
|
|
|
|
|
// The interface of the Thesaurus class uses docstring because a
|
|
|
|
|
// non-english thesaurus is possible in theory.
|
|
|
|
|
if (!support::isAscii(t))
|
|
|
|
|
// to_ascii() would assert
|
|
|
|
|
return meanings;
|
|
|
|
|
|
|
|
|
|
string const text = to_ascii(t);
|
2007-05-28 07:43:15 +00:00
|
|
|
|
if (!thes_->find(text.c_str()))
|
2002-01-13 01:46:33 +00:00
|
|
|
|
return meanings;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
|
// weird api, but ...
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
|
int prev_meaning = -1;
|
|
|
|
|
int cur_meaning;
|
2006-12-10 11:52:46 +00:00
|
|
|
|
docstring meaning;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
// correct, returns "" at the end
|
2007-05-28 07:43:15 +00:00
|
|
|
|
string ret = thes_->next(cur_meaning);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
|
while (!ret.empty()) {
|
2002-01-13 01:46:33 +00:00
|
|
|
|
if (cur_meaning != prev_meaning) {
|
2006-12-10 11:52:46 +00:00
|
|
|
|
meaning = from_ascii(ret);
|
2007-05-28 07:43:15 +00:00
|
|
|
|
ret = thes_->next(cur_meaning);
|
2002-01-13 01:46:33 +00:00
|
|
|
|
prev_meaning = cur_meaning;
|
|
|
|
|
} else {
|
2006-12-10 11:52:46 +00:00
|
|
|
|
if (ret != text)
|
|
|
|
|
meanings[meaning].push_back(from_ascii(ret));
|
2002-01-13 01:46:33 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2007-05-28 07:43:15 +00:00
|
|
|
|
ret = thes_->next(cur_meaning);
|
2001-07-29 10:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
|
for (Meanings::iterator it = meanings.begin();
|
2006-12-10 11:52:46 +00:00
|
|
|
|
it != meanings.end(); ++it)
|
|
|
|
|
sort(it->second.begin(), it->second.end());
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
|
return meanings;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-05-28 07:43:15 +00:00
|
|
|
|
#endif // HAVE_LIBAIKSAURUS
|
|
|
|
|
#endif // !HAVE_LIBMYTHES
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBMYTHES
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
string const to_iconv_encoding(docstring const & s, string const & encoding)
|
|
|
|
|
{
|
|
|
|
|
std::vector<char> const encoded =
|
|
|
|
|
ucs4_to_eightbit(s.data(), s.length(), encoding);
|
|
|
|
|
return string(encoded.begin(), encoded.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
docstring const from_iconv_encoding(string const & s, string const & encoding)
|
|
|
|
|
{
|
|
|
|
|
std::vector<char_type> const ucs4 =
|
|
|
|
|
eightbit_to_ucs4(s.data(), s.length(), encoding);
|
|
|
|
|
return docstring(ucs4.begin(), ucs4.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thesaurus::Thesaurus()
|
|
|
|
|
{
|
|
|
|
|
string const idx("/home/juergen/updates/MyThes-1.0/th_de_DE_v2.idx");
|
|
|
|
|
string const data("/home/juergen/updates/MyThes-1.0/th_de_DE_v2.dat");
|
|
|
|
|
char const * af = idx.c_str();
|
|
|
|
|
char const * df = data.c_str();
|
|
|
|
|
thes_ = new MyThes(af, df);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thesaurus::~Thesaurus()
|
|
|
|
|
{
|
|
|
|
|
if (thes_)
|
|
|
|
|
delete thes_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
|
|
|
|
|
{
|
|
|
|
|
Meanings meanings;
|
|
|
|
|
|
|
|
|
|
string const encoding = thes_->get_th_encoding();
|
|
|
|
|
|
|
|
|
|
mentry * pmean;
|
|
|
|
|
string const text = to_iconv_encoding(support::lowercase(t), encoding);
|
|
|
|
|
int len = strlen(text.c_str());
|
|
|
|
|
int count = thes_->Lookup(text.c_str(), len, &pmean);
|
|
|
|
|
if (!count)
|
|
|
|
|
return meanings;
|
|
|
|
|
|
|
|
|
|
// don't change value of pmean or count
|
|
|
|
|
// they are needed for the CleanUpAfterLookup routine
|
|
|
|
|
mentry * pm = pmean;
|
|
|
|
|
docstring meaning;
|
|
|
|
|
docstring ret;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
meaning = from_iconv_encoding(string(pm->defn), encoding);
|
|
|
|
|
// remove silly item
|
|
|
|
|
if (support::prefixIs(meaning, '-'))
|
|
|
|
|
meaning = support::ltrim(meaning, "- ");
|
|
|
|
|
for (int j = 0; j < pm->count; j++) {
|
|
|
|
|
ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
|
|
|
|
|
}
|
|
|
|
|
meanings[meaning].push_back(ret);
|
|
|
|
|
pm++;
|
|
|
|
|
}
|
|
|
|
|
// now clean up all allocated memory
|
|
|
|
|
thes_->CleanUpAfterLookup(&pmean, count);
|
|
|
|
|
|
|
|
|
|
for (Meanings::iterator it = meanings.begin();
|
|
|
|
|
it != meanings.end(); ++it)
|
|
|
|
|
sort(it->second.begin(), it->second.end());
|
|
|
|
|
|
|
|
|
|
return meanings;
|
|
|
|
|
}
|
|
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
Thesaurus::Thesaurus()
|
|
|
|
|
{
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
|
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
|
Thesaurus::~Thesaurus()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2006-12-10 11:52:46 +00:00
|
|
|
|
Thesaurus::Meanings Thesaurus::lookup(docstring const &)
|
2001-07-29 10:42:11 +00:00
|
|
|
|
{
|
2002-01-13 01:46:33 +00:00
|
|
|
|
return Meanings();
|
2001-07-29 10:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-05-28 07:43:15 +00:00
|
|
|
|
#endif // HAVE_LIBMYTHES
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
// Global instance
|
2002-03-21 17:27:08 +00:00
|
|
|
|
Thesaurus thesaurus;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|