summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-18 19:09:16 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-18 19:09:16 +0100
commit0d49fb74eadcf933f696420cd182077927680d26 (patch)
tree9220d260ce878f369138da12dae0300cf9ade5c9 /src/core
parent24afb3e60bafd98e6a83dcb41ee6a7f7d41e76bc (diff)
Done with 'core', starting to work on 'knowledge'.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt9
-rw-r--r--src/core/assimilate.c281
-rw-r--r--src/core/char.c18
-rw-r--r--src/core/char.h35
-rw-r--r--src/core/char_types.h6
-rw-r--r--src/core/index.c61
-rw-r--r--src/core/index.h11
-rw-r--r--src/core/index_types.h2
-rw-r--r--src/core/knowledge.c15
-rw-r--r--src/core/knowledge.h101
-rw-r--r--src/core/knowledge_finalize.c121
-rw-r--r--src/core/knowledge_search.c335
-rw-r--r--src/core/knowledge_types.h37
-rw-r--r--src/core/main.c436
-rw-r--r--src/core/sequence.c84
-rw-r--r--src/core/sequence.h20
-rw-r--r--src/core/sequence_creation.c9
-rw-r--r--src/core/sequence_from_string.c315
-rw-r--r--src/core/sequence_types.h3
19 files changed, 526 insertions, 1373 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index fe28080..1e1daa8 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -1,13 +1,10 @@
set(
SRC_FILES ${SRC_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/char.c
- ${CMAKE_CURRENT_SOURCE_DIR}/main.c
- ${CMAKE_CURRENT_SOURCE_DIR}/knowledge.c
- ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_search.c
- ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_finalize.c
- ${CMAKE_CURRENT_SOURCE_DIR}/assimilate.c
- ${CMAKE_CURRENT_SOURCE_DIR}/sequence_creation.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/index.c
${CMAKE_CURRENT_SOURCE_DIR}/sequence.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/sequence_creation.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/sequence_from_string.c
)
set(SRC_FILES ${SRC_FILES} PARENT_SCOPE)
diff --git a/src/core/assimilate.c b/src/core/assimilate.c
deleted file mode 100644
index 7f03e1b..0000000
--- a/src/core/assimilate.c
+++ /dev/null
@@ -1,281 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-
-#include "../io/error.h"
-
-#include "knowledge.h"
-
-/** Functions to assimilate sentences using a ZoO_knowledge structure *********/
-
-
-static int add_sequence
-(
- ZoO_index links_count [const],
- struct ZoO_knowledge_link * links [const],
- ZoO_index const sequence [const restrict static ZoO_MARKOV_ORDER],
- ZoO_index const target_i,
- ZoO_index const offset
-)
-{
- ZoO_index link_index, i;
- struct ZoO_knowledge_link * link;
- ZoO_index * new_p;
-
- if
- (
- ZoO_knowledge_get_link
- (
- links_count,
- links,
- (sequence + offset),
- &link_index
- ) < 0
- )
- {
- return -1;
- }
-
- link = (*links + link_index);
- link->occurrences += 1;
-
- for (i = 0; i < link->targets_count; ++i)
- {
- if (link->targets[i] == sequence[target_i])
- {
- link->targets_occurrences[i] += 1;
-
- return 0;
- }
- }
-
- link->targets_count += 1;
-
- new_p =
- (ZoO_index *) realloc
- (
- (void *) link->targets,
- (sizeof(ZoO_index) * link->targets_count)
- );
-
- if (new_p == (ZoO_index *) NULL)
- {
- link->targets_count -= 1;
-
- /* TODO: err. */
- return -1;
- }
-
- link->targets = new_p;
- link->targets[link->targets_count - 1] = sequence[target_i];
-
- new_p =
- (ZoO_index *) realloc
- (
- (void *) link->targets_occurrences,
- (sizeof(ZoO_index) * link->targets_count)
- );
-
- if (new_p == (ZoO_index *) NULL)
- {
- link->targets_count -= 1;
-
- /* TODO: err. */
- return -1;
- }
-
- link->targets_occurrences = new_p;
- link->targets_occurrences[link->targets_count - 1] = 1;
-
- return 0;
-}
-
-static int add_word_occurrence
-(
- struct ZoO_knowledge k [const restrict static 1],
- ZoO_index const sequence [const static ((ZoO_MARKOV_ORDER * 2) + 1)]
-)
-{
- ZoO_index w;
- int error;
-
- w = sequence[ZoO_MARKOV_ORDER];
-
- error =
- add_sequence
- (
- &(k->words[w].forward_links_count),
- &(k->words[w].forward_links),
- sequence + (ZoO_MARKOV_ORDER + 1),
- (ZoO_MARKOV_ORDER - 1),
- 0
- );
-
- error =
- (
- add_sequence
- (
- &(k->words[w].backward_links_count),
- &(k->words[w].backward_links),
- sequence,
- 0,
- 1
- )
- | error
- );
-
- return error;
-}
-
-static int should_assimilate
-(
- struct ZoO_strings string [const restrict static 1],
- ZoO_index const aliases_count,
- const char * restrict aliases [const restrict static aliases_count]
-)
-{
- ZoO_index i;
-
- /* Don't assimilate empty strings. */
- if (string->words_count == 0)
- {
- return 0;
- }
-
- /* Don't assimilate things that start with our name. */
- for (i = 0; i < aliases_count; ++i)
- {
- if (ZoO_IS_PREFIX(aliases[i], string->words[0]))
- {
- return 0;
- }
- }
-
- return 1;
-}
-
-static int init_sequence
-(
- struct ZoO_knowledge k [const static 1],
- struct ZoO_strings string [const restrict static 1],
- ZoO_index sequence [const restrict static ((ZoO_MARKOV_ORDER * 2) + 1)]
-)
-{
- ZoO_index i;
-
- /* We are going to link this sequence to ZoO_WORD_START_OF_LINE */
- sequence[ZoO_MARKOV_ORDER] = ZoO_WORD_START_OF_LINE;
-
- for (i = 1; i <= ZoO_MARKOV_ORDER; ++i)
- {
- sequence[ZoO_MARKOV_ORDER - i] = ZoO_WORD_START_OF_LINE;
-
- if (i <= string->words_count)
- {
- if
- (
- ZoO_knowledge_learn
- (
- k,
- string->words[i - 1],
- (sequence + (ZoO_MARKOV_ORDER + i))
- ) < 0
- )
- {
- return -1;
- }
- }
- else
- {
- sequence[ZoO_MARKOV_ORDER + i] = ZoO_WORD_END_OF_LINE;
- }
- }
-
- return 0;
-}
-
-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 sequence[(ZoO_MARKOV_ORDER * 2) + 1];
- ZoO_index next_word, new_word, new_word_id;
-
- if (!should_assimilate(string, aliases_count, aliases))
- {
- return 0;
- }
-
- if (init_sequence(k, string, sequence) < 0)
- {
- return -1;
- }
-
- if (add_word_occurrence(k, sequence) < 0)
- {
- error = -1;
-
- /* There's a pun... */
- ZoO_S_WARNING("Could not add a link between words.");
-
- return -1;
- }
-
- error = 0;
-
- next_word = 0;
- new_word = ZoO_MARKOV_ORDER;
-
- while (next_word <= (string->words_count + ZoO_MARKOV_ORDER))
- {
- if (new_word < string->words_count)
- {
- /* prevents words [restrict], k [restrict] */
- if (ZoO_knowledge_learn(k, string->words[new_word], &new_word_id) < 0)
- {
- return -1;
- }
- }
- else
- {
- new_word_id = ZoO_WORD_END_OF_LINE;
- }
-
- memmove
- (
- (void *) sequence,
- (const void *) (sequence + 1),
- /* Accepts 0. */
- (sizeof(ZoO_index) * (ZoO_MARKOV_ORDER * 2))
- );
-
- sequence[ZoO_MARKOV_ORDER * 2] = new_word_id;
-
- if (add_word_occurrence(k, sequence) < 0)
- {
- error = -1;
-
- /* There's a pun... */
- ZoO_S_WARNING("Could not add a link between words.");
-
- return -1;
- }
-
- /*
- * Safe:
- * - next_word < words_count
- * - words_count =< ZoO_INDEX_MAX
- * ----
- * next_word < ZoO_INDEX_MAX
- */
- next_word += 1;
- new_word += 1;
- }
-
- return error;
-}
-
diff --git a/src/core/char.c b/src/core/char.c
index 39ca72e..9297643 100644
--- a/src/core/char.c
+++ b/src/core/char.c
@@ -2,6 +2,18 @@
#include "char.h"
+/* See: "char.c" */
+ZoO_char ZoO_char_to_lowercase (const ZoO_char c)
+{
+ if ((c >= 'A') && (c <= 'Z'))
+ {
+ return 'z' - ('Z' - c);
+ }
+
+ return c;
+}
+
+/* See: "char.c" */
int ZoO_char_is_banned (const ZoO_char c)
{
switch (c)
@@ -21,6 +33,7 @@ int ZoO_char_is_banned (const ZoO_char c)
}
}
+/* See: "char.c" */
int ZoO_char_is_punctuation (const ZoO_char c)
{
switch (c)
@@ -38,11 +51,14 @@ int ZoO_char_is_punctuation (const ZoO_char c)
}
}
+/* See: "char.c" */
int ZoO_word_cmp
(
const ZoO_char word_a [const static 1],
+ const size_t word_a_size,
const ZoO_char word_b [const static 1]
)
{
- return strcmp((const char *) word_a, (const char *) word_b);
+ return strncmp((const char *) word_a, (const char *) word_b, word_a_size);
}
+
diff --git a/src/core/char.h b/src/core/char.h
index 772a3a2..2b4a355 100644
--- a/src/core/char.h
+++ b/src/core/char.h
@@ -3,19 +3,42 @@
#include "char_types.h"
-enum ZoO_word_property ZoO_get_word_property
-(
- const ZoO_char word [const restrict],
- size_t word_size
-);
-
+/* Compares two words. {word_a} does not have to be null terminated. */
+/*@
+ @ requires null_terminated_string(word_b);
+ @ requires ((length(word_a) * sizeof(ZoO_char)) == word_a_size);
+ @ ensures ((\result == 1) || (\result == 0) || (\result == -1));
+ @*/
int ZoO_word_cmp
(
const ZoO_char word_a [const static 1],
+ const size_t word_a_size,
const ZoO_char word_b [const static 1]
);
+/*
+ * Returns the lowercase equivalent of ZoO_char that are included in ['A','Z'].
+ * Other ZoO_char are returned untouched.
+ */
+ZoO_char ZoO_char_to_lowercase (const ZoO_char c);
+
+/*
+ * Returns '1' iff {c} should be considered as an punctuation character, '0'
+ * otherwise.
+ */
+/*@
+ @ ensures ((\result == 1) || (\result == 0));
+ @*/
int ZoO_char_is_punctuation (const ZoO_char c);
+
+/*
+ * Returns '1' iff containing {c} means the word should not be learned. '0'
+ * otherwise.
+ */
+/*@
+ @ ensures ((\result == 1) || (\result == 0));
+ @*/
int ZoO_word_char_is_banned (const ZoO_char c);
#endif
+
diff --git a/src/core/char_types.h b/src/core/char_types.h
index 67b5294..a2a736c 100644
--- a/src/core/char_types.h
+++ b/src/core/char_types.h
@@ -1,12 +1,16 @@
#ifndef _ZoO_CORE_CHAR_TYPES_H_
#define _ZoO_CORE_CHAR_TYPES_H_
-
+/*
+ * FIXME: Does not belong here.
+ */
+/*
enum ZoO_word_property
{
ZoO_WORD_NO_PROPERTY,
ZoO_WORD_HAS_NO_LEFT_SEPARATOR,
ZoO_WORD_HAS_NO_RIGHT_SEPARATOR
};
+*/
/* ZoO_char = UTF-8 char */
typedef char ZoO_char;
diff --git a/src/core/index.c b/src/core/index.c
new file mode 100644
index 0000000..375e0ad
--- /dev/null
+++ b/src/core/index.c
@@ -0,0 +1,61 @@
+#include <limits.h>
+#include <stdlib.h>
+
+#include "index.h"
+
+#if (RAND_MAX < UCHAR_MAX)
+ #error "RAND_MAX < UCHAR_MAX, unable to generate random numbers."
+#endif
+
+#if (RAND_MAX == 0)
+ #error "RAND_MAX is included in [0, 0]. What are you even doing?"
+#endif
+
+/*
+ * Returns a random unsigned char.
+ */
+static unsigned char random_uchar (void)
+{
+ return
+ (unsigned char)
+ (
+ /* FIXME: Do floats allow enough precision for this? */
+ (
+ ((float) rand())
+ / ((float) RAND_MAX)
+ )
+ * ((float) UCHAR_MAX)
+ );
+}
+
+/* See: "index.h" */
+ZoO_index ZoO_index_random (void)
+{
+ ZoO_index i;
+ ZoO_index result;
+ unsigned char * result_bytes;
+
+ result_bytes = (unsigned char *) &result;
+
+ for (i = 0; i < sizeof(ZoO_index); ++i)
+ {
+ result_bytes[i] = random_uchar();
+ }
+
+ return result;
+}
+
+/* See: "index.h" */
+ZoO_index ZoO_index_random_up_to (const ZoO_index max)
+{
+ return
+ (ZoO_index)
+ (
+ /* FIXME: Do floats allow enough precision for this? */
+ (
+ ((float) ZoO_index_random())
+ / ((float) ZoO_INDEX_MAX)
+ )
+ * ((float) max)
+ );
+}
diff --git a/src/core/index.h b/src/core/index.h
index 76e3507..1417662 100644
--- a/src/core/index.h
+++ b/src/core/index.h
@@ -3,6 +3,17 @@
#include "index_types.h"
+/*
+ * Returns a random ZoO_index.
+ */
+ZoO_index ZoO_index_random (void);
+
+/*
+ * Returns a random ZoO_index, included in [0, limit]
+ */
+/*@
+ @ ensures (\result <= limit);
+ @*/
ZoO_index ZoO_index_random_up_to (const ZoO_index limit);
#endif
diff --git a/src/core/index_types.h b/src/core/index_types.h
index 2d769ca..ad56d52 100644
--- a/src/core/index_types.h
+++ b/src/core/index_types.h
@@ -1,8 +1,10 @@
#ifndef _ZoO_CORE_INDEX_TYPES_H_
#define _ZoO_CORE_INDEX_TYPES_H_
+/* Must be unsigned. */
typedef unsigned int ZoO_index;
+/* Must be > 0. */
#define ZoO_INDEX_MAX UINT_MAX
#endif
diff --git a/src/core/knowledge.c b/src/core/knowledge.c
deleted file mode 100644
index 94d76cd..0000000
--- a/src/core/knowledge.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#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 ****************************/
-void ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])
-{
- k->words = (struct ZoO_knowledge_word *) NULL;
- k->words_length = 0;
- k->words_sorted = (ZoO_index *) NULL;
-}
diff --git a/src/core/knowledge.h b/src/core/knowledge.h
deleted file mode 100644
index 057e436..0000000
--- a/src/core/knowledge.h
+++ /dev/null
@@ -1,101 +0,0 @@
-#ifndef _ZoO_CORE_KNOWLEDGE_H_
-#define _ZoO_CORE_KNOWLEDGE_H_
-
-#include "../core/char_types.h"
-#include "../core/index_types.h"
-
-#include "knowledge_types.h"
-
-void ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]);
-
-void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]);
-
-
-/*
- * When returning 0:
- * {word} was either added to {k} or its representation in {k} has its
- * occurrences count increased.
- * {*result} indicates where {word} is in {k->words}.
- *
- * When returning -1:
- * Something went wrong when adding the occurrence of {word} to {k}.
- * {k} remains semantically unchanged.
- * {*result} may or may not have been altered.
- */
-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]
-);
-
-int ZoO_knowledge_learn_sequence
-(
- struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
- const ZoO_index sequence_length
-);
-
-int ZoO_knowledge_get_following_sequences_ref
-(
- const struct ZoO_knowledge k [const static 1],
- const ZoO_index initial_word,
- const ZoO_index * restrict following_sequences_ref [const restrict static 1],
- const ZoO_index * restrict following_sequences_weights [const restrict static 1],
- ZoO_index following_sequences_weights_sum [const static 1]
-);
-
-int ZoO_knowledge_get_sequence
-(
- const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequences_ref,
- const ZoO_index * restrict sequence [const restrict static 1]
-);
-
-int 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],
- size_t word_size [const restrict static 1]
-);
-
-/*
- * When returning 0:
- * {word} is in {k}.
- * {word} is located at {k->words[*result]}.
- *
- * When returning -1:
- * {word} is not in {k}.
- * {*result} is where {word} was expected to be found in
- * {k->sorted_indices}.
- */
-int ZoO_knowledge_find_word_id
-(
- 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 ZoO_knowledge_find_preceding_words
-(
- 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]
-);
-
-int ZoO_knowledge_find_following_words
-(
- const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
- const ZoO_index 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]
-);
-
-#endif
diff --git a/src/core/knowledge_finalize.c b/src/core/knowledge_finalize.c
deleted file mode 100644
index e4deda6..0000000
--- a/src/core/knowledge_finalize.c
+++ /dev/null
@@ -1,121 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h> /* defines SIZE_MAX */
-
-#include "../io/error.h"
-
-#include "knowledge.h"
-
-void knowledge_sequence_collection_finalize
-(
- struct ZoO_knowledge_sequence_collection c [const restrict static 1]
-)
-{
- ZoO_index i;
-
- if (c->sequences_ref != (ZoO_index *) NULL)
- {
- free((void *) c->sequences_ref);
- c->sequences_ref = (ZoO_index *) NULL;
- }
-
- if (c->sequences_ref_sorted != (ZoO_index *) NULL)
- {
- free((void *) c->sequences_ref_sorted);
- c->sequences_ref_sorted = (ZoO_index *) NULL;
- }
-
- if (c->occurrences != (ZoO_index *) NULL)
- {
- free((void *) c->occurrences);
- c->occurrences = (ZoO_index *) NULL;
- }
-
- for (i = 0; i < c->sequences_ref_length; ++i)
- {
- free((void *) c->targets[i]);
- free((void *) c->targets_occurrences[i]);
- }
-
- c->sequences_ref_length = 0;
-
- if (c->targets != (ZoO_index **) NULL)
- {
- free((void *) c->targets);
- c->targets != (ZoO_index **) NULL;
- }
-
- free((void *) c->targets_length);
-
- if (c->targets_occurrences != (ZoO_index **) NULL)
- {
- free((void *) c->targets_occurrences);
- c->targets_occurrences != (ZoO_index **) NULL;
- }
-}
-
-void knowledge_word_finalize
-(
- struct ZoO_knowledge_word w [const restrict static 1]
-)
-{
- w->word_size = 0;
- w->occurrences = 0;
-
- if (w->word != (ZoO_char *) NULL)
- {
- free((void *) w->word);
-
- w->word = (ZoO_char *) NULL;
- }
-
- knowledge_sequence_collection_finalize(&(w->followed));
- knowledge_sequence_collection_finalize(&(w->preceded));
-}
-
-void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1])
-{
- ZoO_index i;
-
- for (i = 0; i < k->words_length; ++i)
- {
- knowledge_word_finalize(k->words + i);
- }
-
- k->words_length = 0;
-
- if (k->words != (struct ZoO_knowledge_word *) NULL)
- {
- free((void *) k->words);
-
- k->words = (struct ZoO_knowledge_word *) NULL;
- }
-
- if (k->words_sorted != (ZoO_index *) NULL)
- {
- free((void *) k->words_sorted);
-
- k->words_sorted = (ZoO_index *) NULL;
- }
-
- for (i = 0; i < k->sequences_length; ++i)
- {
- free((void *) k->sequences[i]);
- }
-
- k->sequences_length = 0;
-
- if (k->sequences != (ZoO_index **) NULL)
- {
- free((void *) k->sequences);
-
- k->sequences = (ZoO_index **) NULL;
- }
-
- if (k->sequences_sorted != (ZoO_index *) NULL)
- {
- free((void *) k->sequences_sorted);
-
- k->sequences_sorted = (ZoO_index *) NULL;
- }
-}
diff --git a/src/core/knowledge_search.c b/src/core/knowledge_search.c
deleted file mode 100644
index d0c61ef..0000000
--- a/src/core/knowledge_search.c
+++ /dev/null
@@ -1,335 +0,0 @@
-#include <stdlib.h>
-
-#include "../core/char.h"
-#include "../core/index.h"
-#include "../core/sequence.h"
-
-#include "../io/error.h"
-
-#include "knowledge.h"
-
-/* See "knowledge.h". */
-int ZoO_knowledge_find_word_id
-(
- const struct ZoO_knowledge k [const restrict static 1],
- const ZoO_char word [const restrict static 1],
- ZoO_index result [const restrict static 1]
-)
-{
- /* This is a binary search */
- int cmp;
- ZoO_index i, current_min, current_max;
- ZoO_index candidate_id;
-
- /* Handles the case where the list is empty ********************************/
- current_max = k->words_length;
-
- if (current_max == 0)
- {
- *result = 0;
-
- return -1;
- }
- /***************************************************************************/
-
- current_min = 0;
- current_max -= 1;
-
- for (;;)
- {
- i = (current_min + ((current_max - current_min) / 2));
-
- cmp = ZoO_word_cmp(word, k->words[k->words_sorted[i]].word);
-
- if (cmp > 0)
- {
- current_min = (i + 1);
-
- if (current_min > current_max)
- {
- *result = current_min;
-
- return -1;
- }
- }
- else if (cmp < 0)
- {
- if ((current_min > current_max) || (i == 0))
- {
- *result = current_min;
-
- return -1;
- }
-
- current_max = (i - 1);
- }
- else
- {
- *result = i;
-
- return 0;
- }
- }
-}
-
-int ZoO_knowledge_find_preceding_words
-(
- const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
- 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]
-)
-{
- /* This is a binary search */
- int cmp;
- 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];
-
- if (word >= k->words_length)
- {
- ZoO_S_ERROR
- (
- "Attempting to find the preceding words of an unknown word."
- );
-
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
-
- return -1;
- }
-
-
- if (markov_order == 1)
- {
- /* Special case: empty sequences. */
- *preceding_words = (const ZoO_index *) k->words[word].preceded.targets;
-
- *preceding_words_weights =
- (const ZoO_index *) k->words[word].preceded.targets_occurrences;
-
- *preceding_words_weights_sum = k->words[word].occurrences;
-
- return 0;
- }
-
- /* Handles the case where the list is empty ********************************/
- current_max = k->words[word].preceded.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;
-
- ZoO_S_ERROR
- (
- "Attempting to find the preceding words of a sequence that never had "
- "any."
- );
-
- return -2;
- }
- /***************************************************************************/
-
- current_min = 0;
- current_max -= 1;
-
- for (;;)
- {
- i = (current_min + ((current_max - current_min) / 2));
-
- local_sequence = k->words[word].preceded.sequences_ref_sorted[i];
-
- (void) ZoO_knowledge_get_sequence
- (
- k,
- k->words[word].preceded.sequences_ref[local_sequence],
- &candidate
- );
-
- cmp =
- ZoO_sequence_cmp
- (
- sequence,
- markov_sequence_length,
- candidate,
- markov_sequence_length
- );
-
- if (cmp > 0)
- {
- current_min = (i + 1);
-
- if (current_min > current_max)
- {
- *preceding_words = (const ZoO_index *) NULL;
- *preceding_words_weights = (const ZoO_index *) NULL;
- *preceding_words_weights_sum = 0;
-
- return -2;
- }
- }
- else if (cmp < 0)
- {
- 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;
-
- return -2;
- }
-
- current_max = (i - 1);
- }
- else
- {
- *preceding_words = k->words[word].preceded.targets[local_sequence];
-
- *preceding_words_weights =
- k->words[word].preceded.targets_occurrences[local_sequence];
-
- *preceding_words_weights_sum =
- k->words[word].preceded.occurrences[local_sequence];
-
- return 0;
- }
- }
-}
-
-int ZoO_knowledge_find_following_words
-(
- const struct ZoO_knowledge k [const static 1],
- const ZoO_index sequence [const restrict],
- const ZoO_index 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]
-)
-{
- /* This is a binary search */
- int cmp;
- 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 sequence_offset =
- ((sequence_length - markov_sequence_length) - 1);
- const ZoO_index word = sequence[sequence_offset];
-
- if (word >= k->words_length)
- {
- ZoO_S_ERROR
- (
- "Attempting to find the following words of an unknown word."
- );
-
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
-
- return -1;
- }
-
- if (markov_order == 1)
- {
- /* Special case: empty sequences. */
- *following_words = (const ZoO_index *) k->words[word].preceded.targets;
-
- *following_words_weights =
- (const ZoO_index *) k->words[word].preceded.targets_occurrences;
-
- *following_words_weights_sum = k->words[word].occurrences;
-
- return 0;
- }
-
- /* Handles the case where the list is empty ********************************/
- current_max = k->words[word].preceded.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;
-
- ZoO_S_WARNING
- (
- "Attempting to find the following words of a sequence that never had "
- "any."
- );
-
- return -2;
- }
- /***************************************************************************/
-
- current_min = 0;
- current_max -= 1;
-
- for (;;)
- {
- i = (current_min + ((current_max - current_min) / 2));
-
- local_sequence = k->words[word].followed.sequences_ref_sorted[i];
-
- (void) ZoO_knowledge_get_sequence
- (
- k,
- k->words[word].followed.sequences_ref[local_sequence],
- &candidate
- );
-
- cmp =
- ZoO_sequence_cmp
- (
- (sequence + sequence_offset),
- markov_sequence_length,
- candidate,
- markov_sequence_length
- );
-
- if (cmp > 0)
- {
- current_min = (i + 1);
-
- if (current_min > current_max)
- {
- *following_words = (const ZoO_index *) NULL;
- *following_words_weights = (const ZoO_index *) NULL;
- *following_words_weights_sum = 0;
-
- return -2;
- }
- }
- else if (cmp < 0)
- {
- 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;
-
- return -2;
- }
-
- current_max = (i - 1);
- }
- else
- {
- *following_words = k->words[word].followed.targets[local_sequence];
-
- *following_words_weights =
- k->words[word].followed.targets_occurrences[local_sequence];
-
- *following_words_weights_sum =
- k->words[word].followed.occurrences[local_sequence];
-
- return 0;
- }
- }
-}
diff --git a/src/core/knowledge_types.h b/src/core/knowledge_types.h
deleted file mode 100644
index acd239f..0000000
--- a/src/core/knowledge_types.h
+++ /dev/null
@@ -1,37 +0,0 @@
-#ifndef _ZoO_CORE_KNOWLEDGE_TYPES_H_
-#define _ZoO_CORE_KNOWLEDGE_TYPES_H_
-
-#include "../core/index_types.h"
-#include "../core/char_types.h"
-
-struct ZoO_knowledge_sequence_collection
-{
- ZoO_index * sequences_ref;
- ZoO_index sequences_ref_length;
- ZoO_index * sequences_ref_sorted;
- ZoO_index * occurrences;
- ZoO_index ** targets;
- ZoO_index * targets_length;
- ZoO_index ** targets_occurrences;
-};
-
-struct ZoO_knowledge_word
-{
- const ZoO_char * word;
- size_t word_size;
- ZoO_index occurrences;
- struct ZoO_knowledge_sequence_collection followed;
- struct ZoO_knowledge_sequence_collection preceded;
-};
-
-struct ZoO_knowledge
-{
- struct ZoO_knowledge_word * words;
- ZoO_index words_length;
- ZoO_index * words_sorted;
- ZoO_index ** sequences;
- ZoO_index sequences_length;
- ZoO_index * sequences_sorted;
-};
-
-#endif
diff --git a/src/core/main.c b/src/core/main.c
deleted file mode 100644
index bb4ae23..0000000
--- a/src/core/main.c
+++ /dev/null
@@ -1,436 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <signal.h>
-
-#include "../tool/strings.h"
-
-#include "../io/error.h"
-#include "../io/parameters.h"
-#include "../io/data_input.h"
-#include "../io/data_output.h"
-#include "../io/network.h"
-
-#include "knowledge.h"
-
-#include "state_types.h"
-
-static int run = 1;
-
-static void request_termination (int const signo)
-{
- if ((signo == SIGINT) || (signo == SIGTERM))
- {
- run = 0;
- }
-}
-
-static int initialize
-(
- struct ZoO_state s [const static 1],
- int const argc,
- const char * argv [const static argc]
-)
-{
- ZoO_S_DEBUG(ZoO_DEBUG_PROGRAM_FLOW, "Zero of One is initializing...");
-
- srand(time(NULL));
-
- /* prevents s [restrict] */
- if (ZoO_knowledge_initialize(&(s->knowledge)) < 0)
- {
- return -1;
- }
-
- if (ZoO_parameters_initialize(&(s->param), argc, argv) < 1)
- {
- ZoO_knowledge_finalize(&(s->knowledge));
-
- return -1;
- }
-
- return 0;
-}
-
-static int load_data_file (struct ZoO_state s [const static 1])
-{
- struct ZoO_data_input input;
- char * result;
-
- if (ZoO_data_input_open(&input, s->param.data_filename) < 0)
- {
- return -1;
- }
-
- while
- (
- ZoO_data_input_read_line
- (
- &input,
- ZoO_knowledge_punctuation_chars_count,
- ZoO_knowledge_punctuation_chars
- ) == 0
- )
- {
- (void) ZoO_knowledge_assimilate
- (
- &(s->knowledge),
- &(input.string),
- s->param.aliases_count,
- s->param.aliases
- );
- }
-
- ZoO_data_input_close(&input);
-
- return 0;
-}
-
-static int finalize (struct ZoO_state s [const static 1])
-{
- int error;
-
- ZoO_S_DEBUG(ZoO_DEBUG_PROGRAM_FLOW, "Zero of One is finalizing...");
-
- error = 0;
-
- /* prevents s [restrict] */
- ZoO_knowledge_finalize(&(s->knowledge));
-
- return error;
-}
-
-static int network_connect (struct ZoO_state s [const static 1])
-{
- return
- ZoO_network_connect
- (
- &(s->network),
- s->param.irc_server_addr,
- s->param.irc_server_port,
- s->param.irc_server_channel,
- s->param.irc_username,
- s->param.irc_realname,
- s->param.aliases[0]
- );
-}
-
-static int should_reply
-(
- struct ZoO_parameters param [const restrict static 1],
- struct ZoO_strings string [const restrict static 1],
- int should_learn [const restrict static 1]
-)
-{
- ZoO_index i, j;
-
- for (i = 0; i < param->aliases_count; ++i)
- {
- if (ZoO_IS_PREFIX(param->aliases[i], string->words[0]))
- {
- *should_learn = 0;
-
- return 1;
- }
-
- for (j = 1; j < string->words_count; ++j)
- {
- if (ZoO_IS_PREFIX(param->aliases[i], string->words[j]))
- {
- *should_learn = 1;
-
- return 1;
- }
- }
- }
-
- *should_learn = 1;
-
- return (param->reply_rate >= (rand() % 100));
-}
-
-static void handle_user_join
-(
- struct ZoO_state s [const static 1],
- struct ZoO_strings string [const restrict static 1],
- ssize_t const msg_offset,
- ssize_t const msg_size
-)
-{
- ZoO_char * line;
- ZoO_index loc;
-
- if (s->param.reply_rate < (rand() % 100))
- {
- return;
- }
-
- if
- (
- ZoO_strings_parse
- (
- string,
- (size_t) msg_size,
- (s->network.in + msg_offset),
- ZoO_knowledge_punctuation_chars_count,
- ZoO_knowledge_punctuation_chars
- ) < 0
- )
- {
- ZoO_S_DEBUG(ZoO_DEBUG_PROGRAM_FLOW, "Could not dissect join username.");
-
- return;
- }
-
- if
- (
- (
- ZoO_knowledge_find
- (
- &(s->knowledge),
- string->words[0],
- &loc
- ) < 0
- )
- || (s->knowledge.words[loc].backward_links_count <= 3)
- || (s->knowledge.words[loc].forward_links_count <= 3)
- )
- {
- if
- (
- ZoO_knowledge_extend
- (
- &(s->knowledge),
- (struct ZoO_strings *) NULL,
- 0,
- (const char **) NULL,
- &line
- ) == 0
- )
- {
- if (line[0] == ' ')
- {
- strcpy((s->network.out), (line + 1));
- }
- else
- {
- strcpy((s->network.out), line);
- }
-
- free((void *) line);
-
- ZoO_network_send(&(s->network));
- }
- }
- else
- {
- if
- (
- ZoO_knowledge_extend
- (
- &(s->knowledge),
- string,
- 0,
- (const char **) NULL,
- &line
- ) == 0
- )
- {
- if (line[0] == ' ')
- {
- strcpy((s->network.out), (line + 1));
- }
- else
- {
- strcpy((s->network.out), line);
- }
-
- free((void *) line);
-
- ZoO_network_send(&(s->network));
- }
- }
-}
-
-static void handle_message
-(
- struct ZoO_state s [const static 1],
- struct ZoO_strings string [const restrict static 1],
- ssize_t const msg_offset,
- /* FIXME: somehow we end up using (msg_size + 1), meaning there's a mixup
- * between size and length.
- */
- ssize_t const msg_size
-)
-{
- ZoO_char * line;
- int reply, learn;
-
- if
- (
- ZoO_strings_parse
- (
- string,
- (size_t) msg_size,
- (s->network.in + msg_offset),
- ZoO_knowledge_punctuation_chars_count,
- ZoO_knowledge_punctuation_chars
- ) < 0
- )
- {
- ZoO_S_DEBUG(ZoO_DEBUG_PROGRAM_FLOW, "Could not dissect msg.");
-
- return;
- }
-
- if (string->words_count == 0)
- {
- return;
- }
-
- reply = should_reply(&(s->param), string, &learn);
-
- if (learn)
- {
- /*
- * It would be best to do that after replying, but by then we no longer
- * have the string in 's->network.in'.
- */
- (void) ZoO_data_output_write_line
- (
- s->param.new_data_filename,
- (s->network.in + msg_offset),
- (size_t) (msg_size + 1)
- );
- }
-
- if
- (
- reply
- &&
- (
- ZoO_knowledge_extend
- (
- &(s->knowledge),
- string,
- s->param.aliases_count,
- s->param.aliases,
- &line
- ) == 0
- )
- )
- {
- if (line[0] == ' ')
- {
- strcpy((s->network.out), (line + 1));
- }
- else
- {
- strcpy((s->network.out), line);
- }
-
- free((void *) line);
-
- ZoO_network_send(&(s->network));
- }
-
- if (learn)
- {
- (void) ZoO_knowledge_assimilate
- (
- &(s->knowledge),
- string,
- s->param.aliases_count,
- s->param.aliases
- );
- }
-}
-
-static int main_loop (struct ZoO_state s [const static 1])
-{
- struct ZoO_strings string;
- ssize_t msg_offset, msg_size;
- enum ZoO_msg_type msg_type;
-
- msg_offset = 0;
- msg_size = 0;
-
- ZoO_strings_initialize(&string);
-
- while (run)
- {
- if
- (
- ZoO_network_receive
- (
- &(s->network),
- &msg_offset,
- &msg_size,
- &msg_type
- ) == 0
- )
- {
- switch (msg_type)
- {
- case ZoO_JOIN:
- handle_user_join(s, &string, msg_offset, msg_size);
- break;
-
- case ZoO_PRIVMSG:
- handle_message(s, &string, msg_offset, msg_size);
- break;
- }
- }
- }
-
- ZoO_strings_finalize(&string);
-
- ZoO_network_disconnect(&(s->network));
-
- return 0;
-}
-
-int main (int const argc, const char * argv [const static argc])
-{
- struct ZoO_state s;
-
- if (initialize(&s, argc, argv) < 0)
- {
- return -1;
- }
-
- if (load_data_file(&s) < 0)
- {
- goto CRASH;
- }
-
- if (network_connect(&s) < 0)
- {
- goto CRASH;
- }
-
- if (main_loop(&s) < 0)
- {
- goto CRASH;
- }
-
- (void) finalize(&s);
-
- ZoO_S_DEBUG(ZoO_DEBUG_PROGRAM_FLOW, "Zero of One terminated normally.");
-
- return 0;
-
- CRASH:
- {
- (void) finalize(&s);
-
- ZoO_S_DEBUG
- (
- ZoO_DEBUG_PROGRAM_FLOW,
- "Zero of One terminated by crashing."
- );
-
- return -1;
- }
-}
diff --git a/src/core/sequence.c b/src/core/sequence.c
index 9e370a3..d7ff9d0 100644
--- a/src/core/sequence.c
+++ b/src/core/sequence.c
@@ -5,18 +5,56 @@
#include "sequence.h"
+/*
+ * Bypass rendundant ZoO_START_OF_SEQUENCE_ID at the start of a sequence.
+ */
+/* ensures (*sequence_offset <= sequence_length) */
+static void bypass_redundant_sos
+(
+ const ZoO_index sequence [const restrict],
+ const ZoO_index sequence_length,
+ ZoO_index sequence_offset [const restrict static 1]
+)
+{
+ ZoO_index i;
+
+ *sequence_offset = 0;
+
+ for (i = 0; i < sequence_length; ++i)
+ {
+ if (sequence[i] != ZoO_START_OF_SEQUENCE_ID)
+ {
+ return;
+ }
+ else if (sequence[i] == ZoO_START_OF_SEQUENCE_ID)
+ {
+ *sequence_offset = i;
+ }
+ }
+}
+
+
/* See "sequence.h" */
int ZoO_sequence_cmp
(
const ZoO_index sequence_a [const],
- const ZoO_index sequence_a_length,
+ ZoO_index sequence_a_length,
const ZoO_index sequence_b [const],
- const ZoO_index sequence_b_length
+ ZoO_index sequence_b_length
)
{
- ZoO_index min_length;
+ ZoO_index min_length, a, b;
+ ZoO_index a_offset, b_offset;
ZoO_index i;
+ bypass_redundant_sos(sequence_a, sequence_a_length, &a_offset);
+ bypass_redundant_sos(sequence_b, sequence_b_length, &b_offset);
+
+ /*@ requires (*a_offset <= sequence_a_length) @*/
+ sequence_a_length -= a_offset;
+ /*@ requires (*b_offset <= sequence_b_length) @*/
+ sequence_b_length -= b_offset;
+
if (sequence_a_length < sequence_b_length)
{
min_length = sequence_a_length;
@@ -26,47 +64,37 @@ int ZoO_sequence_cmp
min_length = sequence_b_length;
}
+ /*@ ensures (min_length <= sequence_a_length) @*/
+ /*@ ensures (min_length <= sequence_b_length) @*/
+
for (i = 0; i < min_length; ++i)
{
- if (sequence_a[i] < sequence_b[i])
+ /*@ requires ((i + a_offset) < sequence_a_length) @*/
+ a = sequence_a[i + a_offset];
+ /*@ requires ((i + b_offset) < sequence_b_length) @*/
+ b = sequence_b[i + b_offset];
+
+ if (a < b)
{
return -1;
}
- else if (sequence_b[i] > sequence_b[i])
+ else if (b > a)
{
return 1;
}
- else if
- (
- (sequence_a[i] == ZoO_END_OF_SEQUENCE_ID)
- && (sequence_b[i] == ZoO_END_OF_SEQUENCE_ID)
- )
+ else if ((a == ZoO_END_OF_SEQUENCE_ID) && (b == ZoO_END_OF_SEQUENCE_ID))
{
return 0;
}
}
- if (sequence_a_length < sequence_b_length)
+ if (sequence_a_length > sequence_b_length)
{
- if (sequence_b[i] == ZoO_END_OF_SEQUENCE_ID)
- {
- return 0;
- }
- else
- {
- return -1;
- }
+ return 1;
}
- else if (sequence_a_length > sequence_b_length)
+ else if (sequence_a_length < sequence_b_length)
{
- if (sequence_a[i] == ZoO_END_OF_SEQUENCE_ID)
- {
- return 0;
- }
- else
- {
- return 1;
- }
+ return -1;
}
else
{
diff --git a/src/core/sequence.h b/src/core/sequence.h
index e609b4d..77ecd6c 100644
--- a/src/core/sequence.h
+++ b/src/core/sequence.h
@@ -1,11 +1,21 @@
#ifndef _ZoO_CORE_SEQUENCE_H_
#define _ZoO_CORE_SEQUENCE_H_
+#include "../core/char_types.h"
#include "../core/index_types.h"
-#include "../core/knowledge_types.h"
+#include "../knowledge/knowledge_types.h"
#include "sequence_types.h"
+int ZoO_sequence_from_undercase_string
+(
+ const ZoO_char string [const restrict],
+ const ZoO_index string_length,
+ struct ZoO_knowledge k [const restrict static 1],
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1]
+);
+
/*
* Creates a sequence containing {initial_word}. The remaining elements of
* sequence are added according to what is known to {k} as being possible.
@@ -42,7 +52,13 @@ int ZoO_sequence_create_from
* ZoO_END_OF_SEQUENCE marks the ending of a sequence, regardless of indicated
* sequence length, meaning that [10][ZoO_END_OF_SEQUENCE][9] and
* [10][ZoO_END_OF_SEQUENCE][8] are considered equal. Sequences do not have to
- * contain ZoO_END_OF_SEQUENCE.
+ * contain ZoO_END_OF_SEQUENCE. [10][ZoO_END_OF_SEQUENCE] and [10] are
+ * considered different, [10][ZoO_END_OF_SEQUENCE]
+ * and [10][ZoO_END_OF_SEQUENCE][ZoO_END_OF_SEQUENCE] are considered equal.
+ * Same logic is applyied for ZoO_START_OF_SEQUENCE:
+ * [START_OF_SEQUENCE][10] is not [10], but
+ * [START_OF_SEQUENCE][START_OF_SEQUENCE][10] and [START_OF_SEQUENCE][10] are
+ * the same.
* Return:
* 1 iff {sequence_a} should be considered being more than {sequence_b}
* 0 iff {sequence_a} should be considered being equal to {sequence_b}
diff --git a/src/core/sequence_creation.c b/src/core/sequence_creation.c
index 1133be9..f460629 100644
--- a/src/core/sequence_creation.c
+++ b/src/core/sequence_creation.c
@@ -19,6 +19,11 @@
* (> weights_sum 0).
* (= (sum weights) weights_sum).
*/
+/*@
+ @ requires (weights_sum > 0);
+ @ requires \valid(weights);
+ @ requires (\sum(0, (\length(weights) - 1), weights) = weights_sum);
+@*/
static ZoO_index weighted_random_pick
(
const ZoO_index weights [const restrict static 1],
@@ -29,12 +34,12 @@ static ZoO_index weighted_random_pick
accumulator = 0;
- /* Safe: Included in [0, weights_sum]. */
random_number = ZoO_index_random_up_to(weights_sum);
+ /*@ ensures (0 <= random_number <= weights_sum); @*/
for (result = 0; accumulator < random_number; ++result)
{
- /* Safe: (= (sum weights) weights_sum) */
+ /*@ requires (\sum(0, (\length(weights) - 1), weights) = weights_sum); @*/
accumulator += weights[result];
}
diff --git a/src/core/sequence_from_string.c b/src/core/sequence_from_string.c
new file mode 100644
index 0000000..51d7049
--- /dev/null
+++ b/src/core/sequence_from_string.c
@@ -0,0 +1,315 @@
+#define _POSIX_C_SOURCE 200809L
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h> /* defines SIZE_MAX */
+
+#include "../core/char.h"
+#include "../core/index.h"
+
+#include "../cli/cli.h"
+
+#include "../knowledge/knowledge.h"
+
+#include "sequence.h"
+
+static int add_word_id_to_sequence
+(
+ const ZoO_index word_id,
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1]
+)
+{
+ ZoO_index * new_sequence;
+
+ *sequence_length += 1;
+
+ new_sequence =
+ (ZoO_index *) realloc
+ (
+ (void *) *sequence,
+ (((size_t) sequence_length) * sizeof(ZoO_index))
+ );
+
+ if (new_sequence == (ZoO_index *) NULL)
+ {
+ ZoO_S_ERROR("Unable to reallocate a sequence to add word ids to it.");
+
+ return -1;
+ }
+
+ return 0;
+}
+
+/******************************************************************************/
+/** HANDLING PUNCTUATION ******************************************************/
+/******************************************************************************/
+static int add_punctuation_to_sequence
+(
+ const ZoO_char string [const restrict static 1],
+ const ZoO_char punctuation,
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1],
+ const struct ZoO_knowledge k [const restrict static 1]
+)
+{
+ ZoO_index word_id;
+ ZoO_char as_word[2];
+
+ as_word[0] = punctuation;
+ as_word[1] = '\0';
+
+ if (ZoO_knowledge_find_word_id(k, as_word, 2, &word_id) < 0)
+ {
+ ZoO_PROG_ERROR
+ (
+ "'%s' was defined as a punctuation, was found in a string, yet is not"
+ " defined in the knowledge database.",
+ as_word
+ );
+
+ return -1;
+ }
+
+ if (add_word_id_to_sequence(word_id, sequence, sequence_length) < 0)
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int word_is_punctuation_terminated
+(
+ const ZoO_char string [const restrict static 1],
+ const ZoO_index word_start,
+ const ZoO_index word_length
+)
+{
+ return ZoO_char_is_punctuation(string[word_length]);
+}
+
+/******************************************************************************/
+/** HANDLING WORDS ************************************************************/
+/******************************************************************************/
+static int add_word_to_sequence
+(
+ const ZoO_char string [const restrict static 1],
+ const ZoO_index word_start,
+ const ZoO_index word_length,
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1],
+ struct ZoO_knowledge k [const restrict static 1]
+)
+{
+ ZoO_index word_id;
+ ZoO_char * stored_word;
+
+ if (word_length == 0)
+ {
+ return 0;
+ }
+
+ if
+ (
+ ZoO_knowledge_learn_word
+ (
+ k,
+ (string + word_start),
+ word_length,
+ &word_id
+ ) < 0
+ )
+ {
+ return -1;
+ }
+
+ if (add_word_id_to_sequence(word_id, sequence, sequence_length) < 0)
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int add_finding_to_sequence
+(
+ const ZoO_char string [const restrict static 1],
+ const ZoO_index word_start,
+ const ZoO_index word_length,
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1],
+ struct ZoO_knowledge k [const restrict static 1]
+)
+{
+ ZoO_index punctuation;
+
+ if (word_is_punctuation_terminated(string, word_start, word_length))
+ {
+ punctuation = 1;
+ }
+ else
+ {
+ punctuation = 0;
+ }
+
+ if
+ (
+ add_word_to_sequence
+ (
+ string,
+ word_start,
+ (word_length - punctuation),
+ sequence,
+ sequence_length,
+ k
+ ) < 0
+ )
+ {
+ return -1;
+ }
+
+ if
+ (
+ (punctuation == 1)
+ &&
+ (
+ add_punctuation_to_sequence
+ (
+ string,
+ string[word_start + word_length - 1],
+ sequence,
+ sequence_length,
+ k
+ ) < 0
+ )
+ )
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int find_word
+(
+ const ZoO_char string [const restrict static 1],
+ const ZoO_index string_length,
+ const ZoO_index offset,
+ ZoO_index word_start [const restrict static 1],
+ ZoO_index word_length [const restrict static 1]
+)
+{
+ ZoO_index i;
+
+ i = offset;
+
+ while ((string[i] == ' ') && (i < string_length))
+ {
+ i += 1;
+ }
+
+ if (i >= string_length)
+ {
+ return -1;
+ }
+
+ *word_start = i;
+
+ while ((string[i] != ' ') && (i < string_length))
+ {
+ i += 1;
+ }
+
+ if (i >= string_length)
+ {
+ return -1;
+ }
+
+ *word_length = (i - *word_start);
+
+ return 0;
+}
+
+/******************************************************************************/
+/** EXPORTED ******************************************************************/
+/******************************************************************************/
+int ZoO_sequence_from_undercase_string
+(
+ const ZoO_char string [const restrict],
+ const ZoO_index string_length,
+ struct ZoO_knowledge k [const restrict static 1],
+ ZoO_index * sequence [const restrict static 1],
+ ZoO_index sequence_length [const restrict static 1]
+)
+{
+ ZoO_index word_start, word_length;
+ ZoO_index i;
+
+ i = 0;
+
+ *sequence = (ZoO_index *) NULL;
+ *sequence_length = 0;
+
+ if
+ (
+ add_word_id_to_sequence
+ (
+ ZoO_START_OF_SEQUENCE_ID,
+ sequence,
+ sequence_length
+ ) < 0
+ )
+ {
+ return -1;
+ }
+
+ while (i < string_length)
+ {
+ if (find_word(string, i, string_length, &word_start, &word_length) < 0)
+ {
+ break;
+ }
+
+ if
+ (
+ add_finding_to_sequence
+ (
+ string,
+ word_start,
+ word_length,
+ sequence,
+ sequence_length,
+ k
+ ) < 0
+ )
+ {
+ free((void *) *sequence);
+ *sequence = (ZoO_index *) NULL;
+ *sequence_length = 0;
+
+ return -1;
+ }
+
+ i = (word_start + word_length);
+ }
+
+ if
+ (
+ add_word_id_to_sequence
+ (
+ ZoO_END_OF_SEQUENCE_ID,
+ sequence,
+ sequence_length
+ ) < 0
+ )
+ {
+ free((void *) *sequence);
+
+ *sequence = (ZoO_index *) NULL;
+ *sequence_length = 0;
+
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/src/core/sequence_types.h b/src/core/sequence_types.h
index 717d418..c260a8a 100644
--- a/src/core/sequence_types.h
+++ b/src/core/sequence_types.h
@@ -3,7 +3,8 @@
#define ZoO_START_OF_SEQUENCE_ID 0
#define ZoO_END_OF_SEQUENCE_ID 1
+#define ZoO_ACTION_SEQUENCE_ID 2
-#define ZoO_RESERVED_IDS_COUNT 2
+#define ZoO_RESERVED_IDS_COUNT 3
#endif