diff options
Diffstat (limited to 'src/markov.h')
-rw-r--r-- | src/markov.h | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/src/markov.h b/src/markov.h index 052af53..2fdaf16 100644 --- a/src/markov.h +++ b/src/markov.h @@ -5,7 +5,7 @@ #include <stdlib.h> #include <assert.h> #include <string.h> -#include <time.h> +#include <ctype.h> extern int ITEMS; extern double chain[][ITEM_CAP]; @@ -22,29 +22,43 @@ void generate_chain(char *file_path) int items_seen[ITEM_CAP][ITEM_CAP] = {0}; int cur_item = -1; - char line[256] = {0}; - while((fgets(line, sizeof(line), fp)) != NULL) + char word[256] = {0}; + char ch; + while((ch = fgetc(fp)) != EOF) { - if(line[strlen(line)-1] == '\n') - line[strlen(line)-1] = '\0'; + if(ch != ' ' && ch != ',' && ch != '"' && + ch != '\n' && ch != '[' && ch != ']' && + ch != '!' && ch != '?' && ch != '.') { + word[strlen(word)] = tolower(ch); + continue; + } + + if(strlen(word) == 0) goto manage_cur_item; int item = ITEMS; - for(int i = 0; i < ITEMS; i++) - { - if(strcmp(line, item_names[i]) == 0) + for(int i = 0; i < ITEMS; i++) { + if(strcmp(word, item_names[i]) == 0) item = i; } if(item == ITEMS) { ITEMS++; - strcpy(item_names[item], line); + strcpy(item_names[item], word); } if(cur_item != -1) items_seen[cur_item][item]++; cur_item = item; + + manage_cur_item: + + if(ch == '\n' || ch == '[' || ch == ']' || + ch == '!' || ch == '?' || ch == '.') + cur_item = -1; + + memset(word, 0, sizeof(word)); } fclose(fp); @@ -90,17 +104,34 @@ void take_walk() for(size_t i = 0; i < WALK_LEN; i++) { #ifdef PRINT_ON_WALK - puts(item_names[cur_item]); + printf("%s ", item_names[cur_item]); #endif #ifdef SAVE_TO_FILE_ON_WALK - fprintf(fp, "%s\n", item_names[cur_item]); + fprintf(fp, "%s ", item_names[cur_item]); #endif + items_seen[cur_item]++; int ni = walk_step(cur_item); - assert(ni != -1); + cur_item = ni; + + if(ni == -1) + { + cur_item = rand() % ITEMS; + + #ifdef PRINT_ON_WALK + printf("\n"); + #endif + #ifdef SAVE_TO_FILE_ON_WALK + fprintf(fp, "\n"); + #endif + } } +#ifdef PRINT_ON_WALK + printf("\n"); +#endif + #ifdef SAVE_TO_FILE_ON_WALK fclose(fp); printf("Saved file with %d items at %s\n", WALK_LEN, SAVE_TO_FILE_ON_WALK); |