Monday, 9 May 2016

Create Your Own Personal Assistant in Python [How To]

Do you want to create an awesome personal assistant in python that would respond to whatever you say? You have come to the right place! This post talks about various modules available in Python that can be used to create an assistant that can recognize speech, convert text to speech and do all sorts of stuff like opening various programs. You can extend it further to get information from the internet and process it, such as showing weather forecast, or do a sentiment analysis of tweets or
process the public data available out there. What it can do is only limited by your imagination!

Prerequisites



First of all, you need to install a few modules. Make sure that python and pip installed on your system. Then type the following command:
sudo pip install SpeechRecognition pyttsx

This will install:

  • SpeechRecognition, a library for performing speech recognition

  • pyttsx, a cross platform text-to-speech converter


Personal Assistant in Python: Step by Step Guide



Let's begin creating the assistant now! Open your favourite text editor (mine is Vim). First, we will import the required modules:
import speech_recognition #For converting speech to text
import pyttsx #To convert text to speech
import os #To execute bash commands

Now, we will write a function that will listen to us (wish I had a girlfriend like that :P ). This will convert the spoken words into text using google speech recognition.
#Function that listens what I say and converts it into text
def listenToMe():
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source:
        audio = r.listen(source)
    try:
        text = r.recognize_google(audio)
        return text
    except speech_recognition.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    return "I could not understand what you just said"

Then, we will create a function that will speak to us, that is, it will convert text to speech.
#Function that converts text to speech
def saySomething(text):
    #initialising text to speech engine
    print 'TEXT:',text
    engine = pyttsx.init()
    engine.setProperty('rate',190) #Change number of words per minute
    engine.say(text)
    engine.runAndWait()
    engine.stop()

Finally, we create another function that will understand what we spoke. You can extend this function according to your own needs and add more functionality to your assistant.
#A function that will check what we said and take an action accordingly
def checkWhatIWant():
    #open a particular application
    if 'open' in whatISaid:
        if 'terminal' in whatISaid:
            command = 'gnome-terminal' #Bash command to open a new terminal
        else:
            positionOfOpen = whatISaid.index('open')
            command = whatISaid[positionOfOpen+5:]+'&' #Open independent of current process
        if not os.system(command): #command returned 0; meaning it was successfuly opened
            saySomething('Opened %s'%(command) )
        else:
            saySomething('Sorry, I couldd not open %s'%(command) )

    if 'how are you' in whatISaid:
        saySomething('I am fine, what about you?')

Now, we will test the above functions. Type the following lines at the end of the file.
saySomething('Hello! How are you? What do you want me to do?')
whatISaid = listenToMe()
print whatISaid
checkWhatIWant()

Now save the file as whatever.py (I named it assistantBlog.py). To test it, open the terminal and type:
python assistantBlog.py

You will hear the welcome message. To check if our functions work properly, say 'open terminal' in your microphone. (Note: You will require an active internet connection in order for this to work). It will open a new terminal window. Cool, right? You can even make this program run in the background and listen to your commands while you work on something else.





Complete Code



The complete code can be found here on my github profile.

This is not fully functional and requires a lot of improvements. But it will give you a good idea where to start. How will you use it in your project? Do share with us!

No comments:

Post a Comment