DEV Community

Cover image for Build Your First Python Chatbot In 5 Minutes - "Pyter".
Deepak Raj
Deepak Raj

Posted on • Edited on • Originally published at codeperfectplus.herokuapp.com

Build Your First Python Chatbot In 5 Minutes - "Pyter".

This article was originally published at CodePerfectPlus

In the age of AI, chatbots are getting popular day by day. It's industry’s newest tools designed to simplify the interaction between humans and computers. From E-commerce to Healthcare institutions, Everyone wants to use Chatbot for interaction with the user.

What is a Chatbot

A chatbot is a software application used to conduct an online chat conversation via text or text-to-speech, in lieu of providing direct contact with a live human agent. — According to Wikipedia.

Build Your First Python Chatbot In 5 Minutes

Types of Chatbot

Chatbots can be categorized into two types

  • Rules-Based
  • Self Learning

The Rules-Based:- Rules-based chatbots trains a chatbot to answer question-based on pre-trained rules. these type of chatbot are good for simple queries.

Self-learning chatbot:- Self-learning chatbots are based on machine learning algorithms and they are smarter than rules-based chatbots. They can learn on their own.

How Chatbot Works

AI-powered chatbots are intelligent and can also learn on their own. They use Natural language processing and machine learning algorithm to learn and feed on data.

E.g: Google Assistant, Alexa, Siri

Intelligent AI- chatbot feed on user data and learn and try to improve themselves. They analyze it with complex AI- Algorithms and output response as text or voice.
Since these bots can learn from behaviour and experiences, they can respond to a wide range of queries and commands.

Today, we will create python chatbot using ChatterBot library. Let's get started!

1. Create Virtual Environment

pipenv is a python library to create virtual environment easily.

pip install pipenv
pipenv install
Enter fullscreen mode Exit fullscreen mode

2. Install Libraries

We Will Use ChatterBot library to create Simple Python Chatbot. Install chatterbot and chatterbot_corpus with the help of pip command.

pipenv install chatterbot
pipenv install chatterbot_corpus
Enter fullscreen mode Exit fullscreen mode

3. Create and Train the Chatbot

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

BOTNAME = "Pyter"

def start():
    bot = ChatBot(BOTNAME,
        logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'default_response': 'I am sorry, but I do not understand.',
            'maximum_similarity_threshold': 0.90,
        },        
    ],
        preprocessors = [
            "chatterbot.preprocessors.clean_whitespace",
        ],
        input_adaptor="chatterbot.input.TerminalAdaptor",
        output_adaptor="chatterbot.output.TerminalAdaptor",
        database_uri='sqlite:///database.sqlite3')

    trainer = ChatterBotCorpusTrainer(bot)

    # Train based on the english corpus
    trainer.train(
        "chatterbot.corpus.english",
        "chatterbot.corpus.english.greetings",
        "chatterbot.corpus.english.conversations",
        )

    print(f"Hello I am {BOTNAME}")

    while True:
        try:
            bot_input = input("You: ")
            bot_respose = bot.get_response(bot_input)
            print(f"{BOTNAME}: {bot_respose}")

        except(KeyboardInterrupt, EOFError, SystemExit):
            break

if __name__ == "__main__":
    start()
Enter fullscreen mode Exit fullscreen mode

Get Full Code On Github

GitHub logo codeperfectplus / Python-ChatBot

Python ChatBot Implementation using chatterbot library

Python 3.7 issues forks stars License

Visitor Count

header

This article was originally published at http://codeperfectplus.herokuapp.com/build-your-first-python-chatbot-in-5-minutes

What is a Chatbot

A 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. — According to Wikipedia.

Types of Chatbot

Chatbots can be categorized into two types

Rules- Based Self Learning The Rules Based:- Rules based chatobots trains a chatbot to answer question based on pre trained rules. these type of chatbot are good for simple queries.

Self learning chatbot:- Self learning chatbots are based on machine learning algorithms and they are smarter than rules based chatbots. They can learn on their own.

Author




More Articles by Author

Join for Weekly Updates.

React ❤️ to encourage Author.

Top comments (5)

Collapse
 
abuqaiselegant profile image
abuqaiselegant

I am having error.
NameError Traceback (most recent call last)
in ()
21 database_uri='sqlite:///database.sqlite3')
22
---> 23 trainer = ChatterBotCorpusTrainer(bot)
24 # Train based on the english corpus
25 trainer.train(

NameError: name 'bot' is not defined

Collapse
 
codeperfectplus profile image
Deepak Raj

That issue was due to indentation error. You can find full code error.

Python 3.7 issues forks stars License

Visitor Count

header

This article was originally published at codeperfectplus.herokuapp.com/buil...

What is a Chatbot

A 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. — According to Wikipedia.

Types of Chatbot

Chatbots can be categorized into two types

Rules- Based Self Learning The Rules Based:- Rules based chatobots trains a chatbot to answer question based on pre trained rules. these type of chatbot are good for simple queries.

Self learning chatbot:- Self learning chatbots are based on machine learning algorithms and they are smarter than rules based chatbots. They can learn on their own.

Author




Collapse
 
abuqaiselegant profile image
abuqaiselegant

problem resolved, thank you

Thread Thread
 
codeperfectplus profile image
Deepak Raj

Welcome

Collapse
 
drakeentity profile image
Coding_Ninja

It's awesome brother