From 444d354d3f3e64d2212ca33be437c3b8b3236154 Mon Sep 17 00:00:00 2001 From: Pengfei Wu Date: Wed, 11 Dec 2024 16:25:51 +0800 Subject: [PATCH] feat(translator_commons): support excluding tags from translation --- src/rime/gear/script_translator.cc | 2 +- src/rime/gear/table_translator.cc | 2 +- src/rime/gear/translator_commons.cc | 4 ++++ src/rime/gear/translator_commons.h | 3 +++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/rime/gear/script_translator.cc b/src/rime/gear/script_translator.cc index d128fb035f..ed0c45481a 100644 --- a/src/rime/gear/script_translator.cc +++ b/src/rime/gear/script_translator.cc @@ -194,7 +194,7 @@ an ScriptTranslator::Query(const string& input, const Segment& segment) { if (!dict_ || !dict_->loaded()) return nullptr; - if (!segment.HasAnyTagIn(tags_)) + if (!segment.HasAnyTagIn(tags_) || segment.HasAnyTagIn(exclude_tags_)) return nullptr; DLOG(INFO) << "input = '" << input << "', [" << segment.start << ", " << segment.end << ")"; diff --git a/src/rime/gear/table_translator.cc b/src/rime/gear/table_translator.cc index ccbc36fc2e..aac9431067 100644 --- a/src/rime/gear/table_translator.cc +++ b/src/rime/gear/table_translator.cc @@ -241,7 +241,7 @@ static bool starts_with_completion(an translation) { an TableTranslator::Query(const string& input, const Segment& segment) { - if (!segment.HasAnyTagIn(tags_)) + if (!segment.HasAnyTagIn(tags_) || segment.HasAnyTagIn(exclude_tags_)) return nullptr; DLOG(INFO) << "input = '" << input << "', [" << segment.start << ", " << segment.end << ")"; diff --git a/src/rime/gear/translator_commons.cc b/src/rime/gear/translator_commons.cc index bad043253b..205a185737 100644 --- a/src/rime/gear/translator_commons.cc +++ b/src/rime/gear/translator_commons.cc @@ -145,6 +145,10 @@ TranslatorOptions::TranslatorOptions(const Ticket& ticket) { tags_.push_back(value->str()); if (tags_.empty()) tags_.push_back("abc"); + if (auto list = config->GetList(ticket.name_space + "/exclude_tags")) + for (size_t i = 0; i < list->size(); ++i) + if (auto value = As(list->GetAt(i))) + exclude_tags_.push_back(value->str()); } if (delimiters_.empty()) { delimiters_ = " "; diff --git a/src/rime/gear/translator_commons.h b/src/rime/gear/translator_commons.h index 558559981a..96e9d9ce7b 100644 --- a/src/rime/gear/translator_commons.h +++ b/src/rime/gear/translator_commons.h @@ -155,6 +155,8 @@ class TranslatorOptions { } const string& tag() const { return tags_[0]; } void set_tag(const string& tag) { tags_[0] = tag; } + vector exclude_tags() const { return exclude_tags_; } + void set_exclude_tags(const vector& tags) { exclude_tags_ = tags; } bool contextual_suggestions() const { return contextual_suggestions_; } void set_contextual_suggestions(bool enabled) { contextual_suggestions_ = enabled; @@ -171,6 +173,7 @@ class TranslatorOptions { protected: string delimiters_; vector tags_{"abc"}; // invariant: non-empty + vector exclude_tags_{}; bool contextual_suggestions_ = false; bool enable_completion_ = true; bool strict_spelling_ = false;