I have finished making my core for my artificial intelligence program.
What it does is described first then what needs to happen, then what it might do.
What it does is is takes input from a sentence and writes to a file the grammar for each word is.
If the word is a noun or verb etc.
Along with that it provides a psychological assesment of the speaker by using a percentage process.
As the speaker describes something novel or pure,
words that have the vowel is at the end of the word make a good portion of the sentence.
Then as the speaker gets familiar and sees the quirks of the novel pure person the vowel is at
the front of the word and these types of words are spoken more than words that have the vowel at the end of the word.
So psychological assesment and grammar together form a calculatable sum.
That is if the words are showing a psychological bend, then it is usually with a type of grammer used in the sentence.
What needs to happen is I need a marker in the output file that shows the sentence first and last word,
and number each of the words as they appear in the sentence.
Then after the process of writting the sentence words and percentage to file,
I need to open the text file with the numbered words in the sentence and filter out the bad grammar
associated with each word.
That is a word may be defined as a noun or verb etc.
What is possible.
If I manage to clean the grammar I will have a file that has sentences with good grammar and a percentage system.
That text file can provide a processable source that can be used in logic.
If the logic is true it can be used in a chatbot to fill in story lines.
Now here is the programs two parts:
The first program takes a file that has multiple sentences per line
and writes them into another file so there is only a sentence at a time.
Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int character, file_character=0;
char buffer[1024];
FILE *book=fopen("readtext1.txt", "r");
FILE *book2=fopen("readtext.txt", "a+");
if(!book) {printf("Error: unable to open input file!\n"); return 1;}
if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
while(file_character!=EOF)
{
buffer[0]='\0';
for(character=0;character<sizeof(buffer);character++)
{
file_character=fgetc(book);
if(file_character==EOF)
break;
if(file_character=='.')
{
buffer[character]='\0';
break;
}
buffer[character]=file_character;
}
if(file_character==EOF)
break;
fprintf(book2, "%s.\n", buffer);
}
fclose(book);
fclose(book2);
putchar('\n');
return 0;
}
The second program reads this text file a sentence at a time and
writes the grammar of the sentence and the percentage calculation to a file.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINES 4096
/* function prototype for percentage calculation */
void percentage_calculation_numbers(char*, int, char*);
/* function prototype to count the number of characters in a string */
int countchar (char []);
/* function prototype to find the first and last letter in the word */
char find_letter (char* a, char* b);
char red;
/* function prototype to reverse the letters in the word */
char* rev(char* str);
char* reverse;
/* function so I don't have to type getch all over the place */
void MyExit(void) { system("pause"); }
/* the main program */
int numchar;
int main ()
{
/* declaring and initiaizing variables */
FILE *book;
char * pch = malloc(300);
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char buffer[LINES];
atexit(MyExit);
/* open text file or report error */
book = fopen("readtext.txt", "r");
if(!book)
{
perror("Error: file readtext.txt was not found or opened");
return 0;
}
/* read from file */
while(fgets(buffer, sizeof(buffer), book)!=NULL)
{
/* I tokenize the input string into individual words */
pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
if(strpbrk(pch, alphabet))
{
/* numchar counts the numbers of characters in the pch string */
numchar = countchar (pch);
/* I calculate the percentage */
percentage_calculation_numbers(pch, numchar, buffer);
}
else
{
continue;
}
}
fclose(book);
return 0;
}
/* The function to find the first letter in the word */
char find_letter (char* a, char* b)
{
char string = strlen(b);
strncpy (a,b,1);
a[1]='\0';
return 0;
}
/* The function to reverse the characters in a string */
char* rev(char* str)
{
int end= strlen(str)-1;
int start = 0;
while( start<end )
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start]^= str[end];
++start;
--end;
}
return str;
}
/* The function to count the number of characters in a string */
int countchar (char list[])
{
int i, count = 0;
for (i = 0; list[i] != '\0'; i++)
count++;
return (count);
}
/* The function for percentage calculation */
void percentage_calculation_numbers(char* a_pch, int a_numchar, char* a_b)
{
FILE *sp;
FILE *fp;
char *filedata = malloc(300);
char str2[7];
char vowels[] = "aeiouy";
char letters[] = "bcdfghjklmnpqrstvwxz";
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
float one;
float two;
float three;
float four;
float total;
float five;
float six;
float seven;
float eight;
float total_2;
float percentage_1;
float percentage_2;
float percentage_3;
float percentage_4;
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
five = 0;
six = 0;
seven = 0;
eight = 0;
total_2 = 0;
percentage_1 = 0;
percentage_2 = 0;
percentage_3 = 0;
percentage_4 = 0;
/* open text file or report error */
sp = fopen("readlist.txt", "r");
fp = fopen("writelist.txt", "a+");
if(!sp)
{
perror("Error: file readlist.txt was not found or opened");
exit(1);
}
if((fp = fopen("writelist.txt", "a+"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
while(a_pch != NULL)
{
/* text file comparison begin */
while(fgets(filedata, 300, sp))
{
if(_memicmp(a_pch, filedata, strlen(a_pch)) == 0
&& (filedata[strlen(a_pch)] == ' '
|| filedata[strlen(a_pch)] == '\n'))
{
/* adding numchar to filedata erases the word and leaves the grammar intact */
printf("%s\n", filedata);
/* write the text file comparison result to file */
fprintf(fp, "%s\n", filedata);
}
}
/* text file comparison end */
/* identify the first and last letter in the word begin */
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
one++;
}
if(strpbrk(str2, vowels))
{
two++;
}
reverse = rev(a_pch);
red = find_letter(str2, a_pch);
if(strpbrk(str2, letters))
{
three++;
}
if(strpbrk(str2, vowels))
{
four++;
}
/* identify the first and last letter in the word end */
/* The math to see what kind of word it is start */
if(total = (one && three))
{
five++;
}
else if(total = (one && four))
{
six++;
}
else if(total = (two && three))
{
seven++;
}
else if(total = (two && four))
{
eight++;
}
/* The math to see what kind of word it is end */
/* Reset the pointers */
one = 0;
two = 0;
three = 0;
four = 0;
total = 0;
rewind(sp);
a_pch = strtok (NULL, " .");
}
/* pch is NULL, so I find alphabet in b and pass that into pch */
a_pch = strpbrk(a_b, alphabet);
/* pch is backwards, so I use rev to reverse it back */
/*printf("%s uncomment to test to see if the string is backwards", pch);*/
reverse = rev(a_pch);
/* count the pointers */
total_2 = (five + six + seven + eight);
percentage_1 = ((five)/ total_2);
percentage_2 = ((six)/ total_2);
percentage_3 = ((seven)/ total_2);
percentage_4 = ((eight)/ total_2);
printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
/* write the pointers result to file */
fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
fclose(fp);
fclose(sp);
}
And you need the file with the grammar:
readlist.txt
Code:
car n
ran v
jeremy n
sarah n
And you need the file with the original sentence(s):
readtext1.txt
Code:
car ran.car ran. jeremy car ran ran. 123. @&. "sarah".
And here are the names of the two blank files:
readtext
writelist
Just put the text files and compiled programs in the same folder, then run the first program then the second program
then look at the writelist.txt file.
I'm not sure how far I will get this project, but I made up my mind late last month to make it and I have
with the help of Adak and WayneAKing over at msdn c++ forum.
Why did I make the core first? Because I wanted to see if there was a chance of doing it or just to ignore it.
Now I have to get the dictionary that defines what grammar part each word is then write the results to writethis so that file can be opened and run through a program that will right the correct grammar to a different file.
Mainly I wanted to be able to run the program on the written text of articles and songs or even books to see the percentage results, to see what the author is feeling, like psychology and people watching.
Also if somebody else is making AI then this post and it's code may help them.
And if you see typos, the keys on my notebook keyboard aren't pressing down properly, they float and are difficult to press down.