Efficiency


EXAMPLE

To show you how easy it is, we will give you a simple experiment to try yourself.

Start with installing a popular library called Chatterbot.

pip install chatterbot

Then import some basic functions to later callback in your code.

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

Now we can start writing some basic functions that will define our bot, for now we'll give it his name and the language it will be speaking is English.

# Create a ChatBot instance
bot = ChatBot('MyBot')

# Create a new trainer for the ChatBot
trainer = ChatterBotCorpusTrainer(bot)

# Train the ChatBot based on the English corpus
trainer.train('chatterbot.corpus.english')

# Function to get bot response
def get_response(user_input):
    response = bot.get_response(user_input)
    return str(response)

# Main function to interact with the bot
def main():
    print("Bot: Hi there! How can I help you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() == 'bye':
            print("Bot: Goodbye!")
            break
        else:
            bot_response = get_response(user_input)
            print("Bot:", bot_response)

if __name__ == "__main__":
    main()

Congratulations, you now have a chatbot that can have simple conversations. From here, you can make further enhancements to this bot, try giving it your own twist. Make the bot import data trough API's, integrate your bot with other sites and apps, the possibilities are endless.

Last updated