2 * Copyright (C) 2012, 2013
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * A forward allcator for the corrector. This corrector requires a lot
27 * of allocations. This forward allocator combats all those allocations
28 * and speeds us up a little. It also saves us space in a way since each
29 * allocation isn't wasting a little header space for when NOTRACK isn't
32 #define CORRECT_POOLSIZE (128*1024*1024)
34 static unsigned char **correct_pool_data = NULL;
35 static unsigned char *correct_pool_this = NULL;
36 static size_t correct_pool_addr = 0;
38 static GMQCC_INLINE void correct_pool_new(void) {
39 correct_pool_addr = 0;
40 correct_pool_this = (unsigned char *)mem_a(CORRECT_POOLSIZE);
42 vec_push(correct_pool_data, correct_pool_this);
45 static GMQCC_INLINE void *correct_pool_alloc(size_t bytes) {
47 if (correct_pool_addr + bytes >= CORRECT_POOLSIZE)
50 data = correct_pool_this;
51 correct_pool_this += bytes;
52 correct_pool_addr += bytes;
57 static GMQCC_INLINE void correct_pool_delete(void) {
59 for (i = 0; i < vec_size(correct_pool_data); ++i)
60 mem_d(correct_pool_data[i]);
62 correct_pool_data = NULL;
63 correct_pool_this = NULL;
64 correct_pool_addr = 0;
68 static GMQCC_INLINE char *correct_outstr(const char *s) {
69 char *o = util_strdup(s);
70 correct_pool_delete();
74 correct_trie_t* correct_trie_new()
76 correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t));
82 void correct_trie_del_sub(correct_trie_t *t)
85 for (i = 0; i < vec_size(t->entries); ++i)
86 correct_trie_del_sub(&t->entries[i]);
90 void correct_trie_del(correct_trie_t *t)
93 for (i = 0; i < vec_size(t->entries); ++i)
94 correct_trie_del_sub(&t->entries[i]);
99 void* correct_trie_get(const correct_trie_t *t, const char *key)
101 const unsigned char *data = (const unsigned char*)key;
103 unsigned char ch = *data;
104 const size_t vs = vec_size(t->entries);
106 const correct_trie_t *entries = t->entries;
107 for (i = 0; i < vs; ++i) {
108 if (entries[i].ch == ch) {
120 void correct_trie_set(correct_trie_t *t, const char *key, void * const value)
122 const unsigned char *data = (const unsigned char*)key;
124 unsigned char ch = *data;
125 correct_trie_t *entries = t->entries;
126 const size_t vs = vec_size(t->entries);
128 for (i = 0; i < vs; ++i) {
129 if (entries[i].ch == ch) {
135 correct_trie_t *elem = (correct_trie_t*)vec_add(t->entries, 1);
138 elem->entries = NULL;
147 * This is a very clever method for correcting mistakes in QuakeC code
148 * most notably when invalid identifiers are used or inproper assignments;
149 * we can proprly lookup in multiple dictonaries (depening on the rules
150 * of what the task is trying to acomplish) to find the best possible
154 * A little about how it works, and probability theory:
156 * When given an identifier (which we will denote I), we're essentially
157 * just trying to choose the most likely correction for that identifier.
158 * (the actual "correction" can very well be the identifier itself).
159 * There is actually no way to know for sure that certian identifers
160 * such as "lates", need to be corrected to "late" or "latest" or any
161 * other permutations that look lexically the same. This is why we
162 * must advocate the usage of probabilities. This implies that we're
163 * trying to find the correction for C, out of all possible corrections
164 * that maximizes the probability of C for the original identifer I.
166 * Bayes' Therom suggests something of the following:
167 * AC P(I|C) P(C) / P(I)
168 * Since P(I) is the same for every possibly I, we can ignore it giving
171 * This greatly helps visualize how the parts of the expression are performed
172 * there is essentially three, from right to left we perform the following:
174 * 1: P(C), the probability that a proposed correction C will stand on its
175 * own. This is called the language model.
177 * 2: P(I|C), the probability that I would be used, when the programmer
178 * really meant C. This is the error model.
180 * 3: AC, the control mechanisim, which implies the enumeration of all
181 * feasible values of C, and then determine the one that gives the
182 * greatest probability score. Selecting it as the "correction"
185 * The requirement for complex expression involving two models:
187 * In reality the requirement for a more complex expression involving
188 * two seperate models is considerably a waste. But one must recognize
189 * that P(C|I) is already conflating two factors. It's just much simpler
190 * to seperate the two models and deal with them explicitaly. To properly
191 * estimate P(C|I) you have to consider both the probability of C and
192 * probability of the transposition from C to I. It's simply much more
193 * cleaner, and direct to seperate the two factors.
196 /* some hashtable management for dictonaries */
197 static size_t *correct_find(correct_trie_t *table, const char *word) {
198 return (size_t*)correct_trie_get(table, word);
201 static int correct_update(correct_trie_t* *table, const char *word) {
202 size_t *data = correct_find(*table, word);
210 void correct_add(correct_trie_t* table, size_t ***size, const char *ident) {
212 const char *add = ident;
214 if (!correct_update(&table, add)) {
215 data = (size_t*)mem_a(sizeof(size_t));
218 vec_push((*size), data);
219 correct_trie_set(table, add, data);
223 void correct_del(correct_trie_t* dictonary, size_t **data) {
225 const size_t vs = vec_size(data);
226 for (i = 0; i < vs; i++)
230 correct_trie_del(dictonary);
236 #define mem_a(x) correct_alloc((x))
237 #define mem_r(a,b) correct_realloc((a),(b))
238 /* doing this in order to avoid 'unused variable' warnings */
239 #define mem_d(x) ((void)(0 && (x)))
244 * _ is valid in identifiers. I've yet to implement numerics however
245 * because they're only valid after the first character is of a _, or
248 static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
251 * correcting logic for the following forms of transformations:
257 static size_t correct_deletion(const char *ident, char **array, size_t index) {
259 size_t len = strlen(ident);
261 for (itr = 0; itr < len; itr++) {
262 char *a = (char*)correct_pool_alloc(len+1);
263 memcpy(a, ident, itr);
264 memcpy(a + itr, ident + itr + 1, len - itr);
265 array[index + itr] = a;
271 static size_t correct_transposition(const char *ident, char **array, size_t index) {
273 size_t len = strlen(ident);
275 for (itr = 0; itr < len - 1; itr++) {
277 char *a = (char*)correct_pool_alloc(len+1);
278 memcpy(a, ident, len+1);
282 array[index + itr] = a;
288 static size_t correct_alteration(const char *ident, char **array, size_t index) {
292 size_t len = strlen(ident);
294 for (itr = 0, ktr = 0; itr < len; itr++) {
295 for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
296 char *a = (char*)correct_pool_alloc(len+1);
297 memcpy(a, ident, len+1);
298 a[itr] = correct_alpha[jtr];
299 array[index + ktr] = a;
306 static size_t correct_insertion(const char *ident, char **array, size_t index) {
310 const size_t len = strlen(ident);
312 for (itr = 0, ktr = 0; itr <= len; itr++) {
313 for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
314 char *a = (char*)correct_pool_alloc(len+2);
315 memcpy(a, ident, itr);
316 memcpy(a + itr + 1, ident + itr, len - itr + 1);
317 a[itr] = correct_alpha[jtr];
318 array[index + ktr] = a;
325 static GMQCC_INLINE size_t correct_size(const char *ident) {
328 * transposition = len - 1
329 * alteration = len * sizeof(correct_alpha)
330 * insertion = (len + 1) * sizeof(correct_alpha)
333 register size_t len = strlen(ident);
334 return (len) + (len - 1) + (len * (sizeof(correct_alpha)-1)) + ((len + 1) * (sizeof(correct_alpha)-1));
337 static char **correct_edit(const char *ident) {
339 char **find = (char**)correct_pool_alloc(correct_size(ident) * sizeof(char*));
344 next = correct_deletion (ident, find, 0);
345 next += correct_transposition(ident, find, next);
346 next += correct_alteration (ident, find, next);
347 /*****/ correct_insertion (ident, find, next);
353 * We could use a hashtable but the space complexity isn't worth it
354 * since we're only going to determine the "did you mean?" identifier
357 static int correct_exist(char **array, size_t rows, char *ident) {
359 for (itr = 0; itr < rows; itr++)
360 if (!strcmp(array[itr], ident))
366 static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, size_t size) {
367 size_t oldallocated = *allocated;
369 if (size+1 < *allocated)
373 out = correct_pool_alloc(sizeof(*res) * *allocated);
374 memcpy(out, res, sizeof(*res) * oldallocated);
378 static char **correct_known(correct_trie_t* table, char **array, size_t rows, size_t *next) {
384 char **res = correct_pool_alloc(sizeof(char *) * nxt);
387 for (itr = 0, len = 0; itr < rows; itr++) {
388 end = correct_edit(array[itr]);
389 row = correct_size(array[itr]);
391 for (jtr = 0; jtr < row; jtr++) {
392 if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) {
393 res = correct_known_resize(res, &nxt, len+1);
394 res[len++] = end[jtr];
405 static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) {
411 for (itr = 0, top = 0; itr < rows; itr++) {
412 if ((itm = correct_find(table, array[itr])) && (*itm > top)) {
422 * This is the exposed interface:
423 * takes a table for the dictonary a vector of sizes (used for internal
424 * probability calculation, and an identifier to "correct"
426 * the add function works the same. Except the identifier is used to
427 * add to the dictonary.
430 char *correct_str(correct_trie_t* table, const char *ident) {
435 char *found = util_strdup(ident);
442 /* needs to be allocated for free later */
443 if (correct_find(table, ident))
444 return correct_outstr(found);
446 if ((e1rows = correct_size(ident))) {
447 e1 = correct_edit(ident);
449 if ((e1ident = correct_maximum(table, e1, e1rows))) {
450 found = util_strdup(e1ident);
451 return correct_outstr(found);
455 e2 = correct_known(table, e1, e1rows, &e2rows);
456 if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
457 found = util_strdup(e2ident);
460 return correct_outstr(found);