Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(translator): add history_translator #115

Merged
merged 3 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/rime/gear/history_translator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2016-09-08 osfans <[email protected]>
//
#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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align this continued line to the open parenthesis on the previous line.


protected:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line.

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
66 changes: 66 additions & 0 deletions src/gear/history_translator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2016-09-08 osfans <[email protected]>
//

#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"),
input_("ls"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the default value.
The keyword is schema-specific and need to be explicitly defined.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align to the open parenthesis.

if (!segment.HasTag(tag_))
return nullptr;
if (!input_.empty() && input_ != input)
return nullptr;

const auto& history(engine_->context()->commit_history());
if (!history.empty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's handle the history.empty() branch first, so that the larger branch can be less indented.

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_ > 0 && size_ == count) break;
}
return translation;
} else {
return nullptr;
}
}

} // namespace rime