summaryrefslogtreecommitdiff
blob: 9b4e250021b62c150b7e2712f8ae345e6422acf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <stdlib.h>
#include <string.h>
#include <stdint.h> /* defines SIZE_MAX */

#include "../io/error.h"

#include "knowledge.h"

/** Basic functions of the ZoO_knowledge structure ****************************/

/* XXX: are we as close to immutable as we want to be? */
unsigned int const ZoO_knowledge_punctuation_chars_count = 7;
const ZoO_char const ZoO_knowledge_punctuation_chars[7] =
   {
      '!',
      ',',
      '.',
      ':',
      ';',
      '?',
      '~'
   };

/* XXX: are we as close to immutable as we want to be? */
unsigned int const ZoO_knowledge_forbidden_chars_count = 8;
const ZoO_char const ZoO_knowledge_forbidden_chars[8]=
   {
      '(',
      ')',
      '[',
      ']',
      '{',
      '}',
      '<',
      '>'
   };

/* See "knowledge.h". */
int ZoO_knowledge_find
(
   const struct ZoO_knowledge k [const restrict static 1],
   const ZoO_char word [const restrict static 1],
   ZoO_index result [const restrict static 1]
)
{
   int cmp;
   ZoO_index i, current_min, current_max;

   /* This is a binary search. */

   if (k->words_count < 1)
   {
      *result = 0;

      return -1;
   }

   current_min = 0;

   /* Safe: (>= k->words_count 1) */
   current_max = (k->words_count - 1);

   for (;;)
   {
      /* FIXME: overflow-safe? */
      i = ((current_min + current_max) / 2);

      if (i == k->words_count)
      {
         *result = k->words_count;

         return -1;
      }

      cmp =
         /* XXX: Assumed to be compatible with ZoO_char */
         strcmp
         (
            (char *) word,
            (const char *) k->words[k->sorted_indices[i]].word
         );

      if (cmp > 0)
      {
         if ((current_min > current_max))
         {
            *result = (i + 1);

            return -1;
         }

         /* FIXME: overflow-safe? */
         current_min = (i + 1);
      }
      else if (cmp < 0)
      {
         if ((current_min > current_max) || (i == 0))
         {
            *result = i;

            return -1;
         }

         /* overflow-safe */
         current_max = (i - 1);
      }
      else
      {
         *result = k->sorted_indices[i];

         return 0;
      }
   }
}

static void word_init (struct ZoO_knowledge_word w [const restrict static 1])
{
   w->word_size = 0;
   w->word = (ZoO_char *) NULL;
   w->special = ZoO_WORD_HAS_NO_EFFECT;
   w->occurrences = 1;
   w->forward_links_count  = 0;
   w->backward_links_count = 0;
   w->forward_links_occurrences  = (ZoO_index *) NULL;
   w->backward_links_occurrences = (ZoO_index *) NULL;
   w->forward_links  = (ZoO_index *) NULL;
   w->backward_links = (ZoO_index *) NULL;
}

/*
 * When returning 0:
 *    All punctuation symbols were added to {k}.
 * When returning -1:
 *    The mandatory punctuation symbols have been added to {k}, but some of the
 *    additional ones did not. This does not prevent ZoO from working, but
 *    will result in some punctuation symbols to be handled exactly like
 *    common words.
 * When returning -2:
 *    The mandatory punctuation symbols have not added to {k}. ZoO will not be
 *    able to work.
 */
static int add_punctuation_nodes
(
   struct ZoO_knowledge k [const static 1]
)
{
   int error;
   char w[2];
   ZoO_index i, id;

   if (ZoO_knowledge_learn(k, "START OF LINE", &id) < 0)
   {
      ZoO_S_FATAL("Could not add 'START OF LINE' to knowledge.");

      return -2;
   }

   k->words[id].special = ZoO_WORD_STARTS_SENTENCE;
   k->words[id].occurrences = 0;

   if (ZoO_knowledge_learn(k, "END OF LINE", &id) < 0)
   {
      ZoO_S_FATAL("Could not add 'END OF LINE' to knowledge.");

      return -2;
   }

   k->words[id].special = ZoO_WORD_ENDS_SENTENCE;
   k->words[id].occurrences = 0;

   w[1] = '\0';

   error = 0;

   for (i = 0; i < ZoO_knowledge_punctuation_chars_count; ++i)
   {
      w[0] = ZoO_knowledge_punctuation_chars[i];

      if (ZoO_knowledge_learn(k, w, &id) < 0)
      {
         ZoO_WARNING("Could not add '%s' to knowledge.", w);

         error = -1;
      }
      else
      {
         k->words[id].special = ZoO_WORD_REMOVES_LEFT_SPACE;
         k->words[id].occurrences = 0;
      }
   }

   return error;
}

/* See "knowledge.h" */
int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])
{
   k->words_count = 0;
   k->words = (struct ZoO_knowledge_word *) NULL;
   k->sorted_indices = (ZoO_index *) NULL;

   if (add_punctuation_nodes(k) < -1)
   {
      ZoO_knowledge_finalize(k);

      return -1;
   }

   return 0;
}

/*
 * Frees all the memory used by {w}, but not {w} itself.
 * The values of {w}'s members are set to reflect the changes.
 */
static void finalize_word
(
   struct ZoO_knowledge_word w [const restrict static 1]
)
{
   if (w->word != (ZoO_char *) NULL)
   {
      free((void *) w->word);

      w->word = (ZoO_char *) NULL;
   }

   if (w->forward_links_occurrences != (ZoO_index *) NULL)
   {
      free((void *) w->forward_links_occurrences);

      w->forward_links_occurrences = (ZoO_index *) NULL;
   }

   if (w->backward_links_occurrences != (ZoO_index *) NULL)
   {
      free((void *) w->backward_links_occurrences);

      w->backward_links_occurrences = (ZoO_index *) NULL;
   }

   if (w->forward_links != (ZoO_index *) NULL)
   {
      free((void *) w->forward_links);

      w->forward_links = (ZoO_index *) NULL;
   }

   if (w->backward_links != (ZoO_index *) NULL)
   {
      free((void *) w->backward_links);

      w->backward_links = (ZoO_index *) NULL;
   }

   w->forward_links_count  = 0;
   w->backward_links_count = 0;
}

/* See "knowledge.h" */
void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1])
{
   ZoO_index i;

   for (i = 0; i < k->words_count; ++i)
   {
      finalize_word(k->words + i);
   }

   k->words_count = 0;

   if (k->words != (struct ZoO_knowledge_word *) NULL)
   {
      free((void *) k->words);

      k->words = (struct ZoO_knowledge_word *) NULL;
   }

   if (k->sorted_indices != (ZoO_index *) NULL)
   {
      free((void *) k->sorted_indices);

      k->sorted_indices = (ZoO_index *) NULL;
   }
}

/* See "knowledge.h" */
int ZoO_knowledge_learn
(
   struct ZoO_knowledge k [const static 1],
   const ZoO_char word [const restrict static 1],
   ZoO_index result [const restrict static 1]
)
{
   struct ZoO_knowledge_word * new_wordlist;
   ZoO_index * new_sorted_indices;
   ZoO_index temp;

   /* prevents k [restrict] */
   if (ZoO_knowledge_find(k, word, result) == 0)
   {
      if (k->words[*result].occurrences == ZoO_INDEX_MAX)
      {
         ZoO_WARNING
         (
            "Maximum number of occurrences has been reached for word '"
            ZoO_CHAR_STRING_SYMBOL
            "'.",
            word
         );

         return -1;
      }

      /* overflow-safe: (< k->words[*result].occurrences ZoO_INDEX_MAX) */
      k->words[*result].occurrences += 1;

      return 0;
   }

   if (k->words_count == ZoO_INDEX_MAX)
   {
      ZoO_S_WARNING("Maximum number of words has been reached.");

      return -1;
   }

   new_wordlist =
      (struct ZoO_knowledge_word *) realloc
      (
         (void *) k->words,
         (
            (
               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */
               (size_t) (k->words_count + 1)
            )
            * sizeof(struct ZoO_knowledge_word)
         )
      );

   if (new_wordlist == (struct ZoO_knowledge_word *) NULL)
   {
      ZoO_ERROR
      (
         "Could not learn the word '%s': unable to realloc the word list.",
         word
      );

      return -1;
   }

   k->words = new_wordlist;

   new_sorted_indices =
      (ZoO_index *) realloc
      (
         (void *) k->sorted_indices,
         (
            (
               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */
               (size_t) (k->words_count + 1)
            )
            * sizeof(ZoO_index)
         )
      );

   if (new_sorted_indices == (ZoO_index *) NULL)
   {
      ZoO_ERROR
      (
         "Could not learn the word '"
         ZoO_CHAR_STRING_SYMBOL
         "': unable to realloc the index list.",
         word
      );

      return -1;
   }

   k->sorted_indices = new_sorted_indices;

   /* We can only move indices right of *result if they exist. */
   if (*result < k->words_count)
   {
      /* TODO: check if correct. */
      memmove
      (
         /*
          * Safe:
          *  (->
          *    (and
          *       (== (length k->sorted_indices) (+ k->words_count 1))
          *       (< *result k->words_count)
          *    )
          *    (< (+ *result 1) (length k->sorted_indices))
          * )
          */
         (void *) (k->sorted_indices + *result + 1),
         /* Safe: see above */
         (const void *) (k->sorted_indices + *result),
         (
            (
               /* Safe: (< *result k->words_count) */
               (size_t) (k->words_count - *result)
            )
            * sizeof(ZoO_index)
         )
      );
   }

   temp = *result;

   k->sorted_indices[*result] = k->words_count;

   *result = k->words_count;

   word_init(k->words + *result);

   /* XXX: strlen assumed to work with ZoO_char. */
   k->words[*result].word_size = strlen(word);

   if (k->words[*result].word_size == SIZE_MAX)
   {
      ZoO_S_WARNING
      (
         "Could not learn word that had a size too big to store in a '\\0' "
         "terminated string. Chances are, this is but a symptom of the real "
         "problem."
      );

      return -1;
   }

   /* We also need '\0' */
   k->words[*result].word_size += 1;

   k->words[*result].word =
      (ZoO_char *) calloc
      (
         k->words[*result].word_size,
         sizeof(ZoO_char)
      );

   if (k->words[*result].word == (ZoO_char *) NULL)
   {
      ZoO_S_ERROR
      (
         "Could not learn word due to being unable to allocate the memory to "
         "store it."
      );

      k->words[*result].word_size = 0;

      return -1;
   }

   memcpy(k->words[*result].word, word, k->words[*result].word_size);

   /* Safe: k->words_count < ZoO_INDEX_MAX */
   k->words_count += 1;

   ZoO_DEBUG
   (
      ZoO_DEBUG_LEARNING,
      "Learned word {'%s', id: %u, rank: %u}",
      word,
      *result,
      temp
   );

   return 0;
}