A Simple Voice-Based Chatbot For Hindi Language
A Voice-Based Chatbot acts like a voice assistant to accept your vocal
queries, process them and produce the vocal answer to satisfy the query.
In this article, we will create a simple voice-based chatbot for Hindi
language. The chatbot will accept the Hindi queries through vocal commands,
will process them with text-matching principle and will revert with appropriate
vocal answer in Hindi.
To do
this following Python libraries will be helpful.
- speech_recognition:
Speech
recognition is a machine's ability to listen to spoken words and identify
them. You can then convert the spoken words into text, make a query or
give a reply. The code uses this library to recognize the input Hindi
query. (installation: pip install SpeechRecognition)
- translate: Translate is a simple but
powerful translation tool written in python with with support for multiple
translation providers. The code uses this library to translate the input
Hindi query into English for text-matching. (Installation: pip install
translate)
- gTTS: A gTTS (Google
Text-to-Speech), is a Python library to interface with Google Translate's
text-to-speech API. It can process the .mp3 input file. (Installation: pip
install gTTS). The code uses this library to translate the response in
Hindi. You can reuse the above mentioned library 'translate' instead.
However the code uses this specific library to explore to the geeks, the
another available option.
- pygame: It is a free and
open-source cross-platform library for the development of multimedia
applications including audio and video. (Installation: pip install
pygame). The code uses this library to save and play the translated
response in Hindi.
The Code is explained below.
Step1 : Import the libraries you need.
Python3
# import required libs
import random
import speech_recognition as sr
from translate import Translator
from gtts import gTTS
from datetime import datetime
from pygame import mixer
Step 2: Receive A Vocal Query in Hindi: To do this we create and
initialize object of speech_recognition. It helps to recognize the audio
query obtained from the device-microphone as the source. It recognizes
the spoken Hindi words using the recognize_google().
Python3
# the function is coded to take Hindi input queries
and recognize them
def Receive_In_Hindi():
r =
sr.Recognizer()
with
sr.Microphone() as source:
print('Listening')
r.pause_threshold = 0.7
audio
= r.listen(source)
try:
print("Recognizing")
Query = r.recognize_google(audio, language='hi-In')
Answer_In_Hindi(Query)
#
handling the exception, so that assistant can
#
ask for telling again the command
except Exception as e:
print(e)
print("Didn't Get That ! Please say again..")
return "None"
return Query
Step 3:
Translate the vocal query to English and process response : This is required as our code
does 'text-matching' for processing the query. The input vocal Hindi query
needs to be translated into English first. The Translator() helps in
this task. Then a response is generated in English text format.
Python3
def Answer_In_Hindi(Question):
print("Your Query:",Question)
#translated_text = GoogleTranslator(source='hi',
target='en').translate(Question)
translator=
Translator(from_lang="hindi",to_lang="english")
translation = translator.translate(Question)
if
"today" in translation: # check
if the query is about today
answer_text="today is "+ datetime.today().strftime('%A')
translator=
Translator(from_lang="english",to_lang="hindi")
translation = translator.translate(answer_text)
Process_For_Hindi(translation)
if
"age" in translation: # check if the query is about age
answer_text="I am 22 years old"
translator=
Translator(from_lang="english",to_lang="hindi")
translation = translator.translate(answer_text)
Process_For_Hindi(translation)
Step 4:
Process the English-textual response into Hindi Audio: As we need the final response in
Hindi-audio format, we need to convert the processed textual response into
Hindi audio. The gTTS() does this task. Since all responses need to be
saved, we generate a random text file name and save the audio responses.
Python3
def Process_For_Hindi(translation):
print("Chatbot Response:"+translation)
answer_text=str(translation) #translate the English response into Hindi
and convert it into audio file
myobj =
gTTS(text=answer_text, lang='hi', slow=False)
file_name="chat_aswer"+str(random.randint(0,100))+".mp3"
myobj.save(file_name)
mixer.init()
mixer.music.load(file_name)
mixer.music.play() #play the
audio response
Output:
The Input Query and the Response
The input
should be provided in Hindi to the microphone of the device. When the query is
processed, the code plays the Hindi audio file generated as the response of the
query.
No comments:
Post a Comment