Python Forum

Full Version: Recursive function not returning expected output...(Python speech recog module)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Windows 7, Python 2.7, Bing text-to-speech API.


I' m having  trouble understanding why when I run the function it seems to be outputting


None
instead of
'1'
if the recursion bit of the function is activated.






import win32com.client
BING_KEY = "XXXXXXXX"
import speech_recognition as sr
import win32com.client as wincl

s = sr.Recognizer()

def hey_pc():
    print(" Hey PC = Command")
    with sr.Microphone() as source:
          audio = s.listen(source)
          try:
               x= (s.recognize_bing(audio, key=BING_KEY))
               if x in ['hey PC']:
                  return 1
               else: hey_pc()
          except:
               print('Try again')
               hey_pc()


# Main part of program below

t = hey_pc()
print t
So the code is my attempt at a hobby project involving voice commands.
If the speech recognizer correctly interprets my command "Hey PC", and outputs it as a string ('Hey PC')  or similar variant, I want it to output 1, or else I want it to to keep retrying until it correctly outputs 1.

Below are some of my outputs in IDLE when running the script.




Hey PC == Command 
1

#outputs 1 if the SR guesses/interprets my voice correctly on the first try (thus the recursive part of the function is not
activated)
Hey PC == Command
 Hey PC == Command
 Hey PC == Command
 Hey PC == Command
None

# outputs None, if doesn't guess correctly on the first attempt but eventually guesses it after a number of attempts
Hey PC == Command
 Try again
 Hey PC == Command
 Hey PC == Command
 None


# Once again outputs None, if doesn't guess correctly on the first attempt but eventually guesses it after a number of attempts
I don't understand why I"m getting "None." I want the function to recursively continue until it can correctly output one.


Thank you.
First of all, I think it would be much better to rewrite this as a loop rather than recursion.

Second, the reason it doesn't return anything if it recuses is that you don't return anything from the recursive call. Instead of hey_pc(), you want return hey_pc(). That way the return value of one will be passed up the recursion chain to the initial call at the bottom of the program.
(Jan-07-2017, 04:18 AM)ichabod801 Wrote: [ -> ]First of all, I think it would be much better to rewrite this as a loop rather than recursion.

Second, the reason it doesn't return anything if it recuses is that you don't return anything from the recursive call. Instead of hey_pc(), you want return hey_pc(). That way the return value of one will be passed up the recursion chain to the initial call at the bottom of the program.


Thanks for the reply. I will test out your suggestion.

As for your loop suggestion, are you suggesting a WHILE loop? I will think about it see if I can come up with something.


Edit: Thanks seems to be working once I replace the two hey_pc to return hey_pc. I will play around with it some more.
I notice you're importing "win32com.client".  If you want to have to computer "talk" to you, you would need to initialize it first, for example:

cari = win32com.client.Dispatch('Sapi.SpVoice')
then you can use it like so:

cari.Speak("my name is Cari, how are you today?")
It also acts similar to the print() function (in ver 3 and above, but I presume the same applies to ver 2) so you could do something like:

cari.Speak("{} {}, my name is Cari, how are you today?".format(greeting, user_name))
I've not tried it on Python v2.x, but should work.  It uses the 'default' voice in Speech Recognition so that has to be turned on. (Located in Control Panel --> Speech Recognition)
(Jan-07-2017, 03:29 PM)sparkz_alot Wrote: [ -> ]I notice you're importing "win32com.client".  If you want to have to computer "talk" to you, you would need to initialize it first, for example:

cari = win32com.client.Dispatch('Sapi.SpVoice')
then you can use it like so:

cari.Speak("my name is Cari, how are you today?")
It also acts similar to the print() function (in ver 3 and above, but I presume the same applies to ver 2) so you could do something like:

cari.Speak("{} {}, my name is Cari, how are you today?".format(greeting, user_name))
I've not tried it on Python v2.x, but should work.  It uses the 'default' voice in Speech Recognition so that has to be turned on. (Located in Control Panel --> Speech Recognition)



Thanks. Yeah, I have it working in another part of the code. It works fine with Python 2.7  =)