Thread: I present to yo the program I made. It is the core for my Artificial Intelligence

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    I present to yo the program I made. It is the core for my Artificial Intelligence

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, it's a word analyzer of a sort, but it has no AI features in it yet. I'm unsure of any connection between word analysis and sociology or psychology though.

    Good work for a first project in C.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Quote Originally Posted by Adak View Post
    I'm unsure of any connection between word analysis and sociology or psychology though.
    I like Roger Eberts reviews of good and bad mpvies to show the way words use vowels to emphasize the quality of the subject.
    Here are two reviews, one of the 2011 Conan the Barbariab, and the other of Terminator 2, both reviews are by Roger Ebert.

    Both reviews have been processed to their percentage form:

    Conan:

    Code:
    letter vowel letter 0.357143
    letter vowel vowel 0.214286
    vowel vowel letter 0.285714
    vowel vowel vowel 0.142857
    
    
    letter vowel letter 0.750000
    letter vowel vowel 0.000000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.214286
    letter vowel vowel 0.214286
    vowel vowel letter 0.500000
    vowel vowel vowel 0.071429
    
    
    letter vowel letter 0.390244
    letter vowel vowel 0.317073
    vowel vowel letter 0.268293
    vowel vowel vowel 0.024390
    
    
    letter vowel letter 0.550000
    letter vowel vowel 0.200000
    vowel vowel letter 0.200000
    vowel vowel vowel 0.050000
    
    
    letter vowel letter 0.459459
    letter vowel vowel 0.216216
    vowel vowel letter 0.270270
    vowel vowel vowel 0.054054
    
    
    letter vowel letter 0.444444
    letter vowel vowel 0.555556
    vowel vowel letter 0.000000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.428571
    letter vowel vowel 0.142857
    vowel vowel letter 0.285714
    vowel vowel vowel 0.142857
    
    
    letter vowel letter 0.434783
    letter vowel vowel 0.304348
    vowel vowel letter 0.173913
    vowel vowel vowel 0.086957
    
    
    letter vowel letter 0.375000
    letter vowel vowel 0.125000
    vowel vowel letter 0.312500
    vowel vowel vowel 0.187500
    
    
    letter vowel letter 0.571429
    letter vowel vowel 0.000000
    vowel vowel letter 0.357143
    vowel vowel vowel 0.071429
    
    
    letter vowel letter 0.400000
    letter vowel vowel 0.200000
    vowel vowel letter 0.200000
    vowel vowel vowel 0.200000
    
    
    letter vowel letter 0.588235
    letter vowel vowel 0.294118
    vowel vowel letter 0.117647
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.421053
    letter vowel vowel 0.210526
    vowel vowel letter 0.210526
    vowel vowel vowel 0.157895
    
    
    letter vowel letter 0.478261
    letter vowel vowel 0.130435
    vowel vowel letter 0.260870
    vowel vowel vowel 0.130435
    
    
    letter vowel letter 0.357143
    letter vowel vowel 0.285714
    vowel vowel letter 0.214286
    vowel vowel vowel 0.142857
    
    
    letter vowel letter 0.588235
    letter vowel vowel 0.235294
    vowel vowel letter 0.000000
    vowel vowel vowel 0.176471
    
    
    letter vowel letter 0.375000
    letter vowel vowel 0.125000
    vowel vowel letter 0.375000
    vowel vowel vowel 0.125000
    
    
    letter vowel letter 0.583333
    letter vowel vowel 0.000000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.166667
    
    
    letter vowel letter 0.384615
    letter vowel vowel 0.230769
    vowel vowel letter 0.307692
    vowel vowel vowel 0.076923
    
    
    letter vowel letter 0.413793
    letter vowel vowel 0.241379
    vowel vowel letter 0.206897
    vowel vowel vowel 0.137931
    Terminator 2

    Code:
    letter vowel letter 0.375000
    letter vowel vowel 0.375000
    vowel vowel letter 0.125000
    vowel vowel vowel 0.125000
    
    
    letter vowel letter 0.340426
    letter vowel vowel 0.297872
    vowel vowel letter 0.276596
    vowel vowel vowel 0.085106
    
    
    letter vowel letter 0.608696
    letter vowel vowel 0.260870
    vowel vowel letter 0.086957
    vowel vowel vowel 0.043478
    
    
    letter vowel letter 0.363636
    letter vowel vowel 0.333333
    vowel vowel letter 0.272727
    vowel vowel vowel 0.030303
    
    
    letter vowel letter 0.500000
    letter vowel vowel 0.250000
    vowel vowel letter 0.125000
    vowel vowel vowel 0.125000
    
    
    letter vowel letter 0.517241
    letter vowel vowel 0.310345
    vowel vowel letter 0.137931
    vowel vowel vowel 0.034483
    
    
    letter vowel letter 0.435294
    letter vowel vowel 0.235294
    vowel vowel letter 0.247059
    vowel vowel vowel 0.082353
    
    
    letter vowel letter 0.533333
    letter vowel vowel 0.266667
    vowel vowel letter 0.200000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.250000
    letter vowel vowel 0.321429
    vowel vowel letter 0.321429
    vowel vowel vowel 0.107143
    
    
    letter vowel letter 0.380952
    letter vowel vowel 0.333333
    vowel vowel letter 0.238095
    vowel vowel vowel 0.047619
    
    
    letter vowel letter 0.480000
    letter vowel vowel 0.200000
    vowel vowel letter 0.280000
    vowel vowel vowel 0.040000
    
    
    letter vowel letter 0.400000
    letter vowel vowel 0.200000
    vowel vowel letter 0.300000
    vowel vowel vowel 0.100000
    
    
    letter vowel letter 0.500000
    letter vowel vowel 0.250000
    vowel vowel letter 0.208333
    vowel vowel vowel 0.041667
    
    
    letter vowel letter 0.200000
    letter vowel vowel 0.400000
    vowel vowel letter 0.300000
    vowel vowel vowel 0.100000
    
    
    letter vowel letter 0.375000
    letter vowel vowel 0.375000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.684211
    letter vowel vowel 0.105263
    vowel vowel letter 0.157895
    vowel vowel vowel 0.052632
    
    
    letter vowel letter 0.444444
    letter vowel vowel 0.222222
    vowel vowel letter 0.222222
    vowel vowel vowel 0.111111
    
    
    letter vowel letter 0.384615
    letter vowel vowel 0.384615
    vowel vowel letter 0.153846
    vowel vowel vowel 0.076923
    
    
    letter vowel letter 0.416667
    letter vowel vowel 0.333333
    vowel vowel letter 0.250000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.333333
    letter vowel vowel 0.285714
    vowel vowel letter 0.333333
    vowel vowel vowel 0.047619
    
    
    letter vowel letter 0.437500
    letter vowel vowel 0.125000
    vowel vowel letter 0.312500
    vowel vowel vowel 0.125000
    
    
    letter vowel letter 0.416667
    letter vowel vowel 0.250000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.083333
    
    
    letter vowel letter 0.400000
    letter vowel vowel 0.300000
    vowel vowel letter 0.200000
    vowel vowel vowel 0.100000
    
    
    letter vowel letter 0.384615
    letter vowel vowel 0.192308
    vowel vowel letter 0.230769
    vowel vowel vowel 0.192308
    
    
    letter vowel letter 0.375000
    letter vowel vowel 0.281250
    vowel vowel letter 0.218750
    vowel vowel vowel 0.125000
    
    
    letter vowel letter 0.440000
    letter vowel vowel 0.280000
    vowel vowel letter 0.120000
    vowel vowel vowel 0.160000
    
    
    letter vowel letter 0.448276
    letter vowel vowel 0.172414
    vowel vowel letter 0.275862
    vowel vowel vowel 0.103448
    
    
    letter vowel letter 0.500000
    letter vowel vowel 0.350000
    vowel vowel letter 0.100000
    vowel vowel vowel 0.050000
    
    
    letter vowel letter 0.750000
    letter vowel vowel 0.000000
    vowel vowel letter 0.250000
    vowel vowel vowel 0.000000
    
    
    letter vowel letter 0.222222
    letter vowel vowel 0.111111
    vowel vowel letter 0.444444
    vowel vowel vowel 0.222222
    
    
    letter vowel letter 0.250000
    letter vowel vowel 0.250000
    vowel vowel letter 0.400000
    vowel vowel vowel 0.100000
    
    
    letter vowel letter 0.428571
    letter vowel vowel 0.333333
    vowel vowel letter 0.190476
    vowel vowel vowel 0.047619
    
    Press any key to continue . . .
    I still have to mark in the number the word is in the in the output above though. So if 'A' is the fist word then it looks like 1 A in the output file. But I have to think of how it will work before I go markng the output file so it looks hacky. But read these two reviews to see how he uses the vowel letter and letter vowel.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I would like to note that the second program needs a text file whose sentences have periods. Otherwise some words will be ignored and the results will not be correct.
    On lyrics webpages songs don't have periods, so if those were run through the second program it wouldn't see all the lyrics without periods on each lyric line.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Here is a complete description of my code and the state of my fincal code:

    Link to folder with compiled programs and source code.

    My plan:

    You run the code program, then the new program.
    The code program read readtext1.txt.
    After code.exe is run readtext.txt has a sentence per line.

    New.exe reads readtext.txt and outputs to writelist.txt.

    If readlist.txt has a word and it's grammar beside it
    it will be listed above the percentage results in writlist.txt.

    What is this program useful for?
    It is a sentence analyzer.
    fter analyzing the sentence the writelist.txt shows the
    word, the grammar of the word, and the percent a type of word was usd in the sentence.

    So if words with the vowel as the first letter in the word are used, a word like 'and', it will be calculated and made a percentage value.

    What do the percentage values show?
    Words with letter vowel vowel are positive affirmations.
    Words with vowel vowel letter are negative affirmations.

    As a article begins the affirmations are more
    letter vowel vowel
    but as the article ends the percentage is more
    vowel vowel letter.
    There is a arc that goes from acceptance to irritation.

    The acceptance sentence has a type of grammar.
    The irritation sentence has a type of gammar.
    If later on I make a code that can identify these states and mimick them it can seem intelligent.
    Also if a code can identify a user input belongs to a acceptance sentence, the code runs a statistic on which acceptance sentence best suits the input from the arc and then outputs a best matching result to the user.


    That is the arc is the wheel and the sentences are the teeth on the wheel together they form a gear.
    The user adds input and the teeth hold match the user input to a best fit then output the best result.
    Code:
    wheel = percentage values; // The part
    gear = word added to the wheel // reason painted on the part
    gear teeth mating = the input word is a gear tooth, it fits gear teeth. // the placement of the painting makes sense
    
    user inputs 'car'
    'car'= any_word_any_keyword(A wheel -> a gear -> gear tooth fits gear teeth)
    {
    the keyword belongs first to the wheel, where on the wheel;
    then to the gear, where on the gear;
    then to the mating gear teeth, where on the mating gear teeth
    }
    The current state is I have the wheel.
    I need to open writethis.txt and filter the grammar beside the word so only the right grammar is beside the word, then I will have the gear.
    Then I need to take the gear and add a gear tooth or gear teeth so the word from the previous step makes logical sense.
    This stage is the difference between street lights shining ultraviolet or green.
    Last edited by jeremy duncan; 11-12-2011 at 08:15 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Poems could be a problem as well. I'm not sure you want to resolve this. Maybe use a blank line, as a period? They normally have a blank line (two newlines in succession), to set apart the verse from the chorus.

    Whatever you do, keep it as simple linguistically, as possible. Anything else will be an Anaconda, wrapping around you in a stream, dragging you under.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comprehension in artificial intelligence!!!
    By nishu1988 in forum General AI Programming
    Replies: 5
    Last Post: 08-09-2007, 02:57 AM
  2. Program I made!
    By epidemic in forum C++ Programming
    Replies: 13
    Last Post: 04-06-2007, 09:34 PM
  3. Check out my new CHESS game with AI(Artificial Intelligence)
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 05-26-2003, 10:12 AM
  4. What's the best program you have ever made?
    By Kevin.j in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 10-02-2002, 11:29 AM
  5. Artificial Intelligence
    By Ruski in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-27-2002, 11:38 AM