@@ -34,14 +34,20 @@ class Opencc {
34
34
Opencc (const string& config_path) {
35
35
LOG (INFO) << " initilizing opencc: " << config_path;
36
36
opencc::Config config;
37
- converter_ = config.NewFromFile (config_path);
38
- const list<opencc::ConversionPtr> conversions =
39
- converter_->GetConversionChain ()->GetConversions ();
40
- dict_ = conversions.front ()->GetDict ();
37
+ try {
38
+ converter_ = config.NewFromFile (config_path);
39
+ const list<opencc::ConversionPtr> conversions =
40
+ converter_->GetConversionChain ()->GetConversions ();
41
+ dict_ = conversions.front ()->GetDict ();
42
+ }
43
+ catch (...) {
44
+ LOG (ERROR) << " opencc config not found: " << config_path;
45
+ }
41
46
}
42
47
43
48
bool ConvertWord (const string& text,
44
49
vector<string>* forms) {
50
+ if (dict_ == nullptr ) return false ;
45
51
opencc::Optional<const opencc::DictEntry*> item = dict_->Match (text);
46
52
if (item.IsNull ()) {
47
53
// Match not found
@@ -57,6 +63,7 @@ class Opencc {
57
63
58
64
bool RandomConvertText (const string& text,
59
65
string* simplified) {
66
+ if (dict_ == nullptr ) return false ;
60
67
const char *phrase = text.c_str ();
61
68
std::ostringstream buffer;
62
69
for (const char * pstr = phrase; *pstr != ' \0 ' ;) {
@@ -78,6 +85,7 @@ class Opencc {
78
85
79
86
bool ConvertText (const string& text,
80
87
string* simplified) {
88
+ if (converter_ == nullptr ) return false ;
81
89
*simplified = converter_->Convert (text);
82
90
return *simplified != text;
83
91
}
0 commit comments