]> git.xonotic.org Git - xonotic/gmqcc.git/blob - correct.c
Get rid of correct_strndup and correct_concat altogether, reduces each generated...
[xonotic/gmqcc.git] / correct.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24
25 /*
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
30  * match.
31  *
32  *
33  * A little about how it works, and probability theory:
34  *
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.
44  *
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
48  *      AC P(I|C) P(C)
49  *
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:
52  *
53  *  1: P(C), the probability that a proposed correction C will stand on its
54  *     own.  This is called the language model.
55  *
56  *  2: P(I|C), the probability that I would be used, when the programmer
57  *     really meant C.  This is the error model.
58  *
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"
62  *   
63  *
64  * The requirement for complex expression involving two models:
65  * 
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.
73  */
74
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);
78 }
79
80 static int correct_update(ht *table, const char *word) {
81     size_t *data = correct_find(*table, word);
82     if (!data)
83         return 0;
84
85     (*data)++;
86     return 1;
87 }
88
89
90 /*
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
93  * alpha character.
94  */
95 static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
96
97 /*
98  * correcting logic for the following forms of transformations:
99  *  1) deletion
100  *  2) transposition
101  *  3) alteration
102  *  4) insertion
103  */
104 static size_t correct_deletion(const char *ident, char **array, size_t index) {
105     size_t itr;
106     size_t len = strlen(ident);
107
108     for (itr = 0; itr < len; itr++) {
109         char *a = (char*)mem_a(len+1);
110         memcpy(a, ident, itr);
111         memcpy(a + itr, ident + itr + 1, len - itr);
112         array[index + itr] = a;
113     }
114
115     return itr;
116 }
117
118 static size_t correct_transposition(const char *ident, char **array, size_t index) {
119     size_t itr;
120     size_t len = strlen(ident);
121
122     for (itr = 0; itr < len - 1; itr++) {
123         char  tmp;
124         char *a = (char*)mem_a(len+1);
125         memcpy(a, ident, len+1);
126         tmp      = a[itr];
127         a[itr  ] = a[itr+1];
128         a[itr+1] = tmp;
129         array[index + itr] = a;
130     }
131
132     return itr;
133 }
134
135 static size_t correct_alteration(const char *ident, char **array, size_t index) {
136     size_t itr;
137     size_t jtr;
138     size_t ktr;
139     size_t len    = strlen(ident);
140
141     for (itr = 0, ktr = 0; itr < len; itr++) {
142         for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
143             char *a = (char*)mem_a(len+1);
144             memcpy(a, ident, len+1);
145             a[itr] = correct_alpha[jtr];
146             array[index + ktr] = a;
147         }
148     }
149
150     return ktr;
151 }
152
153 static size_t correct_insertion(const char *ident, char **array, size_t index) {
154     size_t itr;
155     size_t jtr;
156     size_t ktr;
157     size_t len    = strlen(ident);
158
159     for (itr = 0, ktr = 0; itr <= len; itr++) {
160         for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
161             char *a = (char*)mem_a(len+2);
162             memcpy(a, ident, itr);
163             a[itr] = correct_alpha[jtr];
164             memcpy(a + itr + 1, ident + itr, len - itr + 1);
165             array[index + ktr] = a;
166         }
167     }
168
169     return ktr;
170 }
171
172 static GMQCC_INLINE size_t correct_size(const char *ident) {
173     /*
174      * deletion      = len
175      * transposition = len - 1
176      * alteration    = len * sizeof(correct_alpha)
177      * insertion     = (len + 1) * sizeof(correct_alpha)
178      */   
179
180     register size_t len = strlen(ident);
181     return (len) + (len - 1) + (len * (sizeof(correct_alpha)-1)) + ((len + 1) * (sizeof(correct_alpha)-1));
182 }
183
184 static char **correct_edit(const char *ident) {
185     size_t next;
186     char **find = (char**)mem_a(correct_size(ident) * sizeof(char*));
187
188     if (!find)
189         return NULL;
190
191     next  = correct_deletion     (ident, find, 0);
192     next += correct_transposition(ident, find, next);
193     next += correct_alteration   (ident, find, next);
194     /*****/ correct_insertion    (ident, find, next);
195
196     return find;
197 }
198
199 /*
200  * We could use a hashtable but the space complexity isn't worth it
201  * since we're only going to determine the "did you mean?" identifier
202  * on error.
203  */   
204 static int correct_exist(char **array, size_t rows, char *ident) {
205     size_t itr;
206     for (itr = 0; itr < rows; itr++)
207         if (!strcmp(array[itr], ident))
208             return 1;
209
210     return 0;
211 }
212
213 static char **correct_known(ht table, char **array, size_t rows, size_t *next) {
214     size_t itr;
215     size_t jtr;
216     size_t len;
217     size_t row;
218     char **res = NULL;
219     char **end;
220
221     for (itr = 0, len = 0; itr < rows; itr++) {
222         end = correct_edit(array[itr]);
223         row = correct_size(array[itr]);
224
225         for (jtr = 0; jtr < row; jtr++) {
226             if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) {
227                 res        = mem_r(res, sizeof(char*) * (len + 1));
228                 res[len++] = end[jtr];
229             } else {
230                 mem_d(end[jtr]);
231             }
232         }
233
234         mem_d(end);
235     }
236
237     *next = len;
238     return res;
239 }
240
241 static char *correct_maximum(ht table, char **array, size_t rows) {
242     char   *str  = NULL;
243     size_t *itm  = NULL;
244     size_t  itr;
245     size_t  top;
246
247     for (itr = 0, top = 0; itr < rows; itr++) {
248         if ((itm = correct_find(table, array[itr])) && (*itm > top)) {
249             top = *itm;
250             str = array[itr];
251         }
252     }
253
254     return str;
255 }
256
257 static void correct_cleanup(char **array, size_t rows) {
258     size_t itr;
259     for (itr = 0; itr < rows; itr++)
260         mem_d(array[itr]);
261
262     mem_d(array);
263 }
264
265 /*
266  * This is the exposed interface:
267  * takes a table for the dictonary a vector of sizes (used for internal
268  * probability calculation, and an identifier to "correct"
269  *
270  * the add function works the same.  Except the identifier is used to
271  * add to the dictonary.  
272  */   
273 void correct_add(ht table, size_t ***size, const char *ident) {
274     size_t     *data = NULL;
275     const char *add  = ident;
276     
277     if (!correct_update(&table, add)) {
278         data  = (size_t*)mem_a(sizeof(size_t));
279         *data = 1;
280
281         vec_push((*size), data);
282         util_htset(table, add, data);
283     }
284 }
285
286 char *correct_str(ht table, const char *ident) {
287     char **e1;
288     char **e2;
289     char  *e1ident;
290     char  *e2ident;
291     char  *found = util_strdup(ident);
292
293     size_t e1rows = 0;
294     size_t e2rows = 0;
295
296     /* needs to be allocated for free later */
297     if (correct_find(table, ident))
298         return found;
299
300     if ((e1rows = correct_size(ident))) {
301         e1      = correct_edit(ident);
302
303         if ((e1ident = correct_maximum(table, e1, e1rows))) {
304             mem_d(found);
305             found = util_strdup(e1ident);
306             correct_cleanup(e1, e1rows);
307             return found;
308         }
309     }
310
311     e2 = correct_known(table, e1, e1rows, &e2rows);
312     if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
313         mem_d(found);
314         found = util_strdup(e2ident);
315     }
316     
317     correct_cleanup(e1, e1rows);
318     correct_cleanup(e2, e2rows);
319     
320     return found;
321 }
322
323 void correct_del(ht dictonary, size_t **data) {
324     size_t i;
325     for (i = 0; i < vec_size(data); i++)
326         mem_d(data[i]);
327
328     vec_free(data);
329     util_htdel(dictonary);
330 }