Simple easy A.I chatbot example I made

  • 2 Replies
  • 3376 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
  • *
  • 1307
  • 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

 


Requirements for functional equivalence to conscious processing?
by DaltonG (General AI Discussion)
November 19, 2024, 11:56:05 am
Will LLMs ever learn what is ... is?
by HS (Future of AI)
November 10, 2024, 06:28:10 pm
Who's the AI?
by frankinstien (Future of AI)
November 04, 2024, 05:45:05 am
Project Acuitas
by WriterOfMinds (General Project Discussion)
October 27, 2024, 09:17:10 pm
Ai improving AI
by infurl (AI Programming)
October 19, 2024, 03:43:29 am
Atronach's Eye
by WriterOfMinds (Home Made Robots)
October 13, 2024, 09:52:42 pm
Running local AI models
by spydaz (AI Programming)
October 07, 2024, 09:00:53 am
Hi IM BAA---AAACK!!
by MagnusWootton (Home Made Robots)
September 16, 2024, 09:49:10 pm
LLaMA2 Meta's chatbot released
by spydaz (AI News )
August 24, 2024, 02:58:36 pm
ollama and llama3
by spydaz (AI News )
August 24, 2024, 02:55:13 pm
AI controlled F-16, for real!
by frankinstien (AI News )
June 15, 2024, 05:40:28 am
Open AI GPT-4o - audio, vision, text combined reasoning
by MikeB (AI News )
May 14, 2024, 05:46:48 am
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

Users Online

381 Guests, 0 Users

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

Articles