aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2016-06-08 17:58:56 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2016-06-08 17:58:56 +0200
commit16265ec44520c3926c11127d0a6a6bac82a53275 (patch)
treee54aeed621509655e0e5a5c8a0d1ef3bc50be7f9
parentd29327bdc629dfa37360465b03ef45d205e7a225 (diff)
downloadzero-of-one-16265ec44520c3926c11127d0a6a6bac82a53275.zip
zero-of-one-16265ec44520c3926c11127d0a6a6bac82a53275.tar.bz2
ZoO now appends anything it learns to a file.
Also, removes the "pointing to [...]" message, which was used for debug purposes.
-rw-r--r--src/core/create_sentences.c6
-rw-r--r--src/core/main.c109
-rw-r--r--src/io/CMakeLists.txt1
-rw-r--r--src/io/network.c15
-rw-r--r--src/io/parameters.c33
-rw-r--r--src/io/parameters_types.h1
6 files changed, 144 insertions, 21 deletions
diff --git a/src/core/create_sentences.c b/src/core/create_sentences.c
index e999199..6b69111 100644
--- a/src/core/create_sentences.c
+++ b/src/core/create_sentences.c
@@ -307,12 +307,6 @@ static unsigned char * extend_right
switch (w->special)
{
case ZoO_WORD_REMOVES_LEFT_SPACE:
- printf
- (
- "current sentence:'%s', pointing at '%c'.\n",
- current_sentence,
- current_sentence[*sentence_size - addition_size - 2]
- );
current_sentence[*sentence_size - addition_size - 2] = '\0';
case ZoO_WORD_HAS_NO_EFFECT:
diff --git a/src/core/main.c b/src/core/main.c
index 1a8a40f..15df961 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -9,9 +9,9 @@
#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"
@@ -152,15 +152,52 @@ static int should_reply
static void handle_user_join
(
- struct ZoO_state s [const static 1]
+ 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
(
- (s->param.reply_rate >= (rand() % 100))
- &&
+ 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
(
@@ -170,20 +207,47 @@ static void handle_user_join
&line
) == 0
)
- )
- {
- if (line[0] == ' ')
{
- strcpy((s->network.out), (line + 1));
+ if (line[0] == ' ')
+ {
+ strcpy((s->network.out), (line + 1));
+ }
+ else
+ {
+ strcpy((s->network.out), line);
+ }
+
+ free((void *) line);
+
+ ZoO_network_send(&(s->network));
}
- else
+ }
+ else
+ {
+ if
+ (
+ ZoO_knowledge_extend
+ (
+ &(s->knowledge),
+ string,
+ 0,
+ &line
+ ) == 0
+ )
{
- strcpy((s->network.out), line);
- }
+ if (line[0] == ' ')
+ {
+ strcpy((s->network.out), (line + 1));
+ }
+ else
+ {
+ strcpy((s->network.out), line);
+ }
- free((void *) line);
+ free((void *) line);
- ZoO_network_send(&(s->network));
+ ZoO_network_send(&(s->network));
+ }
}
}
@@ -192,6 +256,9 @@ 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
)
{
@@ -222,6 +289,20 @@ static void handle_message
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
@@ -290,7 +371,7 @@ static int main_loop (struct ZoO_state s [const static 1])
switch (msg_type)
{
case ZoO_JOIN:
- handle_user_join(s);
+ handle_user_join(s, &string, msg_offset, msg_size);
break;
case ZoO_PRIVMSG:
diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt
index a13154b..c36413a 100644
--- a/src/io/CMakeLists.txt
+++ b/src/io/CMakeLists.txt
@@ -3,6 +3,7 @@ set(
${CMAKE_CURRENT_SOURCE_DIR}/parameters.c
${CMAKE_CURRENT_SOURCE_DIR}/network.c
${CMAKE_CURRENT_SOURCE_DIR}/data_input.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/data_output.c
)
set(SRC_FILES ${SRC_FILES} PARENT_SCOPE)
diff --git a/src/io/network.c b/src/io/network.c
index 45be5fb..0c9cf33 100644
--- a/src/io/network.c
+++ b/src/io/network.c
@@ -436,6 +436,21 @@ READ_NEW_MSG:
if (ZoO_IS_PREFIX("JOIN", (net->in + cmd)))
{
+ for (i = 1; (i < 512) && (net->in[i] != '!'); ++i)
+ {
+ }
+
+ if ((i == 512) || (i == 1))
+ {
+ ZoO_ERROR("Could not find JOIN username: %s", net->in);
+
+ goto READ_NEW_MSG;
+ }
+
+ *msg_offset = 1;
+ *msg_size = (i - 1);
+ net->in[i] = '\0';
+
*type = ZoO_JOIN;
return 0;
diff --git a/src/io/parameters.c b/src/io/parameters.c
index 0f7d05c..77c33aa 100644
--- a/src/io/parameters.c
+++ b/src/io/parameters.c
@@ -14,7 +14,8 @@ static void load_default_parameters
struct ZoO_parameters param [const restrict static 1]
)
{
- param->data_filename = ZoO_DEFAULT_DATA_FILENAME;
+ param->data_filename = ZoO_DEFAULT_DATA_FILENAME;
+ param->new_data_filename = (char *) NULL;
param->irc_server_addr = ZoO_DEFAULT_IRC_SERVER_ADDR;
param->irc_server_port = ZoO_DEFAULT_IRC_SERVER_PORT;
@@ -39,6 +40,9 @@ static void print_help (const char exec [const restrict static 1])
" [--data-filename | -df] FILENAME\n"
" Learn content from FILENAME before connecting.\n"
" Default: %s.\n"
+ " [--new-data-filename | -ndf] FILENAME\n"
+ " Store new data learned in FILENAME.\n"
+ " Default: value of the --data-filename param.\n"
" [--irc-server-addr | -isa] IRC_SERVER_ADDR\n"
" Connect to this server address.\n"
" Default: %s.\n"
@@ -190,6 +194,28 @@ int ZoO_parameters_initialize
}
else if
(
+ (strcmp(argv[i], "--new-data-filename") == 0)
+ || (strcmp(argv[i], "-ndf") == 0)
+ )
+ {
+ i += 1;
+
+ if
+ (
+ parse_string_arg
+ (
+ &(param->new_data_filename),
+ i,
+ argv,
+ argc
+ ) < 0
+ )
+ {
+ return -1;
+ }
+ }
+ else if
+ (
(strcmp(argv[i], "--irc-server-addr") == 0)
|| (strcmp(argv[i], "-isa") == 0)
)
@@ -350,5 +376,10 @@ int ZoO_parameters_initialize
param->aliases_count = (argc - i);
param->aliases = (argv + i);
+ if (param->new_data_filename == (char *) NULL)
+ {
+ param->new_data_filename = param->data_filename;
+ }
+
return 1;
}
diff --git a/src/io/parameters_types.h b/src/io/parameters_types.h
index 6d511d8..92a9e30 100644
--- a/src/io/parameters_types.h
+++ b/src/io/parameters_types.h
@@ -4,6 +4,7 @@
struct ZoO_parameters
{
const char * restrict data_filename;
+ const char * restrict new_data_filename;
const char * restrict irc_server_addr;
const char * restrict irc_server_port;