A VERY SIMPLE VOICEBOT

Data Science Training Session

VoiceBot needs the below components from the Backend side:

  1. Train Data : Questions & Related Answers List for the Chat Bot to be trained
  2. Microphone to receive the question from the user
  3. Text Matching Algorithm : User Question & Train Data Questions matching Algorithm
In [6]:
# coding: utf-8

# In[ ]:


import numpy as np 
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer        
import pandas as pd
import random

# Function for Generating the Replies

def get_response(userInput):
        greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']
        g_response = ' Hello. I am Contibot.How may I help you? '
        question = ['How are you?','How are you doing?']
        responses = ['I am doing great!',"I'm fine"]
        
        if userInput in greetings:                     # 1. Basic Greetings Reply
            return(g_response)
        elif userInput in question:                    # 2. Enquiry Greetings Reply
            random_response = random.choice(responses)
            return(random_response)
        else:                                          # 3. For other questions match with the Qstn Bank
            df=pd.read_csv('C:\\Users\\uia94128\\Desktop\\ChatBot\\HRDATA.csv')
            kw_list=df['key_words'].tolist()
            kw_list.append(userInput)                  # 4. Add the user Question to Qstn Bank List
            
            tfidf_vectorizer = TfidfVectorizer()       # 5. Tf Idf Score for the Questions List
            tfidf_matrix = tfidf_vectorizer.fit_transform(kw_list) 
            cos=cosine_similarity(tfidf_matrix[-1], tfidf_matrix)
            maxi=np.argsort(cos)[-1][-2]               # 6. Cosine similarity for finding similar questions
            
            return(df['msg'][maxi])                    # 7. Return the corresponding Answer from the Qstn Bank
In [5]:
import speech_recognition as sr                   # import the library
r = sr.Recognizer()                               # initialize recognizer
with sr.Microphone() as Microphone:               # mention source it will be either Microphone or audio files.
    print("Speak Anything :")
    audio = r.listen(Microphone)                  # listen to the source
try:
    text = r.recognize_google(audio)              # use recognizer to convert our audio into text part.
    print("You said : {}".format(text))
except:
    print("Sorry could not recognize your voice") # In case of voice not recognized clearly
    
print(get_response(text))
Speak Anything :
You said : how many general leaves are given
An employee can avail 12 days of general leave per calender year