summaryrefslogtreecommitdiff
blob: e7d1cd954a7a81804050a5e1cee86dc3acf479b7 (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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h> /* defines SIZE_MAX */

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

#include "knowledge.h"

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

/* See: "knowledge.h" */
int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1])
{
   int error;
   ZoO_index reserved_word_id;

   k->words = (struct ZoO_knowledge_word *) NULL;
   k->words_length = 0;
   k->words_sorted = (ZoO_index *) NULL;

   k->sequences = (ZoO_index **) NULL;
   k->sequences_length = 0;
   k->sequences_sorted = (ZoO_index *) NULL;

   error = pthread_mutex_init(&(k->mutex), (const pthread_mutexattr_t *) NULL);

   if (error != 0)
   {
      fprintf
      (
         stderr,
         "[F] Unable to initialize knowledge mutex: %s.\n",
         strerror(error)
      );

      return -1;
   }

   if
   (
      (
         ZoO_knowledge_learn_word
         (
            k,
            "[SoS]",
            5,
            &reserved_word_id,
            stderr
         ) < 0
      )
      ||
      (
         ZoO_knowledge_learn_word
         (
            k,
            "[EoS]",
            5,
            &reserved_word_id,
            stderr
         ) < 0
      )
   )
   {
      ZoO_S_FATAL(stderr, "Unable to learn reserved words.");

      return -1;
   }

   return 0;
}

int ZoO_knowledge_lock_access
(
   struct ZoO_knowledge k [const restrict static 1],
   FILE io [const restrict static 1]
)
{
   int err;

   err = pthread_mutex_lock(&(k->mutex));

   if (err != 0)
   {
      ZoO_ERROR
      (
         io,
         "Unable to get exclusive access to knowledge: %s",
         strerror(err)
      );

      return -1;
   }

   return 0;
}

void ZoO_knowledge_unlock_access
(
   struct ZoO_knowledge k [const restrict static 1],
   FILE io [const restrict static 1]
)
{
   int err;

   err = pthread_mutex_unlock(&(k->mutex));

   if (err != 0)
   {
      ZoO_ERROR
      (
         io,
         "Unable to release exclusive access to knowledge: %s",
         strerror(err)
      );
   }
}

void ZoO_knowledge_get_word
(
   const struct ZoO_knowledge k [const static 1],
   const ZoO_index word_ref,
   const ZoO_char * word [const restrict static 1],
   ZoO_index word_length [const restrict static 1]
)
{
   *word = k->words[word_ref].word;
   *word_length = k->words[word_ref].word_length;
}

int ZoO_knowledge_rarest_word
(
   const struct ZoO_knowledge k [const static 1],
   const ZoO_index sequence [const restrict static 1],
   const size_t sequence_length,
   ZoO_index word_id [const restrict static 1]
)
{
   ZoO_index current_max_score;
   size_t i;
   int success;

   current_max_score = ZoO_INDEX_MAX;

   success = -1;

   for (i = 0; i < sequence_length; ++i)
   {
      if
      (
         (k->words[sequence[i]].occurrences <= current_max_score)
         /* Otherwise we might just have learned it, or it must not be used. */
         && (k->words[sequence[i]].occurrences > 1)
      )
      {
         current_max_score = k->words[sequence[i]].occurrences;
         *word_id = sequence[i];
         success = 0;
      }
   }

   return success;
}