1 // 2 // Copyright RIME Developers 3 // Distributed under the BSD License 4 // 5 // 2012-11-11 GONG Chen <chen.sst@gmail.com> 6 // 7 #include <sstream> 8 #include <boost/algorithm/string.hpp> 9 #include <rime/dict/dict_settings.h> 10 11 namespace rime { 12 DictSettings()13DictSettings::DictSettings() { 14 } 15 LoadDictHeader(std::istream & stream)16bool DictSettings::LoadDictHeader(std::istream& stream) { 17 if (!stream.good()) { 18 LOG(ERROR) << "failed to load dict header from stream."; 19 return false; 20 } 21 std::stringstream header; 22 string line; 23 while (getline(stream, line)) { 24 boost::algorithm::trim_right(line); 25 header << line << std::endl; 26 if (line == "...") { // yaml doc ending 27 break; 28 } 29 } 30 if (!LoadFromStream(header)) { 31 return false; 32 } 33 if ((*this)["name"].IsNull() || (*this)["version"].IsNull()) { 34 LOG(ERROR) << "incomplete dict header."; 35 return false; 36 } 37 return true; 38 } 39 empty()40bool DictSettings::empty() { 41 return (*this)["name"].IsNull(); 42 } 43 dict_name()44string DictSettings::dict_name() { 45 return (*this)["name"].ToString(); 46 } 47 dict_version()48string DictSettings::dict_version() { 49 return (*this)["version"].ToString(); 50 } 51 sort_order()52string DictSettings::sort_order() { 53 return (*this)["sort"].ToString(); 54 } 55 use_preset_vocabulary()56bool DictSettings::use_preset_vocabulary() { 57 return (*this)["use_preset_vocabulary"].ToBool() || 58 (*this)["vocabulary"].IsValue(); 59 } 60 61 static const string kDefaultVocabulary = "essay"; 62 vocabulary()63string DictSettings::vocabulary() { 64 string value = (*this)["vocabulary"].ToString(); 65 return !value.empty() ? value : kDefaultVocabulary; 66 } 67 use_rule_based_encoder()68bool DictSettings::use_rule_based_encoder() { 69 return (*this)["encoder"]["rules"].IsList(); 70 } 71 max_phrase_length()72int DictSettings::max_phrase_length() { 73 return (*this)["max_phrase_length"].ToInt(); 74 } 75 min_phrase_weight()76double DictSettings::min_phrase_weight() { 77 return (*this)["min_phrase_weight"].ToDouble(); 78 } 79 GetTables()80an<ConfigList> DictSettings::GetTables() { 81 if (empty()) 82 return nullptr; 83 auto tables = New<ConfigList>(); 84 tables->Append((*this)["name"]); 85 auto imports = (*this)["import_tables"].AsList(); 86 for (auto it = imports->begin(); it != imports->end(); ++it) { 87 if (!Is<ConfigValue>(*it)) 88 continue; 89 string table = As<ConfigValue>(*it)->str(); 90 if (table == dict_name()) { 91 LOG(WARNING) << "cannot import '" << table << "' from itself."; 92 continue; 93 } 94 tables->Append(*it); 95 } 96 return tables; 97 } 98 GetColumnIndex(const string & column_label)99int DictSettings::GetColumnIndex(const string& column_label) { 100 if ((*this)["columns"].IsNull()) { 101 // default 102 if (column_label == "text") return 0; 103 if (column_label == "code") return 1; 104 if (column_label == "weight") return 2; 105 return -1; 106 } 107 auto columns = (*this)["columns"].AsList(); 108 int index = 0; 109 for (auto it = columns->begin(); it != columns->end(); ++it, ++index) { 110 if (Is<ConfigValue>(*it) && As<ConfigValue>(*it)->str() == column_label) { 111 return index; 112 } 113 } 114 return -1; 115 } 116 117 } // namespace rime 118