[Tutor] Woody, your personal assistant. Draft 0.1

Nick Lukashevich nickylodeon2000 at gmail.com
Fri Jul 17 17:18:55 EDT 2020


Hello world! My first email here! :) I am Nick D. Lukashevich or NDL, or 
nickelodeon

I am currently on Modules and Functions topic of Learning to Program and 
am very close to starting Handling Files.

So I have come up with something really really fun. :) It's my first 
useful piece of software. :) What it does is it listens to user's 
commands and does what he or she says. Well, currently it can only open 
programs and the list of programs is quite short. But I am really 
excited to learn more :)

I called it Woody just because it sounds sweet and kind. I hope you will 
find it fun too and I will be happy to hear any comments.

The program uses speech recognition module

I installed it using pip3 like so "pip3 install SpeechRecognition"

The module requires PyAudio which I got from my system's repositories

If you run Woody in terminal you'll get a few debug messages about ALSA 
and stuff

Well, there is a helpful 
link(https://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time/17673011#17673011)

But for now I just don't really care, I will learn how to deal with 
these things next :)

When run in IDLE, it looks much prettier

I found out that if your run it this way: "nohup ./woody.py", the 
program will run in background even after you close the shell which is 
what you would expect from an assistant :)

Thank you!

Here is the code:

#!/usr/bin/python3

#############################
# Program   :   Woody.py
# Author    :   N.D.Lukashevich
# Version   :   Draft 0.1
'''
Woody, personal assistant.
'''
#############################

import speech_recognition as spr
import os

# This is a list of programs which Woody can open for you.
# First elements of each sublist are names of programs
# and actual commands to be run by os.system().
# All the rest strings in sublists are other possible ways a user can 
name a program.
# I live in Russia, so I added some names in Russian.
# Case doesn't matter. I think prefixes and postfixes doesn't matter either,
# but I need to test it better :)

programs = [
             ['google-chrome', 'гугл хром', 'браузер'],
             ['tomatoes', 'помидор', 'томат', 'шарик', 'мяч'],
             ['blender', 'блендер'],
             ['supertux2', 'пингвин'],
             ['xonotic-sdl', 'стрел'],
             ['supertuxkart']
             ]

# If a user's command sounds like 'open <program>',
# this function will deal with it
# Commands is a list of words said by a user

def openProgram(commands):
     for command in commands:
         for program in programs:
             for name in range(0, len(program)):
                 # the second case is useful because some possible names
                 # are longer than those in programs list
                 # this check let us not care about different endings
                 if command in program[name] or program[name] in command:
                     os.system(program[0])
                     return

store = spr.Recognizer()
with spr.Microphone() as mic:

     print('$ ', end=' ')
     while True:
         audio_input = store.record(mic, duration=5)

         try:
             # I will implement a way to support Russian too
             # by using language="ru-RU" parameter of recognize_google()
             # It doesn't work sometime, though. Because of my 
programming :)
             # I think maybe using an argument is a way to go like 
"./woody.py ru"
             speech = store.recognize_google(audio_input)

             # speech is a string, here we set it to lowercase for 
convenience
             speech = speech.lower()
             print(speech)
             # convert string to a list, so it will be easier to work 
with commands
             commands = speech.split()

             # 'открой' is the Russian for 'open', it's the same about 
'play' :)
             if 'open' in commands or 'открой' in commands:
                 openProgram(commands)
             # This is yet to be implemented
             if 'play' in commands or 'включи' in commands:
                 playMusic()

             # if a user said something, print a new line
             if len(commands) > 0:
                 print('$ ', end=' ')
             # else stay on the same one
             else:
                 print('',end='')

         except:
             # I am yet to learn about error handling :)
             # Although I do know that this is not the way to do it :)
             print('',end='')



More information about the Tutor mailing list