As most of you already know that are reading this, I have been working on an AI program lately. I have the code working as far as sorting and separating input from a file but I don't think that it's very efficient. My estimates with pen and paper tell me that it will approximately 7-8 seconds to load a 1,000 line file. That's without any sorting with qsort() and searching with bsearch() that I planned on using later. In the code I will post below, is there anything that would reduce the approximated time?
Code:
/* Preprocessor directives */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include <windows.h>
/* Structures */
typedef struct memory memory;
struct memory
{
char * question;
char ** responses;
int MAX_RESPONSE; /* No good way to make this constant, but it shouldn't change after it is set */
memory * next;
};
typedef struct tokens tokens;
struct tokens
{
char * token;
tokens * next;
};
/* ************************************************************************** */
/* String functions */
char * s_strdup( char * dest, char * string )
{
dest = malloc(strlen(string) + 1);
if ( !dest )
{
perror("Malloc");
exit(1);
}
return strcpy(dest, string);
}
/* ************************************************************************** */
char * s_filter_string( char * string )
{
char * temp = NULL;
int num = 0;
int len = 0;
for ( ; ; )
{
if ((temp = strchr( string, '!' )))
*temp = '\0';
if ((temp = strchr( string, '?' )))
*temp = '\0';
if ((temp = strchr( string, '.' )))
*temp = '\0';
if (temp == NULL )
break;
temp = NULL;
}
len = strlen(string);
for (temp = string; num < len; num++, temp++)
*temp = tolower(*temp);
return string;
}
/* ************************************************************************** */
/* Printing functions */
void print_tokens_list( tokens * list )
{
for (; list != NULL; list = list->next )
{
fprintf(stdout, "\t%s\n", list->token);
}
putchar('\n');
return;
}
/* ************************************************************************** */
void print_brain_memory( memory * list )
{
int response_num = 0;
for (; list != NULL; list = list->next )
{
fprintf(stdout, "\tQuestion : %s\n\n", list->question );
for (response_num = 0; response_num < list->MAX_RESPONSE; response_num++)
{
fprintf(stdout, "\tResponse %d : %s\n", response_num, list->responses[response_num]);
}
putchar('\n');
}
return;
}
/* ************************************************************************** */
/* Memory functions */
tokens * append_tokens_list(tokens * head, char * token)
{
tokens *temp;
tokens *newnode;
newnode = malloc(sizeof(*head));
if (!newnode)
{
perror("malloc");
exit(1);
}
newnode->token = token;
newnode->next = NULL;
if (!head)
head = newnode;
else
{
temp = head;
for ( ; temp->next != NULL; temp = temp->next );
temp->next = newnode;
}
return head;
}
/* ************************************************************************** */
memory * create_mem_node( memory * brain )
{
if (!(brain = malloc(sizeof(memory))))
{
perror("Malloc");
exit(1);
}
return brain;
}
/* ************************************************************************** */
char ** fill_responses( tokens * temp, char ** responses )
{
int offset = 0;
for ( ; temp != NULL; temp = temp->next, offset++ )
{
responses[offset] = s_strdup(responses[offset - 1], s_filter_string(temp->token));
}
return responses;
}
/* ************************************************************************** */
memory * store_tokens_list( tokens * list, memory * brain, int token_amount )
{
tokens * temp = list;
memory * m_temp = NULL;
if (!brain)
{
brain = create_mem_node(brain);
brain->question = NULL;
if (brain)
brain->question = s_strdup(brain->question, s_filter_string(temp->token));
if ((temp = temp->next))
{
brain->responses = malloc((token_amount - 1) * sizeof(char *));
if (brain->responses)
{
brain->MAX_RESPONSE = token_amount - 1;
brain->responses = fill_responses( temp, brain->responses );
brain->next = NULL;
} else
{
perror("Malloc");
exit(1);
}
}
} else
{
for (m_temp = brain; m_temp->next != NULL; m_temp = m_temp->next);
m_temp->next = create_mem_node(m_temp);
m_temp = m_temp->next;
m_temp->question = NULL;
m_temp->question = s_strdup(m_temp->question, s_filter_string(temp->token));
if ((temp = temp->next))
{
m_temp->responses = malloc((token_amount - 1) * sizeof(char *));
if (m_temp->responses)
{
m_temp->MAX_RESPONSE = token_amount - 1;
m_temp->responses = fill_responses( temp, m_temp->responses );
m_temp->next = NULL;
} else
{
perror("Malloc");
exit(1);
}
}
}
return brain;
}
/* ************************************************************************** */
void release_brain_memory( memory * head )
{
int response_num = 0;
memory * temp = NULL;
while ( head != NULL )
{
for (response_num = 0; response_num < head->MAX_RESPONSE; response_num++)
{
free(head->responses[response_num]);
}
free(head->responses);
free(head->question);
temp = head;
head = head->next;
free(temp);
}
return;
}
/* ************************************************************************** */
void release_tokens_list( tokens * head )
{
tokens * temp = NULL;
while ( head != NULL )
{
temp = head;
head = head->next;
free(temp);
}
return;
}
/* ************************************************************************** */
/* File functions */
FILE * open_file( char * file )
{
FILE * temp = fopen(file, "r+");
if (!temp)
{
perror("Opening file");
getchar();
exit(1);
}
return temp;
}
/* ************************************************************************** */
char * get_next_file_line( FILE * file )
{
char * line = malloc(sizeof(char) * BUFSIZ );
char * new_line;
if ( !line )
{
perror("Malloc");
exit(1);
}
if ( !fgets(line, BUFSIZ, file) )
{
free(line);
return NULL;
} else
{
if ((new_line = strchr(line, '\n'))) /* Removes the '\n' and replaces it with a '\0' if it is found */
*new_line = '\0';
return line;
}
}
/* ************************************************************************** */
tokens * parse_line( char * file_line, char * delimit, tokens * head, int * token_amount )
{
char * token = NULL;
token = strtok(file_line, delimit);
if ( token )
{
for (; token && !isspace(*token); *token_amount += 1 )
{
head = append_tokens_list(head, token);
token = strtok(NULL, delimit);
}
}
return head;
}
/* ************************************************************************** */
memory * load_brain_memory(FILE * txt_file, memory * my_brain)
{
tokens * head = NULL;
char * file_line = NULL;
int array_offset = 0;
int token_amount = 0;
for (; (file_line = get_next_file_line(txt_file)) != NULL; array_offset++, token_amount = 0 ) /* continue the loop until an error occurs or EOF is reached */
{
head = parse_line( file_line, ";", head, &token_amount);
my_brain = store_tokens_list( head, my_brain, token_amount );
release_tokens_list( head );
free(file_line);
head = NULL;
file_line = NULL;
}
return my_brain;
}
/* **************************************************************************
* MAIN ENTRY POINT :
*/
int main()
{
memory * my_brain = NULL;
FILE * txt_file = open_file("test.txt");
my_brain = load_brain_memory( txt_file, my_brain );
print_brain_memory( my_brain );
release_brain_memory( my_brain );
return 0;
}
/* **************************************************************************
* End of program.
* **************************************************************************/
Output :
Code:
Question : how are you
Response 0 : i'm good
Response 1 : i'm bad
Response 2 : i'm horrible
Response 3 : good, how are you
Question : i'm going to kill you
Response 0 : thank you
Response 1 : no problem
Response 2 : i like cows
Question : i don't know what you are
Response 0 : hey there
Response 1 : i like doing things
Question : you're the biggest noob on this planet
Response 0 : i hate you too, but i also love you
Response 1 : well, that escalated quickly
Question : this is a nice text file
Response 0 : i like random things
Response 1 : am i getting paid for this
Response 2 : what's my middle name
File :
Code:
How are you?;I'm good.;I'm bad.;I'm horrible.;Good, how are you?
I'm going to kill you!;Thank you!;No problem;I like cows
I don't know what you are;Hey there!;I like doing things
You're the biggest noob on this planet!!!!!!!!!!!!!!!!!!!!!!;I hate you too, but I also love you...;Well, that escalated quickly.
This is a nice text file;I like random things;Am I getting paid for this?;What's my middle name?
The input file is garbage, but I needed to try different character amounts and different formats of text, so I just typed in random things.
I already set my compiler to the maximum program speed too.