Skip to content

Commit ae13354

Browse files
authored
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
1 parent 57bf14a commit ae13354

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright RIME Developers
3+
// Distributed under the BSD License
4+
//
5+
// 2016-09-08 osfans <waxaca@163.com>
6+
//
7+
#ifndef RIME_HISTORY_TRANSLATOR_H_
8+
#define RIME_HISTORY_TRANSLATOR_H_
9+
10+
#include <rime/translator.h>
11+
12+
namespace rime {
13+
14+
class HistoryTranslator : public Translator {
15+
public:
16+
HistoryTranslator(const Ticket& ticket);
17+
18+
virtual an<Translation> Query(const string& input,
19+
const Segment& segment);
20+
21+
protected:
22+
string tag_;
23+
string input_;
24+
int size_;
25+
double initial_quality_;
26+
};
27+
28+
} // namespace rime
29+
30+
#endif // RIME_HISTORY_TRANSLATOR_H_

src/gear/gears_module.cc

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <rime/gear/table_translator.h>
3737
#include <rime/gear/uniquifier.h>
3838
#include <rime/gear/codepoint_translator.h>
39+
#include <rime/gear/history_translator.h>
3940

4041
static void rime_gears_initialize() {
4142
using namespace rime;
@@ -76,6 +77,7 @@ static void rime_gears_initialize() {
7677
r.Register("schema_list_translator", new Component<SchemaListTranslator>);
7778
r.Register("switch_translator", new Component<SwitchTranslator>);
7879
r.Register("codepoint_translator", new Component<CodepointTranslator>);
80+
r.Register("history_translator", new Component<HistoryTranslator>);
7981

8082
// filters
8183
r.Register("simplifier", new Component<Simplifier>);

src/gear/history_translator.cc

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)