Simple easy A.I chatbot example I made

  • 2 Replies
  • 2995 Views
*

Mr.Fox

  • Bumblebee
  • **
  • 35
    • Dreamwalker Software
Simple easy A.I chatbot example I made
« on: April 08, 2016, 09:34:36 am »
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:
Code
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:
Code
 string strInput = Console.ReadLine().ToLower();

Then, it will search if any of the elements in the strHello array has been found like so:
Code
     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:

Code
 //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
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();


        }
    }
}


*

8pla.net

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1302
  • TV News. Pub. UAL (PhD). Robitron Mod. LPC Judge.
    • 8pla.net
Re: Simple easy A.I chatbot example I made
« Reply #1 on: April 08, 2016, 11:34:21 pm »
Great C# example, and a fun topic to discuss.  Here is one common scenario for discussion...

What if the user types, "This is cool."?
Will it match on "hi" in the word "This"  and respond with a greeting?

If the answer is, "Yes" then let's discuss a solution using word barriers in regular expressions. 
Word barriers have a slash letter b format.

Code
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
string user = "Hello there!";

Match matched = Regex.Match(input, @"\b(hello|hi|hey|greetings)\b",RegexOptions.IgnoreCase);

if (matched.Success)
{
    string response = matched.Groups[1].Value;
    Console.WriteLine(key);
}
    }
}

NOTE:  This is untested C# code.  I am on Linux. 

And, let's also discuss other solutions that don't use regular expressions.


My Very Enormous Monster Just Stopped Using Nine

*

Mr.Fox

  • Bumblebee
  • **
  • 35
    • Dreamwalker Software
Re: Simple easy A.I chatbot example I made
« Reply #2 on: April 08, 2016, 11:42:22 pm »
Yeah it's a nice start, No, in this case it would just say "I do not understand what you're saying" because the characters "Hi" are taken as a literal string, not indevidual letters.

That being said, feel free to build on this as much as you want

 


OpenAI Speech-to-Speech Reasoning Demo
by MikeB (AI News )
March 31, 2024, 01:00:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am
Nvidia Hype
by 8pla.net (AI News )
December 06, 2023, 10:04:52 pm
How will the OpenAI CEO being Fired affect ChatGPT?
by 8pla.net (AI News )
December 06, 2023, 09:54:25 pm
Independent AI sovereignties
by WriterOfMinds (AI News )
November 08, 2023, 04:51:21 am
LLaMA2 Meta's chatbot released
by 8pla.net (AI News )
October 18, 2023, 11:41:21 pm

Users Online

305 Guests, 0 Users

Most Online Today: 376. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles