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
|
|
|
|
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"
|
|
|
|
|
2013-12-20 04:44:29 +01:00
|
|
|
#include <libaegisub/dispatch.h>
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
#include <libaegisub/log.h>
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/make_unique.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>
|
2014-05-29 00:19:05 +02:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <boost/range/algorithm.hpp>
|
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() {
|
2014-07-11 04:57:19 +02:00
|
|
|
if (cancel_load) *cancel_load = true;
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2013-11-02 15:52:45 +01:00
|
|
|
std::vector<Thesaurus::Entry> Thesaurus::Lookup(std::string word) {
|
2014-11-04 17:27:36 +01:00
|
|
|
if (!impl) 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) {
|
2014-05-29 00:19:05 +02:00
|
|
|
idx = path/agi::format("th_%s.idx", language);
|
|
|
|
dat = path/agi::format("th_%s.dat", language);
|
2013-12-23 01:10:34 +01:00
|
|
|
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
|
|
|
|
2014-07-11 04:57:19 +02:00
|
|
|
if (cancel_load) *cancel_load = true;
|
|
|
|
cancel_load = new bool{false};
|
|
|
|
auto cancel = cancel_load; // Needed to avoid capturing via `this`
|
2013-12-20 04:44:29 +01:00
|
|
|
agi::dispatch::Background().Async([=]{
|
|
|
|
try {
|
2014-04-23 22:53:24 +02:00
|
|
|
auto thes = agi::make_unique<agi::Thesaurus>(dat, idx);
|
2014-07-11 04:57:19 +02:00
|
|
|
agi::dispatch::Main().Sync([&thes, cancel, this]{
|
|
|
|
if (!*cancel) {
|
|
|
|
impl = std::move(thes);
|
|
|
|
cancel_load = nullptr;
|
|
|
|
}
|
|
|
|
delete cancel;
|
2013-12-20 04:44:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
2014-05-29 14:57:27 +02:00
|
|
|
LOG_E("thesaurus") << e.GetMessage();
|
2013-12-20 04:44:29 +01:00
|
|
|
}
|
|
|
|
});
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thesaurus::OnPathChanged() {
|
|
|
|
languages.clear();
|
|
|
|
}
|