Debian Codegraph @

librime-octagram/src/octagram.cc

raw symbols SCIP
1 #include "gram_db.h"
2 #include "gram_encoding.h"
3 #include "octagram.h"
4 #include <algorithm>
5 #include <rime/config.h>
6 #include <rime/resource.h>
7 #include <rime/service.h>
8 #include <utf8.h>
9
10 namespace rime {
11
12 struct GrammarConfig {
13 int collocation_max_length = 4;
14 int collocation_min_length = 3;
15 double collocation_penalty = -12;
16 double non_collocation_penalty = -12;
17 double weak_collocation_penalty = -24;
18 double rear_penalty = -18;
19 };
20
21 const ResourceType kGramDbType = {"gram_db", "", ".gram"};
22 const string kGrammarDefaultLanguage = "zh-hant";
23
24 Octagram::Octagram(Config* config, OctagramComponent* component)
25 : config_(std::make_unique<GrammarConfig>()) {
26 string language;
27 if (config) {
28 if (config->GetString("grammar/language", &language)) {
29 LOG(INFO) << "use grammar: " << language;
30 } else {
31 return;
32 }
33 config->GetInt("grammar/collocation_max_length",
34 &config_->collocation_max_length);
35 config->GetInt("grammar/collocation_min_length",
36 &config_->collocation_min_length);
37 config->GetDouble("grammar/collocation_penalty",
38 &config_->collocation_penalty);
39 config->GetDouble("grammar/non_collocation_penalty",
40 &config_->non_collocation_penalty);
41 config->GetDouble("grammar/weak_collocation_penalty",
42 &config_->weak_collocation_penalty);
43 config->GetDouble("grammar/rear_penalty",
44 &config_->rear_penalty);
45 }
46 if (!language.empty()) {
47 db_ = component->GetDb(language);
48 }
49 }
50
51 Octagram::~Octagram() {}
52
53 inline static double scale_value(int value) {
54 return value >= 0 ? double(value) / GramDb::kValueScale : -1;
55 }
56
57 inline static bool update_result(double& result, double new_value) {
58 if (new_value > result) {
59 result = new_value;
60 return true;
61 }
62 return false;
63 }
64
65 inline static const char* str_begin(const string& str) {
66 return str.c_str();
67 }
68
69 inline static const char* str_end(const string& str) {
70 return str.c_str() + str.length();
71 }
72
73 inline static const char* last_n_unicode(const string& str,
74 int max,
75 int& out_count) {
76 const char* begin = str_begin(str);
77 const char* p = str_end(str);
78 out_count = 0;
79 while (p != begin && out_count < max) {
80 utf8::unchecked::prior(p);
81 ++out_count;
82 }
83 return p;
84 }
85
86 inline static const char* first_n_unicode(const string& str,
87 int max,
88 int& out_count) {
89 const char* p = str_begin(str);
90 const char* end = str_end(str);
91 out_count = 0;
92 while (p != end && out_count < max) {
93 utf8::unchecked::next(p);
94 ++out_count;
95 }
96 return p;
97 }
98
99 inline static bool matches_whole_query(const char* context_ptr,
100 const string& context_query,
101 size_t match_length,
102 const string& word_query) {
103 return context_ptr == str_begin(context_query) &&
104 match_length == word_query.length();
105 }
106
107 double Octagram::Query(const string& context,
108 const string& word,
109 bool is_rear) {
110 if (!db_ || context.empty()) {
111 return config_->non_collocation_penalty;
112 }
113 double result = config_->non_collocation_penalty;
114 GramDb::Match matches[GramDb::kMaxResults];
115 int n = (std::min)(grammar::kMaxEncodedUnicode,
116 config_->collocation_max_length - 1);
117 int context_len = 0;
118 string context_query = grammar::encode(
119 last_n_unicode(context, n, context_len),
120 str_end(context));
121 int word_query_len = 0;
122 string word_query = grammar::encode(
123 str_begin(word),
124 first_n_unicode(word, n, word_query_len));
125 for (const char* context_ptr = str_begin(context_query);
126 context_len > 0;
127 --context_len, context_ptr = grammar::next_unicode(context_ptr)) {
128 int num_results = db_->Lookup(context_ptr, word_query, matches);
129 DLOG(INFO) << "Lookup(" << context_ptr << " + " << word_query << ") returns "
130 << num_results << " results";
131 for (auto i = 0; i < num_results; ++i) {
132 const auto& match(matches[i]);
133 const int match_len = grammar::unicode_length(word_query, match.length);
134 DLOG(INFO) << "match[" << match.length << "] = "
135 << scale_value(match.value);
136 const int collocation_len = context_len + match_len;
137 if (update_result(result,
138 scale_value(match.value) +
139 (collocation_len >= config_->collocation_min_length ||
140 matches_whole_query(context_ptr, context_query,
141 match.length, word_query)
142 ? config_->collocation_penalty
143 : config_->weak_collocation_penalty))) {
144 DLOG(INFO) << "update: " << context << "[" << context_len << "] + "
145 << word << "[" << match_len << "] = " << result;
146 }
147 }
148 }
149 if (is_rear) {
150 int word_len = utf8::unchecked::distance(word.c_str(),
151 word.c_str() + word.length());
152 if (word_query_len == word_len &&
153 db_->Lookup(word_query, "$", matches) > 0 &&
154 update_result(result,
155 scale_value(matches[0].value) + config_->rear_penalty)) {
156 DLOG(INFO) << "update: " << word << "$ / " << result;
157 }
158 }
159 DLOG(INFO) << "context = " << context << ", word = " << word
160 << " / " << result;
161 return result;
162 }
163
164 OctagramComponent::OctagramComponent() {}
165
166 OctagramComponent::~OctagramComponent() {}
167
168 Octagram* OctagramComponent::Create(Config* config) {
169 return new Octagram(config, this);
170 }
171
172 GramDb* OctagramComponent::GetDb(const string& language) {
173 auto& loaded = db_by_language_[language];
174 if (!loaded) {
175 the<ResourceResolver> resolver(
176 Service::instance().CreateResourceResolver(kGramDbType));
177 the<GramDb> db =
178 std::make_unique<GramDb>(resolver->ResolvePath(language));
179 if (!db->Load()) {
180 LOG(ERROR) << "failed to load grammar database: " << language;
181 return nullptr;
182 }
183 loaded = std::move(db);
184 }
185 return loaded.get();
186 }
187
188 } // namespace rime