From dcdc30157800a111bec5c84b078d60bc13cf7136 Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Sun, 24 Mar 2019 19:56:45 +0800 Subject: [PATCH] feat(dict): specify vocabulary db name in dict settings the new settings item `vocabulary: essay` is equivalent to `use_preset_vocabulary: true`. this commit enables changing `essay` to a user specified vocabulary db eg. `essay-zh-hans`. --- src/rime/dict/dict_compiler.cc | 2 +- src/rime/dict/dict_settings.cc | 10 +++++++++- src/rime/dict/dict_settings.h | 1 + src/rime/dict/entry_collector.cc | 7 ++++--- src/rime/dict/preset_vocabulary.cc | 10 ++++------ src/rime/dict/preset_vocabulary.h | 4 ++-- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/rime/dict/dict_compiler.cc b/src/rime/dict/dict_compiler.cc index a4dbcd620..1ec049503 100644 --- a/src/rime/dict/dict_compiler.cc +++ b/src/rime/dict/dict_compiler.cc @@ -76,7 +76,7 @@ bool DictCompiler::Compile(const string &schema_file) { cc.ProcessFile(file_name); } if (settings.use_preset_vocabulary()) { - cc.ProcessFile(PresetVocabulary::DictFilePath()); + cc.ProcessFile(PresetVocabulary::DictFilePath(settings.vocabulary())); } dict_file_checksum = cc.Checksum(); } diff --git a/src/rime/dict/dict_settings.cc b/src/rime/dict/dict_settings.cc index 51ffa56f8..4464247b8 100644 --- a/src/rime/dict/dict_settings.cc +++ b/src/rime/dict/dict_settings.cc @@ -50,7 +50,15 @@ string DictSettings::sort_order() { } bool DictSettings::use_preset_vocabulary() { - return (*this)["use_preset_vocabulary"].ToBool(); + return (*this)["use_preset_vocabulary"].ToBool() || + (*this)["vocabulary"].IsValue(); +} + +static const string kDefaultVocabulary = "essay"; + +string DictSettings::vocabulary() { + string value = (*this)["vocabulary"].ToString(); + return !value.empty() ? value : kDefaultVocabulary; } bool DictSettings::use_rule_based_encoder() { diff --git a/src/rime/dict/dict_settings.h b/src/rime/dict/dict_settings.h index 1818a6003..48973cbd9 100644 --- a/src/rime/dict/dict_settings.h +++ b/src/rime/dict/dict_settings.h @@ -21,6 +21,7 @@ class DictSettings : public Config { string dict_version(); string sort_order(); bool use_preset_vocabulary(); + string vocabulary(); bool use_rule_based_encoder(); int max_phrase_length(); double min_phrase_weight(); diff --git a/src/rime/dict/entry_collector.cc b/src/rime/dict/entry_collector.cc index e7d1ae5b6..5a62802e2 100644 --- a/src/rime/dict/entry_collector.cc +++ b/src/rime/dict/entry_collector.cc @@ -41,9 +41,10 @@ void EntryCollector::Collect(const vector& dict_files) { } void EntryCollector::LoadPresetVocabulary(DictSettings* settings) { - LOG(INFO) << "loading preset vocabulary."; - preset_vocabulary.reset(new PresetVocabulary); - if (preset_vocabulary && settings) { + auto vocabulary = settings->vocabulary(); + LOG(INFO) << "loading preset vocabulary: " << vocabulary; + preset_vocabulary.reset(new PresetVocabulary(vocabulary)); + if (preset_vocabulary) { if (settings->max_phrase_length() > 0) preset_vocabulary->set_max_phrase_length(settings->max_phrase_length()); if (settings->min_phrase_weight() > 0) diff --git a/src/rime/dict/preset_vocabulary.cc b/src/rime/dict/preset_vocabulary.cc index f1d6c759b..3d15fcf2f 100644 --- a/src/rime/dict/preset_vocabulary.cc +++ b/src/rime/dict/preset_vocabulary.cc @@ -18,8 +18,6 @@ static const ResourceType kVocabularyResourceType = { "vocabulary", "", ".txt" }; -static const string kDefaultVocabulary = "essay"; - struct VocabularyDb : public TextDb { explicit VocabularyDb(const string& path); an cursor; @@ -56,14 +54,14 @@ const TextFormat VocabularyDb::format = { "Rime vocabulary", }; -string PresetVocabulary::DictFilePath() { +string PresetVocabulary::DictFilePath(const string& vocabulary) { the resource_resolver( Service::instance().CreateResourceResolver(kVocabularyResourceType)); - return resource_resolver->ResolvePath(kDefaultVocabulary).string(); + return resource_resolver->ResolvePath(vocabulary).string(); } -PresetVocabulary::PresetVocabulary() { - db_.reset(new VocabularyDb(DictFilePath())); +PresetVocabulary::PresetVocabulary(const string& vocabulary) { + db_.reset(new VocabularyDb(DictFilePath(vocabulary))); if (db_ && db_->OpenReadOnly()) { db_->cursor = db_->QueryAll(); } diff --git a/src/rime/dict/preset_vocabulary.h b/src/rime/dict/preset_vocabulary.h index 456dbfa13..ba2634eb8 100644 --- a/src/rime/dict/preset_vocabulary.h +++ b/src/rime/dict/preset_vocabulary.h @@ -15,7 +15,7 @@ struct VocabularyDb; class PresetVocabulary { public: - PresetVocabulary(); + explicit PresetVocabulary(const string& vocabulary); ~PresetVocabulary(); // random access @@ -29,7 +29,7 @@ class PresetVocabulary { void set_max_phrase_length(int length) { max_phrase_length_ = length; } void set_min_phrase_weight(double weight) { min_phrase_weight_ = weight; } - static string DictFilePath(); + static string DictFilePath(const string& vacabulary); protected: the db_;