A.I, Data and Software Engineering

Create a trainable chatbot with java


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.

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.

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.

and dont forget to update the answer method:

Testbot class

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

And the result

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.

Pin It on Pinterest


You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.