This is a simple chatbot written in C#, although it's a basic skeleton but you can extend the knowledge base(KB) in external xml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Basic cahtterbot in c# console | |
class Chatterbot2 | |
{ | |
// KB = Knowledge base as string array, this can be extended with an xml file with request & response | |
static string[,] KnowledgeBase = | |
{ | |
{"what is your name","my name is chatterbot2."}, | |
{"hi","hi there!",}, | |
{"how are you","i'm doing fine!"}, | |
{"who are you","i'm an a.i program."}, | |
{"are you intelligent","yes,ofcorse."}, | |
{"are you real","does that question really matters to you?"} | |
}; | |
//search for an answer in the kb | |
static string findMatch(string str){ | |
string result = ""; | |
for(int i=0; i < KnowledgeBase.GetUpperBound(0); ++i) | |
{ | |
if(KnowledgeBase[i,0].Equals(str)) | |
{ | |
result = KnowledgeBase[i,1]; | |
break; | |
} | |
} | |
return result; | |
} | |
//main | |
public static void Main() | |
{ | |
Console.Title="Chatterbot2"; | |
while(true) | |
{ | |
Console.Write("> "); | |
string sInput = Console.ReadLine(); | |
sInput = sInput.ToLower(); | |
string sResponse = findMatch(sInput); | |
if(sInput.Equals("bye") || sInput.Equals("bio")) | |
{ | |
Console.WriteLine("it was nice talking to you user,see you next time!"); | |
Console.In.Read(); | |
break; | |
} | |
else if(sResponse.Length == 0) | |
{ | |
Console.WriteLine("i'm not sure if i understand what you are talking about."); | |
} | |
else | |
{ | |
Console.WriteLine(sResponse); | |
} | |
} | |
} | |
} |