|
| 1 | +// |
| 2 | +// Copyright RIME Developers |
| 3 | +// Distributed under the BSD License |
| 4 | +// |
| 5 | +// 2016-09-08 osfans <waxaca@163.com> |
| 6 | +// |
| 7 | + |
| 8 | +#include <rime/candidate.h> |
| 9 | +#include <rime/context.h> |
| 10 | +#include <rime/engine.h> |
| 11 | +#include <rime/schema.h> |
| 12 | +#include <rime/ticket.h> |
| 13 | +#include <rime/translation.h> |
| 14 | +#include <rime/gear/history_translator.h> |
| 15 | + |
| 16 | +namespace rime { |
| 17 | + |
| 18 | +HistoryTranslator::HistoryTranslator(const Ticket& ticket) |
| 19 | + : Translator(ticket), |
| 20 | + tag_("abc"), |
| 21 | + size_(1), |
| 22 | + initial_quality_(1000) { |
| 23 | + if (ticket.name_space == "translator") { |
| 24 | + name_space_ = "history"; |
| 25 | + } |
| 26 | + if (!ticket.schema) |
| 27 | + return; |
| 28 | + Config* config = ticket.schema->config(); |
| 29 | + config->GetString(name_space_ + "/tag", &tag_); |
| 30 | + config->GetString(name_space_ + "/input", &input_); |
| 31 | + config->GetInt(name_space_ + "/size", &size_); |
| 32 | + config->GetDouble(name_space_ + "/initial_quality", |
| 33 | + &initial_quality_); |
| 34 | +} |
| 35 | + |
| 36 | +an<Translation> HistoryTranslator::Query(const string& input, |
| 37 | + const Segment& segment) { |
| 38 | + if (!segment.HasTag(tag_)) |
| 39 | + return nullptr; |
| 40 | + if (input_.empty() || input_ != input) |
| 41 | + return nullptr; |
| 42 | + |
| 43 | + const auto& history(engine_->context()->commit_history()); |
| 44 | + if (history.empty()) |
| 45 | + return nullptr; |
| 46 | + auto translation = New<FifoTranslation>(); |
| 47 | + auto it = history.rbegin(); |
| 48 | + int count = 0; |
| 49 | + for (; it != history.rend(); ++it) { |
| 50 | + if (it->type == "thru") continue; |
| 51 | + auto candidate = New<SimpleCandidate>(it->type, |
| 52 | + segment.start, |
| 53 | + segment.end, |
| 54 | + it->text); |
| 55 | + candidate->set_quality(initial_quality_); |
| 56 | + translation->Append(candidate); |
| 57 | + count++; |
| 58 | + if (size_ == count) break; |
| 59 | + } |
| 60 | + return translation; |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace rime |
0 commit comments