A.I, Data and Software Engineering

Create a trainable chatbot with java

C

chatbot is a software application used to conduct an on-line chat conversation via text or text-to-speech, in lieu of providing direct contact with a live human agent. This article will demonstrate a short fun coding piece to simulate a chatbot. Its objectives are:

  • Accept input from the console as a string and give an answer
  • If the bot cannot find the answer, they ask for training and add the new answer to the knowledge base.
chat bot fun
🙁

The chatbot class

Firstly, we define our dumb chatbot with a simple knowledge base using HashMap with some simple conversation sentences.

import java.util.*;
/**
 * This class is a dummy chat bot
 */
public class MyBot {
    HashMap<String, String> knowledge = new HashMap<String, String>();
    /**
     * This is a default constructor.
     */
    public MyBot() {
        knowledge.put("Hi", "Hello... Please to meet you!");
        knowledge.put("Hello", "Hi yo");
        knowledge.put("how are you?", "Great! And you?");
        knowledge.put("what time is it", "Look at your watch!");
    }
 }

Next, we can add a method answer to give our bot the ability to response to some user input. The algorithm is simple: it matches the user input with the keys in the database, if found, then the bot will return the corresponding value.

/**
* @param question This is user input as string
*/
public void answer(String question) {
    Set<String> keys = knowledge.keySet();
    for (String key : keys){
        String lowerKey = key.toLowerCase();
        String lowerQuestion = question.toLowerCase();
        if (lowerKey.contains(lowerQuestion)) {
           System.out.println("Bot: " + knowledge.get(key));
           return;//break
        }
    }
}

You may find that the method does not take into account all words from the input. Which you may want to improve the solution for your own version.

So how about we enter a random sentence that is not in the database? In this case, our bot will ask for training and try to save the user suggestion to its knowledge.

public void trainMe(String question) {
    System.out.println("Bot: Sorry, Im dumb! How should I reply");
    System.out.print("User suggestion: ");
    Scanner sc = new Scanner(System.in);
    String userInput = sc.nextLine();
    knowledge.put(question, userInput);
}

and dont forget to update the answer method:

public void answer(String question) {
    ...
    trainMe(question);
}

Testbot class

We create a test class for the bot. When user enter “stop it”, our programme will terminate.

import java.util.*;
public class TestBot {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String userInput = "";
        MyBot chatbot = new MyBot();
        while(!userInput.equalsIgnoreCase("stop it")) {
            System.out.print("User: ");
            userInput = sc.nextLine();
            chatbot.answer(userInput);
        }
        sc.close();
    }
}

And the result

User: hi
Bot: Hello... Please to meet you!
User: How are you?
Bot: Great! And you?
User: You are cool
Bot: Sorry, Im dumb! How should I reply
User suggestion: Thank you!
User: time
Bot: Look at your watch!
User: cool
Bot: Thank you!
User: stop it

Wrapping up

It is not a real intelligent chatbot but definitely it is fun to play with. Also, this bot is trainable even without an AI approach. With a better database, this bot can definitely help users to get some simple questions. For other coding challenges, check this out.

Add comment

💬

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Categories