A Simple Chatbot Using Python

Venkatalakshmi Subramaniam
Analytics Vidhya
Published in
2 min readOct 4, 2021

Chatbot or chatterbot is a software program that simulates conversations made by humans through voice or text chats. It is more commonly used in messaging applications. As we all know Alexa, Zo chatbot, Faketalk, Watson Assistant are some of the chatbots built on Artificial Intelligence and Natural Language Processing.

Here we are going to see how to create a chatbot, to open a system application on your device using python.

Steps to follow

  • The first and foremost thing is to import modules needed to make a chatbot.
  • Then make the chatbot get input from the user ie., to which application h/she needs to open.
  • Next, make the chatbot react to user input by opening an application specified by the user.
  • Make the chatbot work again and again until the user specifies to “quit”.

Modules needed

The major modules needed to build a chatbot are

  • pyttsx3 module
  • os module

pyttsx3 module

Modules are nothing but a library that consists of reusable code that performs desired function when it gets invoked. It can be invoked into a program code with the help of an import statement. pyttsx3 is one of the modules in python which takes text as input and results in speech as an output.

speak( )

pyttsx3.speak( ) function results in the conversion of text to a speech where the text is passed as an argument.

Syntax

pyttsx3.speak(“text to speech”)

For example,

pyttsx3.speak(“Hey how can I help you”)

This command will not print the text passed as an argument in the console, rather it will speak as “Hey how can I help you”. This function is mainly used in building chatbots and applications like translators. Instead of using traditional input and output statements in python, this module helps in improvising the method of getting input from the user.

os module

In python, the os module directly communicates to the operating system of your device. It comes under python’s standard utility module. It provides functionalities that are operating system dependent.

system( )

To make the Operating System of your device interact with applications installed on your device, use the function system( ) from the os module.

Syntax

os.system(“application_name”)

For example,

os.system(“notepad”)

This command will launch a notepad on your device.

Its time to build a chatbot

Here I’ve given a code snippet for a chatbot to open “Microsoft Edge” and “MS Powerpoint” from a command prompt.

Code Snippet

Thanks for learning!!!

--

--