Skip to main content

A Simple Chatbot Using Python

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 am gonna show you how to create a chatbot, to open a system application on your device using python.

Steps to follow

  • First import the modules needed to make a chatbot.
  • Then make the chatbot get input from the user ie., to which application he/she needs to open.
  • Next, make the chatbot react to the 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

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

import os
import pyttsx3 as p
#speak() converts text to speech
p.speak("Welcome to my chatbot")   
#loops until user specify to quit
while True: 
	p.speak("what can I do for you ?  ")
	userinput = input()
	#opens Microsoft Edge
	if ("run" in userinput and ("Microsoft Edge" in userinput)) or ("execute" in userinput and ("Microsoft Edge" in userinput)) or ("open" in userinput and ("Microsoft Edge" in userinput)) or ("launch" in userinput and ("Microsoft Edge" in userinput)) :
    	p.speak("Microsoft Edge will be launched soon")
    	os.system("msedge") 	
	#opens Powerpoint
	elif ("run" in userinput and ("Powerpoint" in userinput)) or ("execute" in userinput and ("Powerpoint" in userinput)) or ("open" in userinput and ("Powerpoint" in userinput)) or ("launch" in userinput and ("Powerpoint" in userinput)) :
    	p.speak("Paint will be opened soon")
    	os.system("POWERPNT") 
	#It stops the execution
	elif ("exit" in userinput) or ("quit" in userinput):
    	p.speak("Thanks for opting me . Share your smiles")
    	break
	#If user inputs anything wrong, it shows don't support    	
    else:
    	print("don't support")

Comments

Popular posts from this blog

Easy Way To Build An Audiobook Using Python

  An audiobook is nothing but a book that was recorded in an audio format. It can also be stated as a book that is being read aloud. Audiobook helps in improvising one's vocabulary, comprehension and pronunciation of words. Hope some of you guys are bookaholic but lazy to read it on your own. So it's time to create your own audiobook by using few codes of Python. This will definitely make you enjoy audiobooks without any subscription fee, that was imposed on platforms like Audible, Scribd, etc. Prerequisite Python has a huge number of modules that contains reusable code which performs desired functions when invoked. It must be installed into your system before using its functionalities. So here we going to grab just 2 modules from a bunch of python modules prevailing around. Install PyPDF2 module, using                     pip install PyPDF2 Install pyttsx3 module, using             ...

How to Create A Voice Recorder Using Python

  A voice recorder records a sound or a voice of a person and converts it into an audio file. The file can be stored in different audio formats like MP3 format, Waveform Audio File (WAV), Advanced Audio Coding (AAC), Audio Interchange File Format (AIFF), etc. and later it can be transferred to other devices. Any device that is capable of recording a sound or a voice is said to be a voice recorder by default. In this article, you will learn to build a voice recorder with a few lines of code in Python. Where do We Use Voice Recorder? Some of the applications of a voice recorder are listed below: Interviews Handling Meetings Lectures/Seminars in Educational Institutions Audiobooks for kids Learning foreign languages Translating a word to another language Google search by voice A quick voice message to a person instead of typing Podcasts Voice assistants like Google assistants, Alexa, Siri, etc., Essentials The first and foremost thing needed to build a voice recorder using Python is t...

Translating text from one language to another using python

The definition of a translator is a  person who helps people who speak different languages to communicate or who take something  (such as a speech or a book) in one language and who puts it into a different language for people to understand.   This functionality can be achieved using the  googletrans module in python. googletrans module     The googletrans module is one of the fast and flexible libraries in python that implements Google Translate API,  to detect the language of text automatically and to convert text from one language to another. By default, all the languages are translated to English. Installation      This module can be installed by using,                pip install googletrans     googletrans can be used after installation and importation of the module               import googletrans or       ...