So a while back I started coding my own (what I called, layer based A.I) chatbot which basically allows you to talk with it, and it'll search for keywords and grammar to try and assess what has been said, it works like this.
You give it an array, in the case of a greeting could be something like:
string[] strHello = { "hello", "hi", "hey" };
You could add as much as you wanted to this, like "hi sir", "yo", "good day" etc
User input>Hello there
The program takes this input, puts it all in lower case and trims it:
string strInput = Console.ReadLine().ToLower();
Then, it will search if any of the elements in the strHello array has been found like so:
foreach (string element in strHello)
{
if (strInput.Contains(element))
{
Console.Write("Hello sir. ");
blRecognizedSomething = true;
break;
}
}
If a matching element has been found, it will return with "Hello sir." to the user, if not, it will cycle through all the other things, and then go to it's default:
//Nothing understood
if (blRecognizedSomething == false)
Console.Write("I didn't understand anything you just said. ");
blRecognizedSomething = false; //reset
A cool thing here is that it can also check things like grammar, for instance, if the user input ends with a ? then it recognizes a question, if it ends with ! it will recognize you're shouting.
This code is something I did quite a bit ago as proof of concept and it's very advanced now with several thousands of lines of code. However I felt I should share this as some may learn from it and get ideas.
Full code
/*
Coded by mailto:Craig.fox@hotmail.co.uk
http://Dream-walker.weebly.com/
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_AI_testing
{
class Program
{
string[] strCreator = { "create", "created", "made", "coded", "master" };
string[] strUserIsReferingToOneSelf = { " me ", " i ", "i " };
string[] strHello = { "hello", "hi", "hey" };
string[] strBye = { "bye", "goodbye", "see ya", "chow" };
bool blRecognizedSomething = false, exit = false;
void interperate()
{
while (!exit)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nSay something: ");
string strInput = Console.ReadLine().ToLower();
Console.ForegroundColor = ConsoleColor.Cyan;
foreach (string element in strHello)
{
if (strInput.Contains(element))
{
Console.Write("Hello sir. ");
blRecognizedSomething = true;
break;
}
}
foreach (string element in strBye)
{
if (strInput.Contains(element))
{
Console.WriteLine("Bye! ");
blRecognizedSomething = true;
exit = true;
break;
}
}
foreach (string element in strCreator)
{
if (strInput.Contains(element))
{
Console.Write("I was created by Craig Fox craig.fox@hotmail.co.uk ");
blRecognizedSomething = true;
break;
}
}
foreach (string element in strUserIsReferingToOneSelf)
{
if (strInput.Contains(element))
{
Console.Write("You are reffering to yourself I think. ");
blRecognizedSomething = true;
break;
}
}
//grammar
if (strInput.Contains("!"))
{
Console.Write("Please don't shout, I have a headache. ");
blRecognizedSomething = true;
}
if (strInput.Contains("?"))
{
Console.Write("You shouldn't ask me questions yet, I am currently stupid. ");
blRecognizedSomething = true;
}
//end grammar
//Nothing understood
if (blRecognizedSomething == false)
Console.Write("I didn't understand anything you just said. ");
blRecognizedSomething = false; //reset
}//end while
//end interp
}
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Initialized L.B.A.I coded by Craig Fox\n______________________________________________\n");
Program AI = new Program();
AI.interperate();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n______________________________________________\nFinished");
Console.ReadLine();
}
}
}