| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/assimilate.c | 26 | ||||
| -rw-r--r-- | src/core/create_sentences.c | 107 | ||||
| -rw-r--r-- | src/core/knowledge.c | 7 | ||||
| -rw-r--r-- | src/core/knowledge.h | 3 | ||||
| -rw-r--r-- | src/core/knowledge_types.h | 2 | ||||
| -rw-r--r-- | src/core/main.c | 5 | ||||
| -rw-r--r-- | src/core/sequence.c | 8 | ||||
| -rw-r--r-- | src/pervasive.h | 2 | 
8 files changed, 111 insertions, 49 deletions
| diff --git a/src/core/assimilate.c b/src/core/assimilate.c index 49efe23..7f03e1b 100644 --- a/src/core/assimilate.c +++ b/src/core/assimilate.c @@ -21,7 +21,16 @@ static int add_sequence     struct ZoO_knowledge_link * link;     ZoO_index * new_p; -   if (ZoO_knowledge_get_link(links_count, links, (sequence + offset), &link_index) < 0) +   if +   ( +      ZoO_knowledge_get_link +      ( +         links_count, +         links, +         (sequence + offset), +         &link_index +      ) < 0 +   )     {        return -1;     } @@ -153,21 +162,22 @@ static int init_sequence  {     ZoO_index i; -   sequence[0] = ZoO_WORD_START_OF_LINE; +   /* We are going to link this sequence to ZoO_WORD_START_OF_LINE */ +   sequence[ZoO_MARKOV_ORDER] = ZoO_WORD_START_OF_LINE; -   for (i = 0; i < ZoO_MARKOV_ORDER; ++i) +   for (i = 1; i <= ZoO_MARKOV_ORDER; ++i)     {        sequence[ZoO_MARKOV_ORDER - i] = ZoO_WORD_START_OF_LINE; -      if (i < string->words_count) +      if (i <= string->words_count)        {           if           (              ZoO_knowledge_learn              (                 k, -               string->words[i], -               (sequence + (ZoO_MARKOV_ORDER + i + 1)) +               string->words[i - 1], +               (sequence + (ZoO_MARKOV_ORDER + i))              ) < 0           )           { @@ -176,7 +186,7 @@ static int init_sequence        }        else        { -         sequence[ZoO_MARKOV_ORDER + i + 1] = ZoO_WORD_END_OF_LINE; +         sequence[ZoO_MARKOV_ORDER + i] = ZoO_WORD_END_OF_LINE;        }     } @@ -220,7 +230,7 @@ int ZoO_knowledge_assimilate     next_word = 0;     new_word = ZoO_MARKOV_ORDER; -   while (next_word <= string->words_count) +   while (next_word <= (string->words_count + ZoO_MARKOV_ORDER))     {        if (new_word < string->words_count)        { diff --git a/src/core/create_sentences.c b/src/core/create_sentences.c index a3640dc..93444a7 100644 --- a/src/core/create_sentences.c +++ b/src/core/create_sentences.c @@ -384,10 +384,11 @@ static ZoO_index select_first_word  (     struct ZoO_knowledge k [const static 1],     const struct ZoO_strings string [const], -   int const ignore_first_word +   ZoO_index const aliases_count, +   const char * restrict aliases [const restrict static aliases_count]  )  { -   ZoO_index i, word_id, word_min_score, word_min_id; +   ZoO_index i, j, word_id, word_min_score, word_min_id;     ZoO_index word_found;     if (string == (struct ZoO_strings *) NULL) @@ -395,19 +396,18 @@ static ZoO_index select_first_word        return word_min_id = (rand() % k->words_count);     } -   if (ignore_first_word) -   { -      i = 1; -   } -   else -   { -      i = 0; -   } -     word_found = 0; -   for (; i < string->words_count; ++i) +   for (i = 0; i < string->words_count; ++i)     { +      for (j = 0; j < aliases_count; ++j) +      { +         if (ZoO_IS_PREFIX(aliases[j], string->words[i])) +         { +            goto NEXT_WORD; +         } +      } +        /* prevents k [restrict] */        if (ZoO_knowledge_find(k, string->words[i], &word_min_id) == 0)        { @@ -416,6 +416,8 @@ static ZoO_index select_first_word           break;        } + +      NEXT_WORD:;     }     if (word_found == 0) @@ -425,6 +427,14 @@ static ZoO_index select_first_word     for (; i < string->words_count; ++i)     { +      for (j = 0; j < aliases_count; ++j) +      { +         if (ZoO_IS_PREFIX(aliases[j], string->words[i])) +         { +            goto NEXT_WORD_BIS; +         } +      } +        if        (           (ZoO_knowledge_find(k, string->words[i], &word_id) == 0) @@ -434,6 +444,8 @@ static ZoO_index select_first_word           word_min_score = k->words[word_id].occurrences;           word_min_id = word_id;        } + +      NEXT_WORD_BIS:;     }     return word_min_id; @@ -443,14 +455,21 @@ static void init_sequence  (     struct ZoO_knowledge k [const static 1],     const struct ZoO_strings string [const], -   int const ignore_first_word, +   ZoO_index const aliases_count, +   const char * restrict aliases [const restrict static aliases_count],     ZoO_index sequence [const static (ZoO_MARKOV_ORDER * 2) + 1]  )  {     ZoO_index i, j, accumulator, random_number;     struct ZoO_knowledge_word * fiw; -   sequence[ZoO_MARKOV_ORDER] = select_first_word(k, string, ignore_first_word); +   sequence[ZoO_MARKOV_ORDER] = +      select_first_word( +         k, +         string, +         aliases_count, +         aliases +      );     fiw = (k->words + sequence[ZoO_MARKOV_ORDER]); @@ -459,25 +478,31 @@ static void init_sequence        sequence[ZoO_MARKOV_ORDER - i - 1] = ZoO_WORD_START_OF_LINE;        sequence[ZoO_MARKOV_ORDER + i + 1] = ZoO_WORD_END_OF_LINE;     } -/* + +   if (fiw->forward_links_count == 0) +   { +      ZoO_S_FATAL("First word has no forward links."); + +      return; +   } + +   /* Chooses a likely forward link for the pillar. */     i = 0; -   accumulator = 0; +   accumulator = fiw->forward_links[0].occurrences;     random_number = (((ZoO_index) rand()) % fiw->occurrences);     while (accumulator < random_number)     { -      i += 1;        accumulator += fiw->forward_links[i].occurrences; -   } -*/ -   if (fiw->forward_links_count == 0) -   { -      ZoO_S_FATAL("First word has no forward links."); +      i += 1;     } -   i = (((ZoO_index) rand()) % fiw->forward_links_count); +/*   i = (((ZoO_index) rand()) % fiw->forward_links_count); */ +   /* Copies the forward link data into the sequence. */ +   /* This adds (ZoO_MARKOV_ORDER - 1) words, as the ZoO_MARKOV_ORDERth word */ +   /* is chosen aftewards. */     memcpy     (        (void *) (sequence + ZoO_MARKOV_ORDER + 1), @@ -485,6 +510,7 @@ static void init_sequence        sizeof(ZoO_index) * (ZoO_MARKOV_ORDER - 1)     ); +   /* selects the last word */     sequence[ZoO_MARKOV_ORDER * 2] =        fiw->forward_links[i].targets        [ @@ -495,28 +521,47 @@ static void init_sequence           )        ]; -   for (i = 1; i <= ZoO_MARKOV_ORDER; ++i) +   /* FIXME: Not clear enough. */ +   /* Now that we have the right side of the sequence, we are going to */ +   /* build the left one, one word at a time. */ +   for (i = 0; i < ZoO_MARKOV_ORDER; ++i)     { -      fiw = (k->words + sequence[(ZoO_MARKOV_ORDER * 2) - i]); +      /* temporary pillar (starts on the right side, minus one so we don't */ +      fiw = (k->words + sequence[(ZoO_MARKOV_ORDER * 2) - i - 1]);        if        ( +         /* finds the backward link corresponding to the words left of the */ +         /* temporary pillar. */           ZoO_knowledge_find_link           (              fiw->backward_links_count,              fiw->backward_links, -            sequence + (ZoO_MARKOV_ORDER - i + 1), +            sequence + (ZoO_MARKOV_ORDER - i),              &j           )           < 0        )        { -         ZoO_S_ERROR("Unexpectedly, no back link was found."); +         ZoO_ERROR +         ( +            "Unexpectedly, no back link was found at i=%u, expected to find" +            "a backlink with %s, from %s.", +            i, +            k->words[sequence[(ZoO_MARKOV_ORDER - i)]].word, +            fiw->word +         ); +         ZoO_S_ERROR("Sequence was:"); + +         for (j = 0; j <= (ZoO_MARKOV_ORDER * 2); ++j) +         { +            ZoO_ERROR("[%u] %s", j, k->words[sequence[j]].word); +         }           break;        } -      sequence[ZoO_MARKOV_ORDER - i] = +      sequence[ZoO_MARKOV_ORDER - i - 1] =           fiw->backward_links[j].targets           [              pick_index @@ -532,7 +577,8 @@ int ZoO_knowledge_extend  (     struct ZoO_knowledge k [const static 1],     const struct ZoO_strings string [const], -   int const ignore_first_word, +   ZoO_index const aliases_count, +   const char * restrict aliases [const restrict static aliases_count],     ZoO_char * result [const static 1]  )  { @@ -543,7 +589,8 @@ int ZoO_knowledge_extend     credits = ZoO_MAX_REPLY_WORDS; -   init_sequence(k, string, ignore_first_word, sequence); +   init_sequence(k, string, aliases_count, aliases, sequence); +     first_word = sequence[ZoO_MARKOV_ORDER];     /* 3: 2 spaces + '\0' */ diff --git a/src/core/knowledge.c b/src/core/knowledge.c index a2bf708..4980fdd 100644 --- a/src/core/knowledge.c +++ b/src/core/knowledge.c @@ -10,8 +10,8 @@  /** Basic functions of the ZoO_knowledge structure ****************************/  /* XXX: are we as close to immutable as we want to be? */ -unsigned int const ZoO_knowledge_punctuation_chars_count = 7; -const ZoO_char const ZoO_knowledge_punctuation_chars[7] = +unsigned int const ZoO_knowledge_punctuation_chars_count = 8; +const ZoO_char const ZoO_knowledge_punctuation_chars[8] =     {        '!',        ',', @@ -19,7 +19,8 @@ const ZoO_char const ZoO_knowledge_punctuation_chars[7] =        ':',        ';',        '?', -      '~' +      '~', +      '\001'     };  /* XXX: are we as close to immutable as we want to be? */ diff --git a/src/core/knowledge.h b/src/core/knowledge.h index 93f5f49..7b5d754 100644 --- a/src/core/knowledge.h +++ b/src/core/knowledge.h @@ -71,7 +71,8 @@ int ZoO_knowledge_extend  (     struct ZoO_knowledge k [const static 1],     const struct ZoO_strings string [const], -   int const ignore_first_word, +   ZoO_index const aliases_count, +   const char * restrict aliases [const restrict static aliases_count],     ZoO_char * result [const static 1]  ); diff --git a/src/core/knowledge_types.h b/src/core/knowledge_types.h index 8f541e7..e92b5e1 100644 --- a/src/core/knowledge_types.h +++ b/src/core/knowledge_types.h @@ -16,7 +16,7 @@  /* XXX: are we as close to immutable as we want to be? */  extern unsigned int const ZoO_knowledge_punctuation_chars_count; -extern const ZoO_char const ZoO_knowledge_punctuation_chars[7]; +extern const ZoO_char const ZoO_knowledge_punctuation_chars[8];  extern unsigned int const ZoO_knowledge_forbidden_chars_count;  extern const ZoO_char const ZoO_knowledge_forbidden_chars[8]; diff --git a/src/core/main.c b/src/core/main.c index 15df961..bb4ae23 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -204,6 +204,7 @@ static void handle_user_join              &(s->knowledge),              (struct ZoO_strings *) NULL,              0, +            (const char **) NULL,              &line           ) == 0        ) @@ -231,6 +232,7 @@ static void handle_user_join              &(s->knowledge),              string,              0, +            (const char **) NULL,              &line           ) == 0        ) @@ -312,7 +314,8 @@ static void handle_message           (              &(s->knowledge),              string, -            !learn, +            s->param.aliases_count, +            s->param.aliases,              &line           ) == 0        ) diff --git a/src/core/sequence.c b/src/core/sequence.c index 0f4043e..67174d1 100644 --- a/src/core/sequence.c +++ b/src/core/sequence.c @@ -14,11 +14,11 @@ static int cmp_seq_link  )  {     ZoO_index j; -   ZoO_index * sequence; -   struct ZoO_knowledge_link * link; +   const ZoO_index * sequence; +   const struct ZoO_knowledge_link * link; -   sequence = (ZoO_index *) a; -   link = (struct ZoO_knowledge_link *) b; +   sequence = (const ZoO_index *) a; +   link = (const struct ZoO_knowledge_link *) b;     for (j = 0; j < ZoO_SEQUENCE_SIZE; ++j)     { diff --git a/src/pervasive.h b/src/pervasive.h index 4cc43fe..f1dd5af 100644 --- a/src/pervasive.h +++ b/src/pervasive.h @@ -40,7 +40,7 @@  #endif  #ifndef ZoO_MARKOV_ORDER -   #define ZoO_MARKOV_ORDER               2 +   #define ZoO_MARKOV_ORDER               3  #endif  typedef unsigned int ZoO_index; | 


