Skip to content
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): allow a translator to take multiple tags #926

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not const vector<string>& due to a compilation eror in librime-lua which I don't know how to solve.

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