Skip to content

Commit

Permalink
feat(translator_commons): allow a translator to take multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Sep 2, 2024
1 parent 0cdce16 commit ef02973
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ an<Translation> ScriptTranslator::Query(const string& input,
const Segment& segment) {
if (!dict_ || !dict_->loaded())
return nullptr;
if (!segment.HasTag(tag_))
if (!segment.HasAnyTagIn(tags_))
return nullptr;
DLOG(INFO) << "input = '" << input << "', [" << segment.start << ", "
<< segment.end << ")";
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static bool starts_with_completion(an<Translation> translation) {

an<Translation> TableTranslator::Query(const string& input,
const Segment& segment) {
if (!segment.HasTag(tag_))
if (!segment.HasAnyTagIn(tags_))
return nullptr;
DLOG(INFO) << "input = '" << input << "', [" << segment.start << ", "
<< segment.end << ")";
Expand Down
15 changes: 14 additions & 1 deletion src/rime/gear/translator_commons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ TranslatorOptions::TranslatorOptions(const Ticket& ticket) {
if (Config* config = ticket.schema->config()) {
config->GetString(ticket.name_space + "/delimiter", &delimiters_) ||
config->GetString("speller/delimiter", &delimiters_);
config->GetString(ticket.name_space + "/tag", &tag_);
config->GetBool(ticket.name_space + "/contextual_suggestions",
&contextual_suggestions_);
config->GetBool(ticket.name_space + "/enable_completion",
Expand All @@ -132,6 +131,20 @@ TranslatorOptions::TranslatorOptions(const Ticket& ticket) {
config->GetList(ticket.name_space + "/comment_format"));
user_dict_disabling_patterns_.Load(
config->GetList(ticket.name_space + "/disable_user_dict_for_patterns"));
string tag;
if (config->GetString(ticket.name_space + "/tag", &tag)) {
// replace the first tag, and understand /tags as extra tags
tags_[0] = tag;
} else {
// replace all of the default tags
tags_.clear();
}
if (auto list = config->GetList(ticket.name_space + "/tags"))
for (size_t i = 0; i < list->size(); ++i)
if (auto value = As<ConfigValue>(list->GetAt(i)))
tags_.push_back(value->str());
if (tags_.empty())
tags_.push_back("abc");
}
if (delimiters_.empty()) {
delimiters_ = " ";
Expand Down
13 changes: 10 additions & 3 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,15 @@ class TranslatorOptions {
bool IsUserDictDisabledFor(const string& input) const;

const string& delimiters() const { return delimiters_; }
const string& tag() const { return tag_; }
void set_tag(const string& tag) { tag_ = tag; }
vector<string> tags() const { return tags_; }
void set_tags(const vector<string>& tags) {
tags_ = tags;
if (tags_.size() == 0) {
tags_.push_back("abc");
}
}
const string& tag() const { return tags_[0]; }
void set_tag(const string& tag) { tags_[0] = tag; }
bool contextual_suggestions() const { return contextual_suggestions_; }
void set_contextual_suggestions(bool enabled) {
contextual_suggestions_ = enabled;
Expand All @@ -163,7 +170,7 @@ class TranslatorOptions {

protected:
string delimiters_;
string tag_ = "abc";
vector<string> tags_{"abc"}; // invariant: non-empty
bool contextual_suggestions_ = false;
bool enable_completion_ = true;
bool strict_spelling_ = false;
Expand Down
4 changes: 4 additions & 0 deletions src/rime/segmentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ struct Segment {
bool Reopen(size_t caret_pos);

bool HasTag(const string& tag) const { return tags.find(tag) != tags.end(); }
bool HasAnyTagIn(const vector<string>& tags) const {
return std::any_of(tags.begin(), tags.end(),
[this](const string& tag) { return HasTag(tag); });
}

an<Candidate> GetCandidateAt(size_t index) const;
an<Candidate> GetSelectedCandidate() const;
Expand Down

0 comments on commit ef02973

Please sign in to comment.