2012-10-30 16:59:47 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2012-10-30 16:59:47 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2012-10-30 16:59:47 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file thesaurus.cpp
|
2012-01-08 02:36:50 +01:00
|
|
|
/// @brief Thesaurus implementation
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @ingroup thesaurus
|
|
|
|
///
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
#include "thesaurus.h"
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2013-12-23 01:10:34 +01:00
|
|
|
#include <boost/range/algorithm.hpp>
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
#include <libaegisub/log.h>
|
2013-01-30 04:35:37 +01:00
|
|
|
#include <libaegisub/path.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
#include <libaegisub/thesaurus.h>
|
2013-12-23 01:10:34 +01:00
|
|
|
#include <libaegisub/util.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
|
|
|
|
Thesaurus::Thesaurus()
|
|
|
|
: lang_listener(OPT_SUB("Tool/Thesaurus/Language", &Thesaurus::OnLanguageChanged, this))
|
|
|
|
, dict_path_listener(OPT_SUB("Path/Dictionary", &Thesaurus::OnPathChanged, this))
|
|
|
|
{
|
|
|
|
OnLanguageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thesaurus::~Thesaurus() {
|
|
|
|
// Explicit empty destructor needed for scoped_ptr with incomplete types
|
|
|
|
}
|
|
|
|
|
2013-11-02 15:52:45 +01:00
|
|
|
std::vector<Thesaurus::Entry> Thesaurus::Lookup(std::string word) {
|
2013-12-12 00:11:06 +01:00
|
|
|
if (!impl.get()) return {};
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::to_lower(word);
|
2013-11-02 15:52:45 +01:00
|
|
|
return impl->Lookup(word);
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
static std::vector<std::string> langs(const char *ext) {
|
|
|
|
std::vector<std::string> paths;
|
|
|
|
auto data_path = config::path->Decode("?data/dictionaries/");
|
|
|
|
auto user_path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString());
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
auto filter = std::string("th_*.") + ext;
|
|
|
|
agi::fs::DirectoryIterator(data_path, filter).GetAll(paths);
|
|
|
|
agi::fs::DirectoryIterator(user_path, filter).GetAll(paths);
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
// Drop extensions and the th_ prefix
|
|
|
|
for (auto& fn : paths) fn = fn.substr(3, fn.size() - filter.size() + 1);
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
boost::sort(paths);
|
|
|
|
paths.erase(unique(begin(paths), end(paths)), end(paths));
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
return paths;
|
|
|
|
}
|
2012-01-08 02:36:50 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
std::vector<std::string> Thesaurus::GetLanguageList() const {
|
|
|
|
if (languages.empty())
|
|
|
|
boost::set_intersection(langs("idx"), langs("dat"), back_inserter(languages));
|
2012-01-08 02:36:50 +01:00
|
|
|
return languages;
|
2006-12-25 22:56:56 +01:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
static bool check_path(agi::fs::path const& path, std::string const& language, agi::fs::path& idx, agi::fs::path& dat) {
|
|
|
|
idx = path/str(boost::format("th_%s.idx") % language);
|
|
|
|
dat = path/str(boost::format("th_%s.dat") % language);
|
|
|
|
return agi::fs::FileExists(idx) && agi::fs::FileExists(dat);
|
|
|
|
}
|
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
void Thesaurus::OnLanguageChanged() {
|
|
|
|
impl.reset();
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
auto language = OPT_GET("Tool/Thesaurus/Language")->GetString();
|
2012-01-08 02:36:50 +01:00
|
|
|
if (language.empty()) return;
|
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
agi::fs::path idx, dat;
|
2012-01-08 02:36:50 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
auto path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString() + "/");
|
|
|
|
if (!check_path(path, language, idx, dat)) {
|
2013-01-30 04:35:37 +01:00
|
|
|
path = config::path->Decode("?data/dictionaries/");
|
2013-12-23 01:10:34 +01:00
|
|
|
if (!check_path(path, language, idx, dat))
|
|
|
|
return;
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
LOG_I("thesaurus/file") << "Using thesaurus: " << dat;
|
2012-01-08 02:36:50 +01:00
|
|
|
|
2013-12-23 01:10:34 +01:00
|
|
|
impl = agi::util::make_unique<agi::Thesaurus>(dat, idx);
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thesaurus::OnPathChanged() {
|
|
|
|
languages.clear();
|
|
|
|
}
|