Chat bot brain ideas

This is a discussion on Chat bot brain ideas within the C++ Programming forums, part of the General Programming Boards category; Hello, i start learning C++ and i wanna create my first C++ chat bot, a very simple one so i ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    Chat bot brain ideas

    Hello,
    i start learning C++ and i wanna create my first C++ chat bot, a very simple one so i would like give me some ideas how the code will be. My idea is something like that...

    Chat bot code:
    Code:
    #inlcude <iostream>
    #inluce <ifstream>
    using namespace std;
    
    int main()
    {
    
    cout<<Bot will wlcome you here
    cin>>You will answer here
    
    }
    Something like that, i'm not sure how will the code be but i wanna with ifstream make the code check another file which will have something like...

    //The {} is for the bot and () for human
    {Hello}
    ...then if...
    (hi) post {how are you?}
    if hi (HI, how are you?)
    bot will answer {fine}
    something like that you get me?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok, if you want a very very simple one. You could just play around with what you already have.

    Here is a pseudo example
    Code:
    do {
    
        get input
    
        check input 
    
            if ( input == EXPECTED_TEXT ) give expected response
    
    } while ( input != exit command )
    Now going by that there are many different ways. You could use a switch statement or a bunch of if's.

    If you still need more help let me know.
    What is C++?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    I didn't get your pseudo example, for exampel you say
    in the code...
    Code:
    do {
    
        get input
    
        check input 
    
            if ( input == Hello ) give expected response for example hi?//something like that?
    
    } while ( input != exit command )

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Yeah, here is a more to the point... example.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    
    	string the_string;
    	
    	cout << "Talk to me." << endl;
    
    	do {
    
    		getline ( cin, the_string );
    
    		if ( the_string == "hello" ) cout << "hi" << endl;
    		else if ( the_string == "quit" ) cout << "bye" << endl;
    		else cout << "I dont understand" << endl;
    
    	} while ( the_string != "quit" );
    
    	return 0;
    
    }
    That is a rough example of a simple chat bot.

    EDIT:
    Now once you play with that, you will probably want to break the strings up and search for keywords. So you can look for any instance of hello in the string and then just reply with "hi".

    example:
    (Hello)
    {Hi}
    (Hello mr copmputer)
    {Hi}
    Once you figure out stiff like that you can make a very nice simple chat bot.
    Last edited by Vicious; 10-01-2004 at 02:35 PM.
    What is C++?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    nice example and a working one, thanks. But i wasn't thinking of that, i was of that but from something different! I was thing that the code if say hello say hi will be in another file for example in brain.txt or brain.bot and i will create the brain like this

    //The {} is for the bot and () for human
    {Hello}
    ...then if...
    (hi) post {how are you?}
    if hi (HI, how are you?)
    bot will answer {fine}
    and i will have one .cpp file which will check for this file

    Edit:
    (Hello)
    {Hi}
    (Hello mr copmputer)
    {Hi}
    how cna i succed the above?
    Last edited by cogeek; 10-01-2004 at 02:42 PM.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok so you want the Computers response to be silent?

    So in the actual console it will be

    Code:
    Hi
    How are you?
    and in the file it will be

    Code:
    (Hi)
    {Hello}
    (How are you?)
    {Fine}
    ?
    What is C++?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    Yes, that i will like happen is:
    code:
    Hello
    if human says hi hello
    how are you?
    if human says fine bot will answer that nice, if human answer bad bot will naswer why that?

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok, here is a quick modification of the above code.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    // Function for opening/writing to a file.
    void WriteToFile ( char *file_name, char *text )
    {
    
    	// Open file with ios::app to append
            ofstream the_file ( file_name, ios::app );
    
    	// Check for file
    	if ( !the_file.is_open () ) {
    
    		cout << "Failed to open " << file_name;
    		exit ( 1 );
    
    	}
    
    	// Output the text and a newline
            the_file << text << endl;
    
    	the_file.close ();
    
    }
    
    int main ()
    {
    
    	string the_string;
    	
    	cout << "Talk to me." << endl;
    
    	do {
    
    		getline ( cin, the_string );
    
    		if ( the_string == "hello" ) WriteToFile ( "C:/CPU.txt", "Hello" );
    
    	} while ( the_string != "quit" );
    
    	return 0;
    
    }
    There is a good bit of stuff you will need to change but this should give you a good idea. If you are still completely lost then maybe you should take it slower and try something simpler, learn the language better. I am not saying this to depress you or anything, but I am saying it because if you try to jumo in the deep end before you can swim.. you'll drown.

    NOTE:
    I am sorry if this is still not what you are looking for. Maybe im just lost myself
    Last edited by Vicious; 10-01-2004 at 03:20 PM.
    What is C++?

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    The problem is i got:
    Failed to load
    file:/home/cobra/Documents/bot.txtbash-2.05b$
    what should I add in bot.txt? A discussion like:
    hello
    hi
    how are you?
    fine
    that nice
    yes it is
    Last edited by cogeek; 10-01-2004 at 03:00 PM.

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    I fixed my code up there.

    Code:
    // Open file with ios::app to append
    ofstream the_file ( file_name, ios::app );
    You will want to put ios::app as an argument when you open the file so that it will go to the end of the file and not overwrite what you have written.

    EDIT:
    Also opening and closing the file everytime you want to write to it is probably not the best way as it is slow. But it was the neatset way I could think of to write the code.
    Last edited by Vicious; 10-01-2004 at 03:10 PM.
    What is C++?

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    put ios::app as an argument
    soory but i don't know what is this?
    I know run the program and i see Talk to me.(cool!!!)
    but i write many lines without answering only and when i write quit it closes?
    Then could you please explain me what do you mean with that
    thanks

  12. #12
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    // Function for opening/writing to a file.
    void WriteToFile ( char *file_name, char *text )
    {
    
    	// Open file with ios::app to append
            ofstream the_file ( file_name, ios::app );
    
    	// Check for file
    	if ( !the_file.is_open () ) {
    
    		cout << "Failed to open " << file_name;
    		exit ( 1 );
    
    	}
    
    	// Output the text and a newline
            the_file << text << endl;
    
    	the_file.close ();
    
    }
    The ios::app goes to the end of the file to write so it doesnt overwrite whats already in the file.

    Also the only thing my code example will do is write "Hello" to the file if you type "hello" and quite if you type "quit".

    Just add more conditions to it and you will get more stuff.

    Code:
    do {
    
    	getline ( cin, the_string );
    
    	if ( the_string == "hello" ) WriteToFile ( "C:/CPU.txt", "Hello" );
            else if ( the_string == "something" ) WriteToFile ( "C:/CPU.txt",  "Why something?" );
    
    } while ( the_string != "quit" );
    Now using this method it is case sensitive. So "Hello" is different than "hello".

    I would suggest you just play around with it until you understand what is going on with it. When you go "Ah ha!" then you will be ready for more

    EDIT:

    This code will be more efficent because you wont be opening and closing the file everytime you write to it.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    
        string the_string;
    
        ofstream the_file ( "c:/CPU.txt", ios::app );
    	
        cout << "Talk to me." << endl;
    
        do {
    
    	getline ( cin, the_string );
    
    	if ( the_string == "hello" ) the_file << "hi" << endl;
    	else if ( the_string == "quit" ) the_file << "bye" << endl;
    	else the_file << "I dont understand" << endl;
    
        } while ( the_string != "quit" );
    
        the_file.close ();
        
        return 0;
    
    }
    Run that code and type

    hello
    llamma
    quit

    Then go to C:\ and you will see CPU.txt, open it and look.

    OMG
    I just realised what you want to do. I am so sorry. doh im so slow.

    You want the information to be in a text file!!!!

    So you want it to search the text file for certain keywords found in the user input string and write the apropriate reponses.

    Jeez im so sorry, I am on some cold medication and the room is spinning so forgive my stupidity.

    Ok well my code still aplies in theory but Im not familiar with searching files for text. Let me go look up some stuff.
    Last edited by Vicious; 10-01-2004 at 03:30 PM.
    What is C++?

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One way is not to search the file per se. You can read the file one input at a time and compare it to some desired value before going to next item in file, or you can read entire file into a container and search container---tends to be faster than the first method, or you can construct the file so that it is completely predictable with regard structure and use fseek(), etc to appropriately position file pointer to read out just a given piece of data (presumes you know where to look, too!).

  14. #14
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    I've been playing around with my own sort of "chatbot" that responds based on "emotion numbers" and a "personality" code. It is very difficult and I may not manage it, but it has taught me about breaking up sentences.

    1)create a string that the user inputs to.
    2)create a char array that takes each letter in the string and puts it into another slot on the array.
    2.5)create an string array, where each word can be put into another slot
    3)make an if statement in a "for" loop for int "i", where (the string array)[0] == char array[i], and have the "if" statement so if the array ==" " (space), int temp = 0 (the string array)[temp++];
    4) make a "for" loop that runs through the sentence and have it check each word with the words in a big switch statement, and make the results, so that if the word is there, it returns the desired response into another string array. Then use a "for" loop to have it print out all the responses.

    If you want something a little more difficult, you may try having each word be a structure, and inside the structure is the word and a "grammar" number. Instead of matching with a switch statement, a pointer array (I love arrays) can point to the different words in the "memory", and determine a response based on their grammar numbers, which would pass through a grammar function.

    I've been working on this for ages, and it is very difficult, but for a realistic chatbot, try this or save effort and just google :Chatbots

    PS:I really hope that string arrays exist, or everything will be a wasted effort.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    thanks, i will see your opinions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat Bot Help!!
    By arealfind08 in forum C Programming
    Replies: 1
    Last Post: 03-09-2009, 07:52 AM
  2. Chat BOT Sample
    By RoD in forum C++ Programming
    Replies: 19
    Last Post: 11-04-2002, 04:51 AM
  3. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 07:11 AM
  4. Msvc Aim Chat Bot ???
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-19-2001, 09:46 AM
  5. Chat bot for AIM
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2001, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21