Skip to content

Commit

Permalink
feat(translator): add history_translator (#115)
Browse files Browse the repository at this point in the history
* feat(translator): add history_translator

* chore(history_translator): remove default value and align continued line to open parenthesis

* chore(history_translator): bring back some default values
  • Loading branch information
osfans authored Nov 28, 2016
1 parent 57bf14a commit ae13354
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/rime/gear/history_translator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2016-09-08 osfans <waxaca@163.com>
//
#ifndef RIME_HISTORY_TRANSLATOR_H_
#define RIME_HISTORY_TRANSLATOR_H_

#include <rime/translator.h>

namespace rime {

class HistoryTranslator : public Translator {
public:
HistoryTranslator(const Ticket& ticket);

virtual an<Translation> Query(const string& input,
const Segment& segment);

protected:
string tag_;
string input_;
int size_;
double initial_quality_;
};

} // namespace rime

#endif // RIME_HISTORY_TRANSLATOR_H_
2 changes: 2 additions & 0 deletions src/gear/gears_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <rime/gear/table_translator.h>
#include <rime/gear/uniquifier.h>
#include <rime/gear/codepoint_translator.h>
#include <rime/gear/history_translator.h>

static void rime_gears_initialize() {
using namespace rime;
Expand Down Expand Up @@ -76,6 +77,7 @@ static void rime_gears_initialize() {
r.Register("schema_list_translator", new Component<SchemaListTranslator>);
r.Register("switch_translator", new Component<SwitchTranslator>);
r.Register("codepoint_translator", new Component<CodepointTranslator>);
r.Register("history_translator", new Component<HistoryTranslator>);

// filters
r.Register("simplifier", new Component<Simplifier>);
Expand Down
63 changes: 63 additions & 0 deletions src/gear/history_translator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2016-09-08 osfans <waxaca@163.com>
//

#include <rime/candidate.h>
#include <rime/context.h>
#include <rime/engine.h>
#include <rime/schema.h>
#include <rime/ticket.h>
#include <rime/translation.h>
#include <rime/gear/history_translator.h>

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<Translation> 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<FifoTranslation>();
auto it = history.rbegin();
int count = 0;
for (; it != history.rend(); ++it) {
if (it->type == "thru") continue;
auto candidate = New<SimpleCandidate>(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

0 comments on commit ae13354

Please sign in to comment.