#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "halium.h"
#include "tree234.h"

void dump_dict(tree234* t)
{
	int i;
	char* p;
	printf("Tree %p:\n", t);
	for (i = 0; (p = index234(t, i)) != NULL; i++)
		printf("%i: %s\n", i, p);
}

void dump_tree(tree234* t, char* text)
{
	int i;
	node_t* p;
	char* text2;
	for (i = 0; (p = index234(t, i)) != NULL; i++) {
		if (count234(p->tree) != 0) {
			text2 = malloc(strlen(text) + strlen(p->word) + 1);
			strcpy(text2, text);
			strcat(text2, p->word);
			dump_tree(p->tree, text2);
			free(text2);
		} else {
			printf("\"%s%s\"\n", text, p->word);
		}
	}
}

char* train[] = {
//#include "training"
"fnord is the word that I use to describe myself.",
"would that I call you grandma?"
};

int main(int argc, char** argv)
{
	model_t* m = make_model(5);
	char* b;
	char** l;
	int i;

	srand(time(NULL));

	printf("\e[1mTraining sentences:\e[0m\n");
	for (i = 0; i < sizeof(train) / sizeof(*train); i++) {
		printf("%s\n", train[i]);

		b = lowercase(strdup(train[i]));
		l = make_word_list(b);
		learn(m, l);
		free(b);
		free_word_list(l);
	}

	printf("\e[1mForward tree, all traversals:\e[0m\n");
	dump_tree(m->forward, "");

	printf("\e[1mBackward tree, all traversals:\e[0m\n");
	dump_tree(m->backward, "");

	free_model(m);
	return EXIT_SUCCESS;
}

