#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define LOG(...) printf(__VA_ARGS__);
typedef unsigned int uint;
typedef uint64_t ulongint;
enum settings_list
{
MARKOVLVL, //level of markov chain
HASHSIZE, //size of hash
MAXTEXTSIZE, //maximal size of text
AVERAGESENTENCESIZE //average size of sentence
};
ulongint hash_coefficients[13]={1,37,1369,50653,1874161,69343957,2565726409,94931877133,3512479453921,
129961739795077,4808584372417849,177917621779460413,6582952005840035281};
int settings[sizeof(settings_list)];
struct suffix
{
char * text;
suffix * next; //pointer to the next suffix
uint pref_counter; //how many times this suffix appears
//uint eos_counter; //how many times this prefix was an end of sentence
};
void free_s_list(suffix * suff)
{
if(suff->next == NULL)
{
free(suff);
return;
}
else
{
free_s_list(suff->next);
free_s_list(suff);
}
}
struct prefix
{
suffix * suffix_list;
uint sl_size; //size of suffix list
char * words;
uint counter; //how many times this prefix appears
};
prefix * create_prefix(char * pref, char * suff)
{
prefix * result = (prefix*) malloc(sizeof(prefix));
result->suffix_list = (suffix*) malloc(sizeof(suffix));
result->suffix_list->next = NULL;
result->suffix_list->text = (char*) malloc(strlen(suff)+1);
result->suffix_list->pref_counter = 1;
strcpy(result->suffix_list->text,suff);
result->words = (char*) malloc(sizeof(pref));
strcpy(result->words, pref);
result->counter = 1;
result->sl_size = 1;
return result;
}
void delete_prefix(prefix * pref)
{
if(pref->suffix_list != NULL)
free_s_list(pref->suffix_list);
if(pref != NULL)
free(pref->words);
}
void add_suffix(prefix * pref, char * suff)
{
/*LOG("ADDING SUFFIX %s TO PREFIX %s\nPREFIX LIST OF SUFFIXES(%d):\n",suff, pref->words,pref->sl_size);
for(int i = 0; i < pref->sl_size; i+= pref->suffix_list->)
{
LOG("%s %d\n",pref->suffix_list->text, pref->suffix_list->pref_counter);
}*/
suffix * suf = pref->suffix_list;
for(int i = 0; i < pref->sl_size;)
{
if(strcmp(suf->text, suff) == 0)
{
suf->pref_counter += 1;
pref->sl_size++;
return;
}
i+=suf->pref_counter;
if( suf->next == NULL)
{
suf->next =(suffix*) malloc(sizeof(suffix));
suf = suf->next;
break;
}
}
pref->sl_size++;
suf->next = NULL;
suf->text = (char*) malloc(strlen(suff)+1);
strcpy(suf->text, suff);
//LOG("SUFFIX THAT WAS ADDED: %s\n", suff);
suf->pref_counter += 1;
}
prefix empty_prefix = {NULL,0,NULL,0};
struct bt_node
{
bt_node * left;
bt_node * right;
bt_node * parent;
ulongint key;
prefix * value;
};
//bt_node * bt_add(char * pref, char * suff)
//{
// prefix * t = search(hash(pref));
// if(t != &empty_prefix)
// {
// t->add_suffix(suff);
// return;
// }
//
// if(this == NULL)
// {
// root->key = hash(pref.words);
// root->value = (prefix*) malloc(sizeof(prefix));
// root->value->words = (char*) malloc(pref_len);
// strcpy(root->value->words, pref);
// root->value
// }
//}
ulongint hash(char * str)
{
int numlen = strlen(str);
ulongint result = 0;
for(int i = 0; i < numlen; i++)
result += (str[i] + 1)*hash_coefficients[i%13];
return result;
}
int main(void)
{
settings[MARKOVLVL] = 3;
settings[HASHSIZE] = 10000;
settings[MAXTEXTSIZE] = 10000;
settings[AVERAGESENTENCESIZE] = 7;
prefix * pref = create_prefix("some words here", "wow");
add_suffix(pref, "wow");
add_suffix(pref, "notwow");
suffix* suff = pref->suffix_list;
while(suff != NULL)
{
LOG("%s\n",suff->text);
if(suff->next == NULL)
break;
else
suff = suff->next;
}
delete_prefix(pref);
getchar();
return 0;
}