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 * This is a very clever method for correcting mistakes in QuakeC code
27 * most notably when invalid identifiers are used or inproper assignments;
28 * we can proprly lookup in multiple dictonaries (depening on the rules
29 * of what the task is trying to acomplish) to find the best possible
33 * A little about how it works, and probability theory:
35 * When given an identifier (which we will denote I), we're essentially
36 * just trying to choose the most likely correction for that identifier.
37 * (the actual "correction" can very well be the identifier itself).
38 * There is actually no way to know for sure that certian identifers
39 * such as "lates", need to be corrected to "late" or "latest" or any
40 * other permutations that look lexically the same. This is why we
41 * must advocate the usage of probabilities. This implies that we're
42 * trying to find the correction for C, out of all possible corrections
43 * that maximizes the probability of C for the original identifer I.
45 * Bayes' Therom suggests something of the following:
46 * AC P(I|C) P(C) / P(I)
47 * Since P(I) is the same for every possibly I, we can ignore it giving
50 * This greatly helps visualize how the parts of the expression are performed
51 * there is essentially three, from right to left we perform the following:
53 * 1: P(C), the probability that a proposed correction C will stand on its
54 * own. This is called the language model.
56 * 2: P(I|C), the probability that I would be used, when the programmer
57 * really meant C. This is the error model.
59 * 3: AC, the control mechanisim, which implies the enumeration of all
60 * feasible values of C, and then determine the one that gives the
61 * greatest probability score. Selecting it as the "correction"
64 * The requirement for complex expression involving two models:
66 * In reality the requirement for a more complex expression involving
67 * two seperate models is considerably a waste. But one must recognize
68 * that P(C|I) is already conflating two factors. It's just much simpler
69 * to seperate the two models and deal with them explicitaly. To properly
70 * estimate P(C|I) you have to consider both the probability of C and
71 * probability of the transposition from C to I. It's simply much more
72 * cleaner, and direct to seperate the two factors.
75 /* some hashtable management for dictonaries */
76 static size_t *correct_find(ht table, const char *word) {
77 return (size_t*)util_htget(table, word);
80 static int correct_update(ht *table, const char *word) {
81 size_t *data = correct_find(*table, word);
91 * _ is valid in identifiers. I've yet to implement numerics however
92 * because they're only valid after the first character is of a _, or
95 static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
97 static char *correct_strndup(const char *src, size_t n) {
99 size_t len = strlen(src);
104 if (!(ret = (char*)mem_a(len + 1)))
108 return (char*)memcpy(ret, src, len);
111 static char *correct_concat(char *str1, char *str2, bool next) {
121 str1 = mem_r (str1, strlen(str1) + strlen(str2) + 1);
122 ret = strcat(str1, str2);
131 * correcting logic for the following forms of transformations:
137 static size_t correct_deletion(const char *ident, char **array, size_t index) {
139 size_t len = strlen(ident);
141 for (itr = 0; itr < len; itr++) {
142 array[index + itr] = correct_concat (
143 correct_strndup (ident, itr),
144 correct_strndup (ident+itr+1, len-(itr+1)),
152 static size_t correct_transposition(const char *ident, char **array, size_t index) {
154 size_t len = strlen(ident);
156 for (itr = 0; itr < len - 1; itr++) {
157 array[index + itr] = correct_concat (
159 correct_strndup(ident, itr),
160 correct_strndup(ident+itr+1, 1),
164 correct_strndup(ident+itr, 1),
165 correct_strndup(ident+itr+2, len-(itr+2)),
175 static size_t correct_alteration(const char *ident, char **array, size_t index) {
179 size_t len = strlen(ident);
180 char cct[2] = { 0, 0 }; /* char code table, for concatenation */
182 for (itr = 0, ktr = 0; itr < len; itr++) {
183 for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
184 *cct = correct_alpha[jtr];
185 array[index + ktr] = correct_concat (
187 correct_strndup(ident, itr),
203 static size_t correct_insertion(const char *ident, char **array, size_t index) {
207 size_t len = strlen(ident);
208 char cct[2] = { 0, 0 }; /* char code table, for concatenation */
210 for (itr = 0, ktr = 0; itr <= len; itr++) {
211 for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
212 *cct = correct_alpha[jtr];
213 array[index + ktr] = correct_concat (
215 correct_strndup (ident, itr),
231 static GMQCC_INLINE size_t correct_size(const char *ident) {
234 * transposition = len - 1
235 * alteration = len * sizeof(correct_alpha)
236 * insertion = (len + 1) * sizeof(correct_alpha)
239 register size_t len = strlen(ident);
240 return (len) + (len - 1) + (len * sizeof(correct_alpha)) + ((len + 1) * sizeof(correct_alpha));
243 static char **correct_edit(const char *ident) {
245 char **find = (char**)mem_a(correct_size(ident) * sizeof(char*));
250 next = correct_deletion (ident, find, 0);
251 next += correct_transposition(ident, find, next);
252 next += correct_alteration (ident, find, next);
253 /*****/ correct_insertion (ident, find, next);
259 * We could use a hashtable but the space complexity isn't worth it
260 * since we're only going to determine the "did you mean?" identifier
263 static int correct_exist(char **array, size_t rows, char *ident) {
265 for (itr = 0; itr < rows; itr++)
266 if (!strcmp(array[itr], ident))
272 static char **correct_known(ht table, char **array, size_t rows, size_t *next) {
280 for (itr = 0, len = 0; itr < rows; itr++) {
281 end = correct_edit(array[itr]);
282 row = correct_size(array[itr]);
284 for (jtr = 0; jtr < row; jtr++) {
285 if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) {
286 res = mem_r(res, sizeof(char*) * (len + 1));
287 res[len++] = end[jtr];
300 static char *correct_maximum(ht table, char **array, size_t rows) {
306 for (itr = 0, top = 0; itr < rows; itr++) {
307 if ((itm = correct_find(table, array[itr])) && (*itm > top)) {
316 static void correct_cleanup(char **array, size_t rows) {
318 for (itr = 0; itr < rows; itr++)
325 * This is the exposed interface:
326 * takes a table for the dictonary a vector of sizes (used for internal
327 * probability calculation, and an identifier to "correct"
329 * the add function works the same. Except the identifier is used to
330 * add to the dictonary.
332 void correct_add(ht table, size_t ***size, const char *ident) {
334 const char *add = ident;
336 if (!correct_update(&table, add)) {
337 data = (size_t*)mem_a(sizeof(size_t));
340 vec_push((*size), data);
341 util_htset(table, add, data);
345 char *correct_str(ht table, const char *ident) {
350 char *found = util_strdup(ident);
355 /* needs to be allocated for free later */
356 if (correct_find(table, ident))
359 if ((e1rows = correct_size(ident))) {
360 e1 = correct_edit(ident);
362 if ((e1ident = correct_maximum(table, e1, e1rows))) {
364 found = util_strdup(e1ident);
365 correct_cleanup(e1, e1rows);
370 e2 = correct_known(table, e1, e1rows, &e2rows);
371 if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
373 found = util_strdup(e2ident);
376 correct_cleanup(e1, e1rows);
377 correct_cleanup(e2, e2rows);
382 void correct_del(ht dictonary, size_t **data) {
384 for (i = 0; i < vec_size(data); i++)
388 util_htdel(dictonary);