diff --git a/include/rime/gear/history_translator.h b/include/rime/gear/history_translator.h new file mode 100644 index 000000000..b039d78ea --- /dev/null +++ b/include/rime/gear/history_translator.h @@ -0,0 +1,30 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +// 2016-09-08 osfans +// +#ifndef RIME_HISTORY_TRANSLATOR_H_ +#define RIME_HISTORY_TRANSLATOR_H_ + +#include + +namespace rime { + +class HistoryTranslator : public Translator { + public: + HistoryTranslator(const Ticket& ticket); + + virtual an Query(const string& input, + const Segment& segment); + + protected: + string tag_; + string input_; + int size_; + double initial_quality_; +}; + +} // namespace rime + +#endif // RIME_HISTORY_TRANSLATOR_H_ diff --git a/src/gear/gears_module.cc b/src/gear/gears_module.cc index 33ea6edb5..ad175315d 100644 --- a/src/gear/gears_module.cc +++ b/src/gear/gears_module.cc @@ -36,6 +36,7 @@ #include #include #include +#include static void rime_gears_initialize() { using namespace rime; @@ -76,6 +77,7 @@ static void rime_gears_initialize() { r.Register("schema_list_translator", new Component); r.Register("switch_translator", new Component); r.Register("codepoint_translator", new Component); + r.Register("history_translator", new Component); // filters r.Register("simplifier", new Component); diff --git a/src/gear/history_translator.cc b/src/gear/history_translator.cc new file mode 100644 index 000000000..eb4469c9d --- /dev/null +++ b/src/gear/history_translator.cc @@ -0,0 +1,63 @@ +// +// Copyright RIME Developers +// Distributed under the BSD License +// +// 2016-09-08 osfans +// + +#include +#include +#include +#include +#include +#include +#include + +namespace rime { + +HistoryTranslator::HistoryTranslator(const Ticket& ticket) + : Translator(ticket), + tag_("abc"), + size_(1), + initial_quality_(1000) { + if (ticket.name_space == "translator") { + name_space_ = "history"; + } + if (!ticket.schema) + return; + Config* config = ticket.schema->config(); + config->GetString(name_space_ + "/tag", &tag_); + config->GetString(name_space_ + "/input", &input_); + config->GetInt(name_space_ + "/size", &size_); + config->GetDouble(name_space_ + "/initial_quality", + &initial_quality_); +} + +an HistoryTranslator::Query(const string& input, + const Segment& segment) { + if (!segment.HasTag(tag_)) + return nullptr; + if (input_.empty() || input_ != input) + return nullptr; + + const auto& history(engine_->context()->commit_history()); + if (history.empty()) + return nullptr; + auto translation = New(); + auto it = history.rbegin(); + int count = 0; + for (; it != history.rend(); ++it) { + if (it->type == "thru") continue; + auto candidate = New(it->type, + segment.start, + segment.end, + it->text); + candidate->set_quality(initial_quality_); + translation->Append(candidate); + count++; + if (size_ == count) break; + } + return translation; +} + +} // namespace rime