-
Notifications
You must be signed in to change notification settings - Fork 563
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
protected: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_ |
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the default value. |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's handle the |
||
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 |
There was a problem hiding this comment.
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.