Analytics Vidhya
Published in

Analytics Vidhya

Can you store employee information using a voice bot in Python?

Photo by Ben White on Unsplash

How to get started?

This project requires you to have a basic understanding of SQLITE3 module in Python since you will use the module to create the database. You will also need a basic understanding of classes and instances in Python.

First step: Create the employee class

The employee class will be used to create the employees first name, last name, and pay. This class is just a few lines of code and could be written in the project itself, however, creating the employee class in a different python file was a way for me to get started with OOP Programming and understand how classes can be imported to another python file.

class Employees:
"""

This class saves the employee first name, last name, and pay. It's imported in the project SQLITE3

"""
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay

Second step: Creating the project

Modules to import for the project:

# modules for the voice bot
import speech_recognition as sr
from gtts import gTTS
import playsound
# modules for the database
import random
from employee_class import Employees
import sqlite3
import os
import re

Create the voice bot:

def speak_text(speak):
“””
This function makes the voice bot speak a specific command.
“””
rand = random.randint(1, 10000)
filename = ‘file’ + str(rand) + ‘.mp3’
tts = gTTS(text=speak, lang=’en’)
tts.save(filename)
playsound.playsound(filename)
os.remove(filename)

Create the function get_audio():

def get_audio():
"""
This function takes input from the user through the microphone and returns an exception if the command is not understood by the voice bot
""" r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ‘’
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print(‘Exception’ + str(e))
return said
speak_text(‘welcome to my SQLITE project that stores employee information into a database.’)
speak_text(‘you can enter the data manually or use the voice bot’)`

Create the database:

conn = sqlite3.connect('employee.db')

c = conn.cursor()

''' Create a table called 'employees' using SQLITE3 to store data'''


c.execute(""" CREATE TABLE IF NOT EXISTS employees (
employee_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
first TEXT,
last TEXT,
pay INTEGER
)""")

conn.commit()

Create a set of functions that inserts, deletes, or gets employees from the table:

In order to make changes to your employee table and make things easy, you will have to create functions that inserts an employee, deletes an employee, or gets every employee information from the table.

def insert_emp(emp): 
“””
This function inserts the employee data into the table
“””
with conn:
c.execute(“”” INSERT INTO employees VALUES (?,?,?,?) “””, (None, emp.first, emp.last, emp.pay))
conn.commit()
def delete_emp(id_emp):   
""" This function deletes the employee from the data """ with conn:
c.execute(""" DELETE FROM employees WHERE employee_id=:employee_id""",
{'employee_id': id_emp}
)conn.commit()
def get_employees():  
# gets all employee names
"""
This function prints out all the employees in the data """
c.execute(""" SELECT * FROM employees """)
return c.fetchall()
conn.commit()

Create the employee_info() function:

def employee_info(): while True:speak_text(‘please enter the first name of the employee’) print(‘\nPlease enter the first name of the employee:-’) 
first_name = input()
if re.findall(‘[a-z]’, first_name):
break
while True:
speak_text(‘please enter the last name of the employee’)
last_name = input() print(‘Please enter the last name of the employee:-’)if re.findall(‘[a-z]’, last_name):
break
while True:
speak_text(‘please enter the pay of the employee’)
print(‘Please enter the pay of the employee:-’) pay_emp = input()

if re.findall(“\d”, pay_emp):
break user_emp = Employees(first_name, last_name, pay_emp)

speak_text(‘employee was successfully added to the database’)
return insert_emp(user_emp)

Create the select_program() function:

def select_program(): 
“””
This function makes the user select from writing manual commands to edit the employee data or use the voice bot
“””
print(‘Type in voice bot to use the \”voice bot\” to enter your employee data or type in \”manual\” to enter the data manually :- ‘)
select = input()
if select == ‘voice bot’ or select == ‘ voice bot’:
voice_commands()
elif select == ‘manual’ or select == ‘ manual’:
manual_commands()

else:
select_program()

Create the manual_commands() function:

def manual_commands():
"""
This function is executed when the user decides to edit the data manually. The if-elif-else statements are used to update, delete or get employees
"""print('1. \nWrite \"update\" to add another employee into the data')
print('2. Write \"delete\" to delete an employee from the data') print('3. Write \"get employees\" to delete an employee from the data')
print('update, delete, or get employees....?')
command = input()
if command == 'update' or command == ' update': employee_info()
speak_text('Here are the employees up until now')
print('Here are the employees up until now ' + '\n' + str(get_employees()))
loop_commands_function()
elif text == 'delete':

print("Which employee do you want me to delete. Please select the id of the employee from the data below: " + '\n'
+str(get_employees()))
delete_employee = input()
delete_emp(delete_employee)
speak_text('employee successfully deleted')
speak_text('Here are the employees up until now ')
print('Here are the employees up until now' + '\n' + str(get_employees()))

loop_commands_function()
elif command == 'get employees' or command == ' get employees': speak_text('Here are all the employees')
print(str(get_employees()))
loop_commands_function()
else:
print('\n' + 'Please say from one of the following commands')
print('\n' + manual_commands())
def loop_commands_function(): 
“””
This function asks the user if he/she wants to continue with the program
“””
print(‘Do you want me to continue? Write Yes or No’)
cont = input()
if cont == ‘Yes’ or cont == ‘ Yes’:
select_program()
elif cont == ‘No’ or cont == ‘ No’:
speak_text(‘thank you for your time’)
speak_text(‘goodbye now’)
exit()
else:
speak_text(‘please type in yes or no’) print(‘Please type in Yes or No!’)

Final step: Create the voice_commands() function

def voice_commands():   
""" This function uses the voice bot to insert, delete, or get employees from the data. Similar to the manual_commands() function, but it just uses voice. """
speak_text('\nSay update to insert another employee into the data....')
speak_text('Say delete to delete the employee from the data....') speak_text('Say get employees to get the information of all employees...')
print('\n1. Say update to insert the employee into the data') print('2. Say delete to delete the employee from the data') print('3. Say get employees to get the information of all employees')
text = get_audio()
if text == 'update':
employee_info()
speak_text('Here are the employees up until now ')
print('Here are the employees up until now ' + '\n' + str(get_employees()))
loop_commands_function()
elif text == 'delete':
print("Which employee do you want me to delete. Please select the id of the employee from the data below: " + '\n' +str(get_employees()))
delete_employee = input()
delete_emp(delete_employee)
speak_text('employee successfully deleted')
speak_text('Here are the employees up until now ')
print('Here are the employees up until now' + '\n' + str(get_employees()))
loop_commands_function()
elif text == 'get employees':
speak_text('\nHere are all the employees') print(str(get_employees()))
loop_commands_function()
else:
speak_text('Please say from one of the following commands')
voice_commands()

Order of functions in the program

It’s essential to execute the function by calling it. Therefore, you will need to call the following functions in the order mentioned to run the program:

  1. employee_info()
  2. select_program()

Final thoughts

I apologize if the program has some weird function names, however, I tried my best to explain the program and its functions. Feel free to use the source code to either add functionality to the program or change the function names.

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Syed Hassan Ali Rizvi

An enthusiastic teen passionate about trading and software engineering!