DEV Community

Piyush Goyani
Piyush Goyani

Posted on • Originally published at blog.thesourcepedia.org on

Build ChatBot with ChatterBot and Python - Part 1

Introduction

ChatterBot is a machine-learning based conversational dialogue engine build in Python which makes it possible to generate responses based on collections of known conversations. The language-independent design of ChatterBot allows it to be trained to speak any language. Initially, it has no knowledge, then based on training and communication it tries to respond to the user's query accurately. It matches the closest matching response by searching a statement that matches the input and how frequently the response is used. These things are highly customizable with logic adapters, training processes, preprocessors, filters, and other custom behaviour.

Installation

We'll install a chatterbot package with pip. So first, open your terminal and type the following command with the appropriate Python version. Here I'm using v3.7.10 specifically.

pip install chatterbot

Enter fullscreen mode Exit fullscreen mode

Check if the installation is successful, run the below command:

pip show chatterbot

Enter fullscreen mode Exit fullscreen mode

Once done with installation let's start with a basic example.

Create a Bot

First, create a file named greet_bot.py. import ChatBot class from chatterbot library.

from chatterbot import ChatBot

Enter fullscreen mode Exit fullscreen mode

Create a new chatbot named "GreetBot", you can give any name you want.

chatbot = ChatBot("GreetBot")

Enter fullscreen mode Exit fullscreen mode

Training your Bot

Import ListTrainer from sets of available trainers to train bot with greet data so it can respond to our inputs.

from chatterbot.trainers import ListTrainer

Enter fullscreen mode Exit fullscreen mode

Create trainer instance of ListTrainer with chatbot parameter, and train with list of sample greeting data.

trainer = ListTrainer(chatbot)
train_data = [
    "Hello",
    "Hi",
    "Hi, how are you?",
    "I'm doing great.",
    "Greetings!",
    "How do you do?",
    "Great, thanks for asking!",
    "What's up"
] 
trainer.train(train_data)

Enter fullscreen mode Exit fullscreen mode

Retrieve response from Bot

To retrieve a response from trained bot use get_response method of bot object with input parameter.

response = chatbot.get_response('hi')
print(response)

Enter fullscreen mode Exit fullscreen mode

Final Code

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

chatbot = ChatBot('GreetBot')

trainer = ListTrainer(chatbot)

train_data = [
    "Hello",
    "Hi",
    "Hi, how are you?",
    "I'm doing great.",
    "Greetings!",
    "How do you do?",
    "Great, thanks for asking!",
    "What's up"
]

trainer.train(train_data)

response = chatbot.get_response('Hi')
print(response)

Enter fullscreen mode Exit fullscreen mode

To run a bot, run the script greet_bot.py

python greet_bot.py

Enter fullscreen mode Exit fullscreen mode

image.png

Conclusion/Notes

Thank You for reading this article. I hope this helps in some ways and get a basic idea of how to get started with bot building with Python and the Chatterbot library. This was a very basic explanation and demo. Share, Like, Subscribe and stay tuned for the same and other bot-building tutorials.

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay