-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translator): add history_translator (#115)
* 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
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |