Skip to content

Commit 744cec6

Browse files
committed
fix(simplifier): Lazily initialize Opencc
1 parent 9e5fece commit 744cec6

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/rime/gear/simplifier.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,27 @@ namespace rime {
3131

3232
class Opencc {
3333
public:
34-
Opencc(const path& config_path) {
35-
LOG(INFO) << "initializing opencc: " << config_path;
34+
Opencc(const path& config_path) : initialized_(false), config_path_(config_path) {}
35+
36+
void Initialize() {
37+
if (initialized_)
38+
return;
39+
initialized_ = true;
3640
opencc::Config config;
3741
try {
3842
// opencc accepts file path encoded in UTF-8.
39-
converter_ = config.NewFromFile(config_path.u8string());
43+
converter_ = config.NewFromFile(config_path_.u8string());
4044

4145
const list<opencc::ConversionPtr> conversions =
4246
converter_->GetConversionChain()->GetConversions();
4347
dict_ = conversions.front()->GetDict();
4448
} catch (...) {
45-
LOG(ERROR) << "opencc config not found: " << config_path;
49+
LOG(ERROR) << "opencc config not found: " << config_path_;
4650
}
4751
}
4852

4953
bool ConvertWord(const string& text, vector<string>* forms) {
54+
Initialize();
5055
if (converter_ == nullptr) {
5156
return false;
5257
}
@@ -111,6 +116,7 @@ class Opencc {
111116
}
112117

113118
bool RandomConvertText(const string& text, string* simplified) {
119+
Initialize();
114120
if (dict_ == nullptr)
115121
return false;
116122
const list<opencc::ConversionPtr> conversions =
@@ -143,13 +149,16 @@ class Opencc {
143149
}
144150

145151
bool ConvertText(const string& text, string* simplified) {
152+
Initialize();
146153
if (converter_ == nullptr)
147154
return false;
148155
*simplified = converter_->Convert(text);
149156
return *simplified != text;
150157
}
151158

152159
private:
160+
bool initialized_;
161+
path config_path_;
153162
opencc::ConverterPtr converter_;
154163
opencc::DictPtr dict_;
155164
};

0 commit comments

Comments
 (0)