How to Make a Robot That Talks Back Using AIML in C#

by AaronThacker946 in Circuits > Robots

12082 Views, 30 Favorites, 0 Comments

How to Make a Robot That Talks Back Using AIML in C#

final bot working.PNG

In this instructable I will show you how to make a AIML AI from scratch using the AIML libraries. The AI can respond to most question's asked from the default template AIML provide. In this tutorial I will show you how to setup AIML with the default template.

You need the following software to continue:

1. Visual studio (https://www.visualstudio.com/) (Visual studio 2015 community edition is free!)

2. AIML binaries (https://sourceforge.net/projects/aimlbot/files/) (Click "Download AIMLbot2.5.zip (18.0 kB)" in the middle of the page)

2. AIML templates (Attached with this instructable called "templates.zip") (Source from the AIMLConsole at https://sourceforge.net/projects/aimlbot/files/aim...

3. A basic knowledge of C# (I will try to go into as much detail as possible)

Downloads

Creating the Project

creating project.PNG
Project type.PNG

Once installed Visual Studio you want to head over to "File", Click "New" Then select a project. A dialogue window will appear where you can choose your project type, project name and project location.

The window will have a list of projects depending on what is selected on the left hand side. In our case we want to go to "Templates", Then "Visual C#".

Once you have selected this you will see a list of project types we can choose from, The most easiest and most used templates are:

"Windows Form Application", As in applications with a GUI.
"Console Application", For everything without a GUI, in a terminal window.
"Class Library", A collection of classes which can be used in other programs.

In our case we want to select Console Application as we dont need any fancy button's to talk to a AI, just a window were we can type text and display it.

The Name and the Solution Name can be the same as we are not making a multi-project typed project.
The file location can be where ever you choose, But I recommend saving it where visual studio recommends in the "Visual Studio 2015/Projects" folder in "My Documents" as it pay's off in the future to have a organised list of projects.

Then click "Ok" at the bottom of the window.

Importing the Packages

IDE.PNG
reference.PNG
solution exploere with imported files.PNG
build events.PNG

Gather up the files you downloaded in step one (The AIMLbot.dll and the templates)

If you dont have the solution explorer on the right hand side of your screen showing all of the files in your project, Go to "View" at the top of the IDE and click "Solution Explorer".

In the solution explorer Right click on the file which only contains your project name, It has a icon of "C#" in a box.

Then you want to click "Add", Then "Reference..." This dialogue box allow's us to add items to our project. If you chose a "Class Library" Then this is how you would add it to another project.

Now we want to add a external DLL to the project, So we would click "Browse" and the bottom of the Dialogue and navigate to the "AIMLbot.dll" you downloaded in step 1.

Once the file is selected, Click "Add" then "Ok" on the reference window.

The final step to prepare our project is to add the templates to our project. Open the templates.zip file attached in step 1 and drag both folders into the Project (The file which only contains your project name, It has a icon of "C#" in a box.)

Your solution explorer should now look like mine. The last step is to make sure Visual Studio copy's both folders into the build dictionary when you click run. This can be achieved by right clicking on the project file, clicking "Properties", Then into "Build Events" .

Copy these two lines into the "Pre-build event command line"

XCOPY "$(ProjectDir)aiml" "$(TargetDir)aiml" /E /I /F /Y /D
XCOPY "$(ProjectDir)config" "$(TargetDir)config" /E /I /F /Y /D

This uses the xcopy command to copy both of our dictionary (aiml and config) to the build folder, and the tag /D will only copy the files if they have been updated from the source (The project folder).

You can go ahead and close the Properties tab as we are done with importing the packages and setting the project up. You should be left with a tab which contains the basics for a command line application.

The Code

The Code.PNG

Copy the code down into the Program.cs tab, I have explained the code using comments, These dont effect the program as they are commented out by two slashes "// ". Visual Studio show's the comment as green.

Bot AI = new Bot(); // This defines the object "AI" To hold the bot's infomation

AI.loadSettings(); // This loads the settings from the config folder

AI.loadAIMLFromFiles(); // This loads the AIML files from the aiml folder

AI.isAcceptingUserInput = false; // This swithes off the bot to stop the user entering input while the bot is loading

User myUser = new User("Username", AI); // This creates a new User called "Username", using the object "AI"'s information.

AI.isAcceptingUserInput = true; // This swithces the user input back on

while (true) { // This starts a loop forever so the bot will keep replying and accepting input

Request r = new Request(Console.ReadLine(), myUser, AI); // This generates a request using the Console's ReadLine function to get text from the console, the user and the AI object's.

Result res = AI.Chat(r); // This sends the request off to the object AI to get a reply back based of the AIML file's.

Console.WriteLine("Robot: " + res.Output); // This display's the output in the console by retrieving a string from res.Output

} // This is the end of the loop which would repeat back to the "While (true)" part

The parts required are in the box above which go in the "static void Main(string[] args)" section

Results

Running.PNG

If you click the "Start" button at the top of the window your application will start, and the project is finished!

The console window should open with nothing inside. Type something. Press Enter.
If all went well you should see the robot respond.

To customise the bot more, go into the config folder in your project and change some settings to make it more personal

What I would do better:

  • I would write my own template's for the bot or expand on the templates from the creator of the library.
  • Make an API to expand the bot into something which can have multiple users.