Skip to content

Commit

Permalink
feat(dict): specify vocabulary db name in dict settings
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
lotem committed Mar 24, 2019
1 parent 873719f commit dcdc301
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/rime/dict/dict_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 9 additions & 1 deletion src/rime/dict/dict_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/rime/dict/dict_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions src/rime/dict/entry_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ void EntryCollector::Collect(const vector<string>& 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)
Expand Down
10 changes: 4 additions & 6 deletions src/rime/dict/preset_vocabulary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<DbAccessor> cursor;
Expand Down Expand Up @@ -56,14 +54,14 @@ const TextFormat VocabularyDb::format = {
"Rime vocabulary",
};

string PresetVocabulary::DictFilePath() {
string PresetVocabulary::DictFilePath(const string& vocabulary) {
the<ResourceResolver> 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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/rime/dict/preset_vocabulary.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct VocabularyDb;

class PresetVocabulary {
public:
PresetVocabulary();
explicit PresetVocabulary(const string& vocabulary);
~PresetVocabulary();

// random access
Expand All @@ -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<VocabularyDb> db_;
Expand Down

0 comments on commit dcdc301

Please sign in to comment.