aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-24 16:48:07 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-24 16:48:07 +0300
commite25536a4df3e6ffaa80abdcb34df2382213a725a (patch)
tree69f1190a290298758fd2001e7cf8608455a8da88
parent104a6da9210a0c2ea2a6a8edf93fe6b7cc93680b (diff)
works better
-rwxr-xr-xbuild.sh2
-rw-r--r--src/gen_chain.c11
-rw-r--r--src/main.c13
-rw-r--r--src/markov.h55
4 files changed, 57 insertions, 24 deletions
diff --git a/build.sh b/build.sh
index 988ee24..f1befea 100755
--- a/build.sh
+++ b/build.sh
@@ -14,7 +14,7 @@ function __run__ {
function __clean__ {
rm -rf $BIN
- rm -rf files
+ rm -rf $FILES/*.txt
kill $( ps -q $$ -o pgid= ) # exit
}
diff --git a/src/gen_chain.c b/src/gen_chain.c
index 636d3ea..0ad13d1 100644
--- a/src/gen_chain.c
+++ b/src/gen_chain.c
@@ -1,9 +1,10 @@
#include <stdio.h>
+#include <time.h>
#define PRINT_ITEM_FREQUENCY
// #define PRINT_ON_WALK
-#define SAVE_TO_FILE_ON_WALK "files/chain.txt"
-#define WALK_LEN 100000
+#define SAVE_TO_FILE_ON_WALK "files/in.txt"
+#define WALK_LEN 10000
#define ITEM_CAP 3
int ITEMS = 3;
@@ -15,9 +16,9 @@ double chain[ITEM_CAP][ITEM_CAP] = {
};
char item_names[ITEM_CAP][64] = {
- "burger",
- "pizza",
- "hotdog"
+ "Burger",
+ "Pizza",
+ "Hotdog"
};
#include "markov.h"
diff --git a/src/main.c b/src/main.c
index e1a942e..01dfe23 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,11 +1,12 @@
#include <stdio.h>
+#include <time.h>
#define PRINT_ITEM_FREQUENCY
-// #define PRINT_ON_WALK
-// #define SAVE_TO_FILE_ON_WALK "walk.txt"
-#define WALK_LEN 1000000
+#define PRINT_ON_WALK
+// #define SAVE_TO_FILE_ON_WALK "out.txt"
+#define WALK_LEN 100
-#define ITEM_CAP 30
+#define ITEM_CAP 400
int ITEMS = 0;
double chain[ITEM_CAP][ITEM_CAP] = {0};
@@ -16,9 +17,9 @@ char item_names[ITEM_CAP][64] = {0};
int main(void)
{
srand(time(NULL));
- generate_chain("files/chain.txt");
+ generate_chain("files/in.txt");
- print_chain();
+ // print_chain();
take_walk();
return 0;
}
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);