From 0d5f9fd8b4142fc61c8f76c84796c597b4cfd636 Mon Sep 17 00:00:00 2001 From: osfans Date: Fri, 18 Nov 2016 14:29:38 +0800 Subject: [PATCH 1/3] feat(translator): add history_translator --- include/rime/gear/history_translator.h | 31 ++++++++++++ src/gear/gears_module.cc | 2 + src/gear/history_translator.cc | 66 ++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 include/rime/gear/history_translator.h create mode 100644 src/gear/history_translator.cc diff --git a/include/rime/gear/history_translator.h b/include/rime/gear/history_translator.h new file mode 100644 index 0000000000..5faefadf1c --- /dev/null +++ b/include/rime/gear/history_translator.h @@ -0,0 +1,31 @@ +// +// 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 33ea6edb5b..ad175315dc 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 0000000000..5533ade14e --- /dev/null +++ b/src/gear/history_translator.cc @@ -0,0 +1,66 @@ +// +// 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"), + input_("ls"), + 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()) { + 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_ > 0 && size_ == count) break; + } + return translation; + } else { + return nullptr; + } +} + +} // namespace rime From edb6c14a0558df883c11b90e9acb5bb0a45b9ad4 Mon Sep 17 00:00:00 2001 From: osfans Date: Wed, 23 Nov 2016 15:19:13 +0800 Subject: [PATCH 2/3] chore(history_translator): remove default value and align continued line to open parenthesis --- include/rime/gear/history_translator.h | 3 +- src/gear/history_translator.cc | 44 +++++++++++--------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/include/rime/gear/history_translator.h b/include/rime/gear/history_translator.h index 5faefadf1c..b039d78eaa 100644 --- a/include/rime/gear/history_translator.h +++ b/include/rime/gear/history_translator.h @@ -16,10 +16,9 @@ class HistoryTranslator : public Translator { HistoryTranslator(const Ticket& ticket); virtual an Query(const string& input, - const Segment& segment); + const Segment& segment); protected: - string tag_; string input_; int size_; diff --git a/src/gear/history_translator.cc b/src/gear/history_translator.cc index 5533ade14e..207ce6f87d 100644 --- a/src/gear/history_translator.cc +++ b/src/gear/history_translator.cc @@ -16,11 +16,7 @@ namespace rime { HistoryTranslator::HistoryTranslator(const Ticket& ticket) - : Translator(ticket), - tag_("abc"), - input_("ls"), - size_(1), - initial_quality_(1000) { + : Translator(ticket) { if (ticket.name_space == "translator") { name_space_ = "history"; } @@ -31,36 +27,34 @@ HistoryTranslator::HistoryTranslator(const Ticket& ticket) config->GetString(name_space_ + "/input", &input_); config->GetInt(name_space_ + "/size", &size_); config->GetDouble(name_space_ + "/initial_quality", - &initial_quality_); + &initial_quality_); } an HistoryTranslator::Query(const string& input, - const Segment& segment) { + const Segment& segment) { if (!segment.HasTag(tag_)) return nullptr; - if (!input_.empty() && input_ != input) + if (input_.empty() || input_ != input) return nullptr; const auto& history(engine_->context()->commit_history()); - if (!history.empty()) { - 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_ > 0 && size_ == count) break; - } - return translation; - } else { + 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 From 4c2692307ec665b104ad50d9f982cd930bfaf94b Mon Sep 17 00:00:00 2001 From: osfans Date: Thu, 24 Nov 2016 09:21:34 +0800 Subject: [PATCH 3/3] chore(history_translator): bring back some default values --- src/gear/history_translator.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/gear/history_translator.cc b/src/gear/history_translator.cc index 207ce6f87d..eb4469c9da 100644 --- a/src/gear/history_translator.cc +++ b/src/gear/history_translator.cc @@ -16,7 +16,10 @@ namespace rime { HistoryTranslator::HistoryTranslator(const Ticket& ticket) - : Translator(ticket) { + : Translator(ticket), + tag_("abc"), + size_(1), + initial_quality_(1000) { if (ticket.name_space == "translator") { name_space_ = "history"; }