loading

The following will provide instructions on how to write a fun and simple chatterbot like application in C++. The user enters a phrase and then the learner responds if the initial phrase exists in memory. If it doesn't exist in memory, then the user can teach the learner what to say. The open source speech synthesiser espeak is used to create audible output. You can get it here:

http://sourceforge.net/projects/espeak/

The code does assume you have this in the same dir as the chatterbot application, but if it is not, you just won't hear any audible message along with the textual output.

If you can't be bothered typing up the code, we have it attached..... but we wouldn't recommend that :P

We are assuming that you have a basic understanding of C++, and compilation of multifile projects.

Step 1: Learner.h

Let's first create our learner class. A learner will respond to phrases if it knows how to, and learn how, if it doesn't.
Open up your favourite editor/IDE, create a header file called learner.h and type up the code in the image.
Again, I assume you understand what all this means, but I will explain the algorthmic details in the the next step.

Step 2: Learner.cpp

Create another file called learner.cpp and copy the code in the image.

Let’s take a look at the respond function. The algorithm is fundamentally simple, but the file operations can be confusing.

On line 15 we create a file stream object and then assign the memory file on line 16. Notice the ios::in argument. This means that we want to open the file for input, i.e. to read the file.

At line 19 we begin a while loop which will continue until the end of file is reached.

Line 21, getline, will read a multi word line, where the cursor is currently sat, and then move to the next line for the future operation. The phrase is stored in the identifier variable.

On line 23 we see if the current phrase matches the user’s phrase. If it does we use getline again on line 25 to get the response underneath the matched phrase. The response is then said using the learner’s voice object, and then we leave the function on line 27.

If we reach the end of file and we have found no response, then we go to line 31, where we close the file.

On 32 we again open the file, but this time for output, i.e. program to file. We also add the ios::app argument to ensure that we append to the end of the file.

On 33 we write the initial phrase, and then repeat the phrase on 35 to prompt the user for an ideal response. The user’s response is then written, and the file is closed.

The say function on line 46 simply passes the phrase to the voice objects say function for textual and audible output.

Step 3: Voice.h

Create another file called voice.h, and copy the code in the image. You may be questioning the need for this class, but we did this for portability and reusability. The class is actually quite good for any other projects where you want audible output.

This header file doesn’t require much description. This will come in the next step.

Step 4: Voice.cpp

Create another file called voice.cpp, and copy the code in the image. As expected it’s quite straight forward.

We are basically just executing the espeak application from current one, passing the phrase as an argument, hence the call to the system function…. The simplest way of executing cmd commands.

On 13 we use string summation to add our phrase to the command.

On 14 we convert from string to const char* so that we can pass it to the system function.

On 15 we output the text.

On 16 we call system, and execute espeak with the phrase as the argument to be said.

Make sure you put espeak.exe in the immediate directory of the compiled program.

Step 5: Main.cpp

Create another file named main.cpp, and copy the code in the image.

This is where are main function resides, hence where we start the program.

On line 7 we create a learner object and call it AI.

On 13 we begin the main program loop. It is infinite, therefore the only way to stop execution is if you close the application. The code from 13 to 19 is repeated.

Lines 14 to 16 prompt the user for input.

Lines 18 to 19 represent the learner output. We call AI’s respond function and get a response, or teach him what to say.

Step 6: Compilation and Setting Up for First Run

Now we are all done with the code.

Go ahead and compile.

Make sure you add a memory directory to the directory where the exe is. The memory file will be created in this folder on first run. You can actually edit the memory file directly if you wish to correct or add anything!

Also make sure you have the espeak exe in here too.

Now everything should be set up for the first execution :D

<p>What about the Windows.h file anyone else have this problem.( If not then I will edit this comment later when I use a actual IDE not a online one, Thanks!</p>
<p>Hello! Thanks for the tutorial! Do you have any links or other tutorials about weighted strings? I mean, i want to update the intelligence of this chatbot in order to understand and answer phrases without &quot;ready&quot; questions and answers. Any help will be appreciated. Thank you in advance!</p>
<p>if you wanna make perfect chatbot O could recommend you these company and their article about how to create your own chatbot- https://www.cleveroad.com/blog/how-to-create-your-own-chatbot-</p>
<p>I get a error while building main.cpp which says that 'getline' : undeclared Identifier,</p><p>When I include the header file &lt;string&gt; I get an error:</p><p>main.obj : error LNK2001: unresolved external symbol &quot;public: void __thiscall Learner::respond(class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt;)&quot; (?respond@Learner@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?</p><p>Please Help me out</p><p>Using Visual C++ 6.0</p>
<p>where do i put the memory.txt file and what do i put in it</p>
<p>I tried running it in turbo c7 IDE......not compiling at all</p>
<p>You dumb man!!! It is coded in ANSI C++, rather turbo uses its own standards.</p><p>Don't know why people still code in Turbo...</p>
<p>amazing!! thank you so so much! YAYYYY!!!!!!!!!</p>
<p>Did it! It's an awesome program, and a great place to start with AI. I'm learning more about AI and C++, so this has helped to visualize how it all goes together. I had to add a Memory folder with a Memory.txt in it for the program to function, otherwise it would not reply, but it's all working great! I'm more than pleased, and your instructions were easy to follow. I do have a few questions though, how can this program be expanded upon? And what else can we do besides creating a database of responses?</p>
<p>Hello, i made everything but then when i build it and say something like hi the ai responds with nothing and i cant type anything. The only prob i have is with the memory file i dont get it :/</p>
<p>Like i everything works but it doesnt respond to anything</p><p>. i say hi it says hi, i say sup it says sup so i say nice it sais nce :/</p>
<p>@GustavoR31: I face the same issue. It is because the while(!eof()) is being executed infinitely. You can modify the file read to tailor your needs accordingly.</p>
<p>Where is Learner.exe???</p>
<p>I am not getting any memory directory. what should i do?</p>
<p>It says to make one</p>
<p>I'm using code::blocks and it won't compile because of this error:</p><p>C:\Users\Link\Desktop\chatbot\main.cpp|19|undefined reference to `Learner::respond(std::string)'|</p>
<p>@GustavoR31</p><p>For this code to work for me, I have to create and put the file memory.txt in the same directory of my project.</p><p>ps: I use the CodeBlocks IDE.</p>
<p>how do i create a file memory.txt , i tried running it on visual studio 2015(i know basic c++ /learning object oriented)</p>
<p>sorry, I have a problem, when I execute the program it runs, but when i tap something and then appears the line of computer, it doesn&acute;t appears any text and also it doesn&acute;t appears the directory of memmory, how can i solve this?</p>
<p>simplest form of ai i have ever seen...keep up the good work</p>
<p>Thanks shubham_bhatt! The simplicity is a great aspect of this system.</p>
that's cool. I made one just like that once on petit computer(a version of basic for ds) I thought it was cool but none else did :(
<p>Thanks JavaProgrammer! I sure think it's cool, especially when you start to work from this concept, and make the system more intelligent. Perhaps to understand the semantics of a phrase! </p>

About This Instructable

29,198views

54favorites

More by EngyFun:Arduino Library for 28BYJ-48 Stepper Motor and ULN2003 Driver A Learning Chatterbot in C++  
Add instructable to: