summaryrefslogtreecommitdiff
blob: 28f4dd4b59c7635f0c03bfa88d147a573ed53c7b (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
#include <stdlib.h>
#include <string.h>

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

#include "knowledge.h"

/** Functions to assimilate sentences using a ZoO_knowledge structure *********/

static int link_to
(
   ZoO_index links_count [const restrict static 1],
   ZoO_index * links_occurrences [const restrict static 1],
   ZoO_index * links [const restrict static 1],
   ZoO_index const target
)
{
   ZoO_index i, * new_p;

   for (i = 0; i < *links_count; ++i)
   {
      if ((*links)[i] == target)
      {
         if ((*links_occurrences)[i] == ZoO_INDEX_MAX)
         {
            ZoO_S_WARNING
            (
               "Maximum link occurrences count has been reached."
            );

            return -1;
         }

         (*links_occurrences)[i] += 1;

         return 0;
      }
   }

   if (*links_count == ZoO_INDEX_MAX)
   {
      ZoO_S_WARNING("Maximum links count has been reached.");

      return -1;
   }

   new_p =
      (ZoO_index *) realloc
      (
         *links_occurrences,
         (
            (
               /* Safe: *links_count < ZoO_INDEX_MAX */
               (size_t) (*links_count + 1)
            )
            * sizeof(ZoO_index)
         )
      );

   if (new_p == (ZoO_index *) NULL)
   {
      ZoO_S_ERROR("Could not reallocate a link occurrences list.");

      return -1;
   }

   new_p[*links_count] = 1;

   *links_occurrences = new_p;

   new_p =
      (ZoO_index *) realloc
      (
         *links,
         (
            (
               /* Safe: *links_count < ZoO_INDEX_MAX */
               (size_t) (*links_count + 1)
            ) * sizeof(ZoO_index)
         )
      );

   if (new_p == (ZoO_index *) NULL)
   {
      ZoO_S_ERROR("Could not reallocate a link list.");

      return -1;
   }

   new_p[*links_count] = target;

   *links = new_p;

   *links_count += 1;

   return 0;
}

static int link_words
(
   struct ZoO_knowledge k [const restrict static 1],
   ZoO_index const a,
   ZoO_index const b
)
{
   int error;

   error =
      link_to
      (
         &(k->words[a].forward_links_count),
         &(k->words[a].forward_links_occurrences),
         &(k->words[a].forward_links),
         b
      );

   error =
      (
         link_to
         (
            &(k->words[b].backward_links_count),
            &(k->words[b].backward_links_occurrences),
            &(k->words[b].backward_links),
            a
         )
         | error
      );

   return error;
}

int ZoO_knowledge_assimilate
(
   struct ZoO_knowledge k [const static 1],
   struct ZoO_strings string [const restrict static 1],
   ZoO_index const aliases_count,
   const char * restrict aliases [const restrict static aliases_count]
)
{
   int error;
   ZoO_index curr_word, next_word;
   ZoO_index curr_word_id, next_word_id;

   curr_word = 0;

   if (string->words_count == 0)
   {
      return 0;
   }

   for (curr_word = 0; curr_word < aliases_count; ++curr_word)
   {
      if (ZoO_IS_PREFIX(aliases[curr_word], string->words[0]))
      {
         return 0;
      }
   }

   curr_word = 0;

   if (ZoO_knowledge_learn(k, string->words[curr_word], &curr_word_id) < 0)
   {
      return -1;
   }

   if (link_words(k, ZoO_WORD_START_OF_LINE, curr_word_id) < 0)
   {
      error = -1;

      ZoO_WARNING
      (
         "Could not indicate that '"
         ZoO_CHAR_STRING_SYMBOL
         "' was the first word of the sentence.",
         string->words[0]
      );
   }

   next_word = 1;

   error = 0;

   while (next_word < string->words_count)
   {
      /* prevents words [restrict], k [restrict] */
      if (ZoO_knowledge_learn(k, string->words[next_word], &next_word_id) < 0)
      {
         return -1;
      }

      if (link_words(k, curr_word_id, next_word_id) < 0)
      {
         error = -1;

         ZoO_WARNING
         (
            "Could not add a link between words '"
            ZoO_CHAR_STRING_SYMBOL
            "' and '"
            ZoO_CHAR_STRING_SYMBOL
            "'.",
            string->words[curr_word],
            string->words[next_word]
         );
      }

      curr_word = next_word;
      curr_word_id = next_word_id;
      /*
       * Safe:
       *  - next_word < words_count
       *  - words_count =< ZoO_INDEX_MAX
       *  ----
       *  next_word < ZoO_INDEX_MAX
       */
      next_word += 1;
   }

   if (link_words(k, curr_word_id, ZoO_WORD_END_OF_LINE) < 0)
   {
      error = -1;

      ZoO_WARNING
      (
         "Could not indicate that '"
         ZoO_CHAR_STRING_SYMBOL
         "' was the last word of the sentence.",
         string->words[curr_word_id]
      );
   }

   return error;
}