Skip to content

Commit dcdc301

Browse files
committed
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`.
1 parent 873719f commit dcdc301

6 files changed

+21
-13
lines changed

src/rime/dict/dict_compiler.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool DictCompiler::Compile(const string &schema_file) {
7676
cc.ProcessFile(file_name);
7777
}
7878
if (settings.use_preset_vocabulary()) {
79-
cc.ProcessFile(PresetVocabulary::DictFilePath());
79+
cc.ProcessFile(PresetVocabulary::DictFilePath(settings.vocabulary()));
8080
}
8181
dict_file_checksum = cc.Checksum();
8282
}

src/rime/dict/dict_settings.cc

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ string DictSettings::sort_order() {
5050
}
5151

5252
bool DictSettings::use_preset_vocabulary() {
53-
return (*this)["use_preset_vocabulary"].ToBool();
53+
return (*this)["use_preset_vocabulary"].ToBool() ||
54+
(*this)["vocabulary"].IsValue();
55+
}
56+
57+
static const string kDefaultVocabulary = "essay";
58+
59+
string DictSettings::vocabulary() {
60+
string value = (*this)["vocabulary"].ToString();
61+
return !value.empty() ? value : kDefaultVocabulary;
5462
}
5563

5664
bool DictSettings::use_rule_based_encoder() {

src/rime/dict/dict_settings.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DictSettings : public Config {
2121
string dict_version();
2222
string sort_order();
2323
bool use_preset_vocabulary();
24+
string vocabulary();
2425
bool use_rule_based_encoder();
2526
int max_phrase_length();
2627
double min_phrase_weight();

src/rime/dict/entry_collector.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ void EntryCollector::Collect(const vector<string>& dict_files) {
4141
}
4242

4343
void EntryCollector::LoadPresetVocabulary(DictSettings* settings) {
44-
LOG(INFO) << "loading preset vocabulary.";
45-
preset_vocabulary.reset(new PresetVocabulary);
46-
if (preset_vocabulary && settings) {
44+
auto vocabulary = settings->vocabulary();
45+
LOG(INFO) << "loading preset vocabulary: " << vocabulary;
46+
preset_vocabulary.reset(new PresetVocabulary(vocabulary));
47+
if (preset_vocabulary) {
4748
if (settings->max_phrase_length() > 0)
4849
preset_vocabulary->set_max_phrase_length(settings->max_phrase_length());
4950
if (settings->min_phrase_weight() > 0)

src/rime/dict/preset_vocabulary.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ static const ResourceType kVocabularyResourceType = {
1818
"vocabulary", "", ".txt"
1919
};
2020

21-
static const string kDefaultVocabulary = "essay";
22-
2321
struct VocabularyDb : public TextDb {
2422
explicit VocabularyDb(const string& path);
2523
an<DbAccessor> cursor;
@@ -56,14 +54,14 @@ const TextFormat VocabularyDb::format = {
5654
"Rime vocabulary",
5755
};
5856

59-
string PresetVocabulary::DictFilePath() {
57+
string PresetVocabulary::DictFilePath(const string& vocabulary) {
6058
the<ResourceResolver> resource_resolver(
6159
Service::instance().CreateResourceResolver(kVocabularyResourceType));
62-
return resource_resolver->ResolvePath(kDefaultVocabulary).string();
60+
return resource_resolver->ResolvePath(vocabulary).string();
6361
}
6462

65-
PresetVocabulary::PresetVocabulary() {
66-
db_.reset(new VocabularyDb(DictFilePath()));
63+
PresetVocabulary::PresetVocabulary(const string& vocabulary) {
64+
db_.reset(new VocabularyDb(DictFilePath(vocabulary)));
6765
if (db_ && db_->OpenReadOnly()) {
6866
db_->cursor = db_->QueryAll();
6967
}

src/rime/dict/preset_vocabulary.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct VocabularyDb;
1515

1616
class PresetVocabulary {
1717
public:
18-
PresetVocabulary();
18+
explicit PresetVocabulary(const string& vocabulary);
1919
~PresetVocabulary();
2020

2121
// random access
@@ -29,7 +29,7 @@ class PresetVocabulary {
2929
void set_max_phrase_length(int length) { max_phrase_length_ = length; }
3030
void set_min_phrase_weight(double weight) { min_phrase_weight_ = weight; }
3131

32-
static string DictFilePath();
32+
static string DictFilePath(const string& vacabulary);
3333

3434
protected:
3535
the<VocabularyDb> db_;

0 commit comments

Comments
 (0)