summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-31 16:21:24 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-31 16:21:24 +0100
commit509ac16d892aeb5091f68620247f6815d2e4b5f5 (patch)
treec4adebce7791c10c4c362b77f32d4a339e8c8125 /src/knowledge
parent1373211465c34015ee900e097aa87fbffb401187 (diff)
Switched to sockets, continuing implementation...
Diffstat (limited to 'src/knowledge')
-rw-r--r--src/knowledge/knowledge.c26
-rw-r--r--src/knowledge/knowledge.h66
-rw-r--r--src/knowledge/knowledge_finalize.c6
-rw-r--r--src/knowledge/knowledge_learn_markov_sequence.c55
-rw-r--r--src/knowledge/knowledge_learn_sequence.c192
-rw-r--r--src/knowledge/knowledge_learn_word.c4
-rw-r--r--src/knowledge/knowledge_search.c153
-rw-r--r--src/knowledge/knowledge_swt_tws_modifications.c29
-rw-r--r--src/knowledge/knowledge_types.h11
9 files changed, 381 insertions, 161 deletions
diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c
index 463d40b..c9bfc2a 100644
--- a/src/knowledge/knowledge.c
+++ b/src/knowledge/knowledge.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include <stdint.h> /* defines SIZE_MAX */
#include "../pipe/pipe.h"
@@ -9,8 +10,10 @@
/** Basic functions of the ZoO_knowledge structure ****************************/
/* See: "knowledge.h" */
-void ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])
+int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1])
{
+ int error;
+
k->words = (struct ZoO_knowledge_word *) NULL;
k->words_length = 0;
k->words_sorted = (ZoO_index *) NULL;
@@ -18,6 +21,22 @@ void ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])
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;
+ }
+
+ return 0;
}
int ZoO_knowledge_lock_access
@@ -26,8 +45,7 @@ int ZoO_knowledge_lock_access
const struct ZoO_pipe io [const restrict static 1]
)
{
- /* TODO */
- return 0;
+ return pthread_mutex_lock(&(k->mutex));
}
void ZoO_knowledge_unlock_access
@@ -36,5 +54,5 @@ void ZoO_knowledge_unlock_access
const struct ZoO_pipe io [const restrict static 1]
)
{
- /* TODO */
+ pthread_mutex_unlock(&(k->mutex));
}
diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h
index c65ee2c..e868943 100644
--- a/src/knowledge/knowledge.h
+++ b/src/knowledge/knowledge.h
@@ -20,10 +20,7 @@ void ZoO_knowledge_unlock_access
const struct ZoO_pipe io [const restrict static 1]
);
-void ZoO_knowledge_initialize
-(
- struct ZoO_knowledge k [const restrict static 1]
-);
+int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]);
void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]);
@@ -59,13 +56,12 @@ int ZoO_knowledge_learn_markov_sequence
(
struct ZoO_knowledge k [const restrict static 1],
const ZoO_index sequence [const restrict static 1],
- const ZoO_index sequence_length,
- const ZoO_index markov_order,
+ const ZoO_index markov_order, /* Pre (> markov_order 1) */
ZoO_index sequence_id [const restrict static 1],
const struct ZoO_pipe io [const restrict static 1]
);
-int ZoO_knowledge_get_following_sequences_ref
+int ZoO_knowledge_get_swt_sequences_ref
(
const struct ZoO_knowledge k [const static 1],
const ZoO_index initial_word,
@@ -110,27 +106,67 @@ int ZoO_knowledge_find_word_id
ZoO_index result [const restrict static 1]
);
-int ZoO_knowledge_find_preceding_words
+int ZoO_knowledge_find_tws_targets
(
const struct ZoO_knowledge k [const static 1],
const ZoO_index sequence [const restrict],
const ZoO_index markov_order,
- const ZoO_index * restrict preceding_words [const restrict static 1],
- const ZoO_index * restrict preceding_words_weights [const restrict static 1],
- ZoO_index preceding_words_weights_sum [const restrict static 1],
+ const ZoO_index * restrict targets [const restrict static 1],
+ const ZoO_index * restrict targets_weights [const restrict static 1],
+ ZoO_index targets_weights_sum [const restrict static 1],
const struct ZoO_pipe io [const restrict static 1]
);
-int ZoO_knowledge_find_following_words
+int ZoO_knowledge_find_swt_targets
(
const struct ZoO_knowledge k [const static 1],
const ZoO_index sequence [const restrict],
const size_t sequence_length,
const ZoO_index markov_order,
- const ZoO_index * restrict following_words [const restrict static 1],
- const ZoO_index * restrict following_words_weights [const restrict static 1],
- ZoO_index following_words_weights_sum [const restrict static 1],
+ const ZoO_index * restrict targets [const restrict static 1],
+ const ZoO_index * restrict targets_weights [const restrict static 1],
+ ZoO_index targets_weights_sum [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
+);
+
+int ZoO_knowledge_strengthen_swt
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index sequence_id,
+ const ZoO_index word_id,
+ const ZoO_index target_id,
+ const struct ZoO_pipe io [const restrict static 1]
+);
+
+int ZoO_knowledge_strengthen_tws
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index target_id,
+ const ZoO_index word_id,
+ const ZoO_index sequence_id,
const struct ZoO_pipe io [const restrict static 1]
);
+/*
+ * TODO
+ */
+/*
+int ZoO_knowledge_weaken_swt
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index sequence_id,
+ const ZoO_index word_id,
+ const ZoO_index target_id,
+ const struct ZoO_pipe io [const restrict static 1]
+);
+
+int ZoO_knowledge_weaken_tws
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index target_id,
+ const ZoO_index word_id,
+ const ZoO_index sequence_id,
+ const struct ZoO_pipe io [const restrict static 1]
+);
+*/
#endif
diff --git a/src/knowledge/knowledge_finalize.c b/src/knowledge/knowledge_finalize.c
index 9628672..9546650 100644
--- a/src/knowledge/knowledge_finalize.c
+++ b/src/knowledge/knowledge_finalize.c
@@ -65,8 +65,8 @@ static void knowledge_word_finalize
w->word = (ZoO_char *) NULL;
}
- knowledge_sequence_collection_finalize(&(w->followed));
- knowledge_sequence_collection_finalize(&(w->preceded));
+ knowledge_sequence_collection_finalize(&(w->swt));
+ knowledge_sequence_collection_finalize(&(w->tws));
}
/* See: "knowledge.h" */
@@ -115,4 +115,6 @@ void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1])
k->sequences_sorted = (ZoO_index *) NULL;
}
+
+ pthread_mutex_destroy(&(k->mutex));
}
diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c
index 2bd0103..4258c4a 100644
--- a/src/knowledge/knowledge_learn_markov_sequence.c
+++ b/src/knowledge/knowledge_learn_markov_sequence.c
@@ -143,7 +143,6 @@ static int reallocate_sequences_sorted_list
static ZoO_index * copy_sequence
(
const ZoO_index base [const restrict static 1],
- const ZoO_index base_length,
const ZoO_index destination_length,
const struct ZoO_pipe io [const restrict static 1]
)
@@ -169,45 +168,12 @@ static ZoO_index * copy_sequence
return (ZoO_index *) NULL;
}
- if (base_length == destination_length)
- {
- memcpy
- (
- (void *) result,
- (const void *) base,
- (((size_t) base_length) * sizeof(ZoO_index))
- );
- }
- else if (base[0] == ZoO_START_OF_SEQUENCE_ID)
- {
- diff = (destination_length - base_length);
-
- memcpy
- (
- (void *) (result + diff),
- (const void *) base,
- (((size_t) base_length) * sizeof(ZoO_index))
- );
-
- for (i = 0; i < diff; ++i)
- {
- result[i] = ZoO_START_OF_SEQUENCE_ID;
- }
- }
- else if (base[(base_length - 1)] == ZoO_END_OF_SEQUENCE_ID)
- {
- memcpy
- (
- (void *) result,
- (const void *) base,
- (((size_t) base_length) * sizeof(ZoO_index))
- );
-
- for (i = base_length; i < destination_length; ++i)
- {
- result[i] = ZoO_END_OF_SEQUENCE_ID;
- }
- }
+ memcpy
+ (
+ (void *) result,
+ (const void *) base,
+ (((size_t) destination_length) * sizeof(ZoO_index))
+ );
return result;
}
@@ -220,7 +186,6 @@ static int add_sequence
(
struct ZoO_knowledge k [const restrict static 1],
const ZoO_index sequence [const restrict static 1],
- const ZoO_index sequence_length,
const ZoO_index markov_order, /* Pre (> markov_order 1) */
const ZoO_index sequence_id,
const ZoO_index sorted_sequence_id,
@@ -241,7 +206,7 @@ static int add_sequence
return -1;
}
- stored_sequence = copy_sequence(sequence, sequence_length, markov_order, io);
+ stored_sequence = copy_sequence(sequence, (markov_order - 1), io);
if (stored_sequence == (ZoO_index *) NULL)
{
@@ -283,7 +248,6 @@ static int find_sequence
(
const struct ZoO_knowledge k [const static 1],
const ZoO_index sequence [const restrict static 1],
- const ZoO_index sequence_length,
const ZoO_index markov_order, /* Pre: (> 1) */
ZoO_index sequence_id [const restrict static 1]
)
@@ -317,7 +281,7 @@ static int find_sequence
k->sequences[k->sequences_sorted[i]],
markov_sequence_length,
sequence,
- sequence_length
+ markov_sequence_length
);
if (cmp > 0)
@@ -359,7 +323,6 @@ int ZoO_knowledge_learn_markov_sequence
(
struct ZoO_knowledge k [const restrict static 1],
const ZoO_index sequence [const restrict static 1],
- const ZoO_index sequence_length,
const ZoO_index markov_order, /* Pre (> markov_order 1) */
ZoO_index sequence_id [const restrict static 1],
const struct ZoO_pipe io [const restrict static 1]
@@ -373,7 +336,6 @@ int ZoO_knowledge_learn_markov_sequence
(
k,
sequence,
- sequence_length,
markov_order,
sequence_id
) == 0
@@ -390,7 +352,6 @@ int ZoO_knowledge_learn_markov_sequence
(
k,
sequence,
- sequence_length,
markov_order,
*sequence_id,
sorted_id,
diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c
index 35b6d15..927b186 100644
--- a/src/knowledge/knowledge_learn_sequence.c
+++ b/src/knowledge/knowledge_learn_sequence.c
@@ -8,38 +8,180 @@
#include "knowledge.h"
+
+
/******************************************************************************/
/** LEARN FOLLOWING SEQUENCE **************************************************/
/******************************************************************************/
-static int add_following_sequence
+static void parse_swt_sequence
+(
+ const ZoO_index sequence [const restrict static 1],
+ const size_t index,
+ ZoO_index buffer [const restrict static 1],
+ const ZoO_index buffer_length
+)
+{
+ size_t j;
+ size_t index_offset;
+
+ index_offset = buffer_length;
+
+ for (j = 0; j < buffer_length; ++j)
+ {
+ index_offset = (buffer_length - j);
+
+ if (index >= index_offset)
+ {
+ buffer[j] = sequence[index - index_offset];
+ }
+ else
+ {
+ buffer[j] = ZoO_START_OF_SEQUENCE_ID;
+ }
+ }
+}
+
+static int add_swt_sequence
(
struct ZoO_knowledge k [const restrict static 1],
const ZoO_index sequence [const restrict static 1],
const size_t index,
const size_t sequence_length,
- const ZoO_index markov_order,
+ ZoO_index buffer [const restrict static 1],
+ const ZoO_index buffer_length,
const struct ZoO_pipe io [const restrict static 1]
)
{
- /* TODO */
- return -1;
+ ZoO_index sequence_id;
+
+ parse_swt_sequence(sequence, index, buffer, buffer_length);
+
+ if
+ (
+ ZoO_knowledge_learn_markov_sequence
+ (
+ k,
+ buffer,
+ (buffer_length + 1),
+ &sequence_id,
+ io
+ )
+ )
+ {
+ return -1;
+ }
+
+ if (index == (sequence_length - 1))
+ {
+ return
+ ZoO_knowledge_strengthen_swt
+ (
+ k,
+ sequence_id,
+ sequence[index],
+ ZoO_END_OF_SEQUENCE_ID,
+ io
+ );
+ }
+ else
+ {
+ return
+ ZoO_knowledge_strengthen_swt
+ (
+ k,
+ sequence_id,
+ sequence[index],
+ sequence[index + 1],
+ io
+ );
+ }
}
/******************************************************************************/
/** LEARN PRECEDING SEQUENCE **************************************************/
/******************************************************************************/
-static int add_preceding_sequence
+static void parse_tws_sequence
+(
+ const ZoO_index sequence [const restrict static 1],
+ const size_t index,
+ const size_t sequence_length,
+ ZoO_index buffer [const restrict static 1],
+ const ZoO_index buffer_length
+)
+{
+ size_t j;
+ size_t index_offset;
+ const size_t remaining_items = (sequence_length - index);
+
+ for (j = 0; j < buffer_length; ++j)
+ {
+ index_offset = (j + 1);
+
+ if (remaining_items > index_offset)
+ {
+ buffer[j] = sequence[index + index_offset];
+ }
+ else
+ {
+ buffer[j] = ZoO_END_OF_SEQUENCE_ID;
+ }
+ }
+}
+
+static int add_tws_sequence
(
struct ZoO_knowledge k [const restrict static 1],
const ZoO_index sequence [const restrict static 1],
const size_t index,
const size_t sequence_length,
- const ZoO_index markov_order,
+ ZoO_index buffer [const restrict static 1],
+ const ZoO_index buffer_length,
const struct ZoO_pipe io [const restrict static 1]
)
{
- /* TODO */
- return -1;
+ ZoO_index sequence_id;
+
+ parse_tws_sequence(sequence, index, sequence_length, buffer, buffer_length);
+
+ if
+ (
+ ZoO_knowledge_learn_markov_sequence
+ (
+ k,
+ buffer,
+ (buffer_length + 1),
+ &sequence_id,
+ io
+ )
+ )
+ {
+ return -1;
+ }
+
+ if (index == 0)
+ {
+ return
+ ZoO_knowledge_strengthen_tws
+ (
+ k,
+ ZoO_START_OF_SEQUENCE_ID,
+ sequence[index],
+ sequence_id,
+ io
+ );
+ }
+ else
+ {
+ return
+ ZoO_knowledge_strengthen_tws
+ (
+ k,
+ sequence[index - 1],
+ sequence[index],
+ sequence_id,
+ io
+ );
+ }
}
/******************************************************************************/
@@ -58,35 +200,49 @@ int ZoO_knowledge_learn_sequence
size_t i;
const ZoO_index buffer_length = (markov_order - 1);
- for (i = 0; i < sequence_length; ++i)
+ buffer =
+ (ZoO_index *) calloc
+ (
+ (size_t) buffer_length,
+ sizeof(ZoO_index)
+ );
+
+ if (buffer == (ZoO_index *) NULL)
{
- k->words[sequence[i]].occurrences += 1;
+ ZoO_S_ERROR
+ (
+ io,
+ "Unable to allocate memory required to create markov sequences."
+ );
+
+ return -1;
+ }
- add_preceding_sequence
+ for (i = 0; i < sequence_length; ++i)
+ {
+ add_tws_sequence
(
k,
sequence,
i,
sequence_length,
+ buffer,
buffer_length,
io
);
- add_following_sequence
+ add_swt_sequence
(
k,
sequence,
i,
sequence_length,
- markov_order,
+ buffer,
+ buffer_length,
io
);
- /*
- * TODO: in case of failure, undo part of the word done so far: instead
- * of unlearning, just remove the occurrence count of sequences and
- * words so that {k} remains coherent.
- */
+ k->words[sequence[i]].occurrences += 1;
}
return 0;
diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c
index 33a2bf1..5d932d2 100644
--- a/src/knowledge/knowledge_learn_word.c
+++ b/src/knowledge/knowledge_learn_word.c
@@ -33,8 +33,8 @@ static void initialize_word
w->word_length = 0;
w->occurrences = 0;
- initialize_sequence_collection(&(w->followed));
- initialize_sequence_collection(&(w->preceded));
+ initialize_sequence_collection(&(w->swt));
+ initialize_sequence_collection(&(w->tws));
}
/******************************************************************************/
diff --git a/src/knowledge/knowledge_search.c b/src/knowledge/knowledge_search.c
index 4ea4572..198da1d 100644
--- a/src/knowledge/knowledge_search.c
+++ b/src/knowledge/knowledge_search.c
@@ -73,14 +73,15 @@ int ZoO_knowledge_find_word_id
}
}
-int ZoO_knowledge_find_preceding_words
+/* pre: \length(sequence) >= markov_order */
+int ZoO_knowledge_find_tws_targets
(
const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
+ const ZoO_index sequence [restrict static 1],
const ZoO_index markov_order, /* Pre: (> 0) */
- const ZoO_index * restrict preceding_words [const restrict static 1],
- const ZoO_index * restrict preceding_words_weights [const restrict static 1],
- ZoO_index preceding_words_weights_sum [const restrict static 1],
+ const ZoO_index * restrict targets [const restrict static 1],
+ const ZoO_index * restrict targets_weights [const restrict static 1],
+ ZoO_index targets_weights_sum [const restrict static 1],
const struct ZoO_pipe io [const restrict static 1]
)
{
@@ -89,19 +90,23 @@ int ZoO_knowledge_find_preceding_words
ZoO_index i, current_min, current_max, local_sequence;
const ZoO_index * restrict candidate;
const ZoO_index markov_sequence_length = (markov_order - 1);
- const ZoO_index word = sequence[markov_sequence_length];
+ const ZoO_index word = sequence[0];
- if (word >= k->words_length)
+ if
+ (
+ (word >= k->words_length)
+ || (k->words[word].occurrences == 0)
+ )
{
ZoO_S_ERROR
(
io,
- "Attempting to find the preceding words of an unknown word."
+ "Attempting to find the TWS targets of an unknown word."
);
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -1;
}
@@ -110,30 +115,34 @@ int ZoO_knowledge_find_preceding_words
if (markov_order == 1)
{
/* Special case: empty sequences. */
- *preceding_words = (const ZoO_index *) k->words[word].preceded.targets;
+ *targets = (const ZoO_index *) k->words[word].tws.targets;
- *preceding_words_weights =
- (const ZoO_index *) k->words[word].preceded.targets_occurrences;
+ *targets_weights =
+ (const ZoO_index *) k->words[word].tws.targets_occurrences;
- *preceding_words_weights_sum = k->words[word].occurrences;
+ *targets_weights_sum = k->words[word].occurrences;
return 0;
}
+
+ /* pre: \length(sequence) >= markov_order */
+ /* markov_order > 1 */
+ sequence += 1; /* get the relevant part of the sequence. */
+
/* Handles the case where the list is empty ********************************/
- current_max = k->words[word].preceded.sequences_ref_length;
+ current_max = k->words[word].tws.sequences_ref_length;
if (current_max == 0)
{
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
ZoO_S_ERROR
(
io,
- "Attempting to find the preceding words of a sequence that never had "
- "any."
+ "Attempting to find the TWS targets of a sequence that never had any."
);
return -2;
@@ -147,12 +156,12 @@ int ZoO_knowledge_find_preceding_words
{
i = (current_min + ((current_max - current_min) / 2));
- local_sequence = k->words[word].preceded.sequences_ref_sorted[i];
+ local_sequence = k->words[word].tws.sequences_ref_sorted[i];
(void) ZoO_knowledge_get_sequence
(
k,
- k->words[word].preceded.sequences_ref[local_sequence],
+ k->words[word].tws.sequences_ref[local_sequence],
&candidate,
io
);
@@ -172,9 +181,9 @@ int ZoO_knowledge_find_preceding_words
if (current_min > current_max)
{
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -2;
}
@@ -183,9 +192,9 @@ int ZoO_knowledge_find_preceding_words
{
if ((current_min > current_max) || (i == 0))
{
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -2;
}
@@ -194,28 +203,28 @@ int ZoO_knowledge_find_preceding_words
}
else
{
- *preceding_words = k->words[word].preceded.targets[local_sequence];
+ *targets = k->words[word].tws.targets[local_sequence];
- *preceding_words_weights =
- k->words[word].preceded.targets_occurrences[local_sequence];
+ *targets_weights =
+ k->words[word].tws.targets_occurrences[local_sequence];
- *preceding_words_weights_sum =
- k->words[word].preceded.occurrences[local_sequence];
+ *targets_weights_sum =
+ k->words[word].tws.occurrences[local_sequence];
return 0;
}
}
}
-int ZoO_knowledge_find_following_words
+int ZoO_knowledge_find_swt_targets
(
const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
+ const ZoO_index sequence [restrict static 1],
const size_t sequence_length,
const ZoO_index markov_order,
- const ZoO_index * restrict following_words [const restrict static 1],
- const ZoO_index * restrict following_words_weights [const restrict static 1],
- ZoO_index following_words_weights_sum [const restrict static 1],
+ const ZoO_index * restrict targets [const restrict static 1],
+ const ZoO_index * restrict targets_weights [const restrict static 1],
+ ZoO_index targets_weights_sum [const restrict static 1],
const struct ZoO_pipe io [const restrict static 1]
)
{
@@ -224,21 +233,23 @@ int ZoO_knowledge_find_following_words
ZoO_index i, current_min, current_max, local_sequence;
const ZoO_index * restrict candidate;
const ZoO_index markov_sequence_length = (markov_order - 1);
- const size_t sequence_offset =
- ((sequence_length - ((size_t) markov_sequence_length)) - 1);
- const ZoO_index word = sequence[sequence_offset];
+ const ZoO_index word = sequence[sequence_length - 1];
- if (word >= k->words_length)
+ if
+ (
+ (word >= k->words_length)
+ || (k->words[word].occurrences == 0)
+ )
{
ZoO_S_ERROR
(
io,
- "Attempting to find the following words of an unknown word."
+ "Attempting to find the SWT targets of an unknown word."
);
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -1;
}
@@ -246,30 +257,30 @@ int ZoO_knowledge_find_following_words
if (markov_order == 1)
{
/* Special case: empty sequences. */
- *following_words = (const ZoO_index *) k->words[word].preceded.targets;
+ *targets = (const ZoO_index *) k->words[word].swt.targets;
- *following_words_weights =
- (const ZoO_index *) k->words[word].preceded.targets_occurrences;
+ *targets_weights =
+ (const ZoO_index *) k->words[word].swt.targets_occurrences;
- *following_words_weights_sum = k->words[word].occurrences;
+ *targets_weights_sum = k->words[word].occurrences;
return 0;
}
+ sequence = (sequence + (sequence_length - markov_order));
/* Handles the case where the list is empty ********************************/
- current_max = k->words[word].preceded.sequences_ref_length;
+ current_max = k->words[word].swt.sequences_ref_length;
if (current_max == 0)
{
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
ZoO_S_WARNING
(
io,
- "Attempting to find the following words of a sequence that never had "
- "any."
+ "Attempting to find the SWT targets of a sequence that never had any."
);
return -2;
@@ -283,12 +294,12 @@ int ZoO_knowledge_find_following_words
{
i = (current_min + ((current_max - current_min) / 2));
- local_sequence = k->words[word].followed.sequences_ref_sorted[i];
+ local_sequence = k->words[word].swt.sequences_ref_sorted[i];
(void) ZoO_knowledge_get_sequence
(
k,
- k->words[word].followed.sequences_ref[local_sequence],
+ k->words[word].swt.sequences_ref[local_sequence],
&candidate,
io
);
@@ -296,7 +307,7 @@ int ZoO_knowledge_find_following_words
cmp =
ZoO_sequence_cmp
(
- (sequence + sequence_offset),
+ sequence,
markov_sequence_length,
candidate,
markov_sequence_length
@@ -308,9 +319,9 @@ int ZoO_knowledge_find_following_words
if (current_min > current_max)
{
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -2;
}
@@ -319,9 +330,9 @@ int ZoO_knowledge_find_following_words
{
if ((current_min > current_max) || (i == 0))
{
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
+ *targets = (const ZoO_index *) NULL;
+ *targets_weights = (const ZoO_index *) NULL;
+ *targets_weights_sum = 0;
return -2;
}
@@ -330,13 +341,13 @@ int ZoO_knowledge_find_following_words
}
else
{
- *following_words = k->words[word].followed.targets[local_sequence];
+ *targets = k->words[word].swt.targets[local_sequence];
- *following_words_weights =
- k->words[word].followed.targets_occurrences[local_sequence];
+ *targets_weights =
+ k->words[word].swt.targets_occurrences[local_sequence];
- *following_words_weights_sum =
- k->words[word].followed.occurrences[local_sequence];
+ *targets_weights_sum =
+ k->words[word].swt.occurrences[local_sequence];
return 0;
}
diff --git a/src/knowledge/knowledge_swt_tws_modifications.c b/src/knowledge/knowledge_swt_tws_modifications.c
new file mode 100644
index 0000000..87305f2
--- /dev/null
+++ b/src/knowledge/knowledge_swt_tws_modifications.c
@@ -0,0 +1,29 @@
+#include "knowledge.h"
+
+int ZoO_knowledge_strengthen_swt
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index sequence_id,
+ const ZoO_index word_id,
+ const ZoO_index target_id,
+ const struct ZoO_pipe io [const restrict static 1]
+)
+{
+ /* TODO */
+
+ return -1;
+}
+
+int ZoO_knowledge_strengthen_tws
+(
+ struct ZoO_knowledge k [const restrict static 1],
+ const ZoO_index target_id,
+ const ZoO_index word_id,
+ const ZoO_index sequence_id,
+ const struct ZoO_pipe io [const restrict static 1]
+)
+{
+ /* TODO */
+
+ return -1;
+}
diff --git a/src/knowledge/knowledge_types.h b/src/knowledge/knowledge_types.h
index 4962991..9db77bd 100644
--- a/src/knowledge/knowledge_types.h
+++ b/src/knowledge/knowledge_types.h
@@ -1,6 +1,8 @@
#ifndef _ZoO_KNOWLEDGE_KNOWLEDGE_TYPES_H_
#define _ZoO_KNOWLEDGE_KNOWLEDGE_TYPES_H_
+#include <pthread.h>
+
#include "../core/index_types.h"
#include "../core/char_types.h"
@@ -20,8 +22,12 @@ struct ZoO_knowledge_word
const ZoO_char * word;
ZoO_index word_length;
ZoO_index occurrences;
- struct ZoO_knowledge_sequence_collection followed;
- struct ZoO_knowledge_sequence_collection preceded;
+
+ /* [Sequence] [Word] [Target] */
+ struct ZoO_knowledge_sequence_collection swt;
+
+ /* [Target] [Word] [Sequence] */
+ struct ZoO_knowledge_sequence_collection tws;
};
struct ZoO_knowledge
@@ -32,6 +38,7 @@ struct ZoO_knowledge
ZoO_index ** sequences;
ZoO_index sequences_length;
ZoO_index * sequences_sorted;
+ pthread_mutex_t mutex;
};
#endif