Skip to content

Commit

Permalink
feat(simplifier): cache opencc (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzv5 authored Feb 12, 2025
1 parent e8184dc commit f9411ae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/rime/gear/gears_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void rime_gears_initialize() {
r.Register("history_translator", new Component<HistoryTranslator>);

// filters
r.Register("simplifier", new Component<Simplifier>);
r.Register("simplifier", new SimplifierComponent);
r.Register("uniquifier", new Component<Uniquifier>);
if (!r.Find("charset_filter")) { // allow improved implementation
r.Register("charset_filter", new Component<CharsetFilter>);
Expand Down
81 changes: 47 additions & 34 deletions src/rime/gear/simplifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class Opencc {

// Simplifier

Simplifier::Simplifier(const Ticket& ticket)
: Filter(ticket), TagMatching(ticket) {
Simplifier::Simplifier(const Ticket& ticket, an<Opencc> opencc)
: Filter(ticket), TagMatching(ticket), opencc_(opencc) {
if (name_space_ == "filter") {
name_space_ = "simplifier";
}
Expand All @@ -174,7 +174,6 @@ Simplifier::Simplifier(const Ticket& ticket)
comment_formatter_.Load(config->GetList(name_space_ + "/comment_format"));
config->GetBool(name_space_ + "/random", &random_);
config->GetString(name_space_ + "/option_name", &option_name_);
config->GetString(name_space_ + "/opencc_config", &opencc_config_);
if (auto types = config->GetList(name_space_ + "/excluded_types")) {
for (auto it = types->begin(); it != types->end(); ++it) {
if (auto value = As<ConfigValue>(*it)) {
Expand All @@ -186,39 +185,11 @@ Simplifier::Simplifier(const Ticket& ticket)
if (option_name_.empty()) {
option_name_ = "simplification"; // default switcher option
}
if (opencc_config_.empty()) {
opencc_config_ = "t2s.json"; // default opencc config file
}
if (random_) {
srand((unsigned)time(NULL));
}
}

void Simplifier::Initialize() {
initialized_ = true; // no retry
path opencc_config_path = path(opencc_config_);
if (opencc_config_path.extension().u8string() == ".ini") {
LOG(ERROR) << "please upgrade opencc_config to an opencc 1.0 config file.";
return;
}
if (opencc_config_path.is_relative()) {
path user_config_path = Service::instance().deployer().user_data_dir;
path shared_config_path = Service::instance().deployer().shared_data_dir;
(user_config_path /= "opencc") /= opencc_config_path;
(shared_config_path /= "opencc") /= opencc_config_path;
if (exists(user_config_path)) {
opencc_config_path = user_config_path;
} else if (exists(shared_config_path)) {
opencc_config_path = shared_config_path;
}
}
try {
opencc_.reset(new Opencc(opencc_config_path));
} catch (opencc::Exception& e) {
LOG(ERROR) << "Error initializing opencc: " << e.what();
}
}

class SimplifiedTranslation : public PrefetchTranslation {
public:
SimplifiedTranslation(an<Translation> translation, Simplifier* simplifier)
Expand All @@ -244,9 +215,6 @@ an<Translation> Simplifier::Apply(an<Translation> translation,
if (!engine_->context()->get_option(option_name_)) { // off
return translation;
}
if (!initialized_) {
Initialize();
}
if (!opencc_) {
return translation;
}
Expand Down Expand Up @@ -317,4 +285,49 @@ bool Simplifier::Convert(const an<Candidate>& original,
return success;
}

SimplifierComponent::SimplifierComponent() {}

Simplifier* SimplifierComponent::Create(const Ticket& ticket) {
string name_space = ticket.name_space;
if (name_space == "filter") {
name_space = "simplifier";
}
string opencc_config;
an<Opencc> opencc;
if (Config* config = ticket.engine->schema()->config()) {
config->GetString(name_space + "/opencc_config", &opencc_config);
}
if (opencc_config.empty()) {
opencc_config = "t2s.json"; // default opencc config file
}
opencc = opencc_map_[opencc_config].lock();
if (opencc) {
return new Simplifier(ticket, opencc);
}
path opencc_config_path = path(opencc_config);
if (opencc_config_path.extension().u8string() == ".ini") {
LOG(ERROR) << "please upgrade opencc_config to an opencc 1.0 config file.";
return nullptr;
}
if (opencc_config_path.is_relative()) {
path user_config_path = Service::instance().deployer().user_data_dir;
path shared_config_path = Service::instance().deployer().shared_data_dir;
(user_config_path /= "opencc") /= opencc_config_path;
(shared_config_path /= "opencc") /= opencc_config_path;
if (exists(user_config_path)) {
opencc_config_path = user_config_path;
} else if (exists(shared_config_path)) {
opencc_config_path = shared_config_path;
}
}
try {
opencc = New<Opencc>(opencc_config_path);
// 以原始配置中的文件路径作为 key,避免重复查找文件
opencc_map_[opencc_config] = opencc;
} catch (opencc::Exception& e) {
LOG(ERROR) << "Error initializing opencc: " << e.what();
}
return new Simplifier(ticket, opencc);
}

} // namespace rime
16 changes: 11 additions & 5 deletions src/rime/gear/simplifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Opencc;

class Simplifier : public Filter, TagMatching {
public:
explicit Simplifier(const Ticket& ticket);
Simplifier(const Ticket& ticket, an<Opencc> opencc);

virtual an<Translation> Apply(an<Translation> translation,
CandidateList* candidates);
Expand All @@ -29,24 +29,30 @@ class Simplifier : public Filter, TagMatching {
protected:
enum TipsLevel { kTipsNone, kTipsChar, kTipsAll };

void Initialize();
void PushBack(const an<Candidate>& original,
CandidateQueue* result,
const string& simplified);

bool initialized_ = false;
the<Opencc> opencc_;
an<Opencc> opencc_;
// settings
TipsLevel tips_level_ = kTipsNone;
string option_name_;
string opencc_config_;
set<string> excluded_types_;
bool show_in_comment_ = false;
bool inherit_comment_ = true;
Projection comment_formatter_;
bool random_ = false;
};

class SimplifierComponent : public Simplifier::Component {
public:
SimplifierComponent();
Simplifier* Create(const Ticket& ticket);

private:
hash_map<string, weak<Opencc>> opencc_map_;
};

} // namespace rime

#endif // RIME_SIMPLIFIER_H_

0 comments on commit f9411ae

Please sign in to comment.