Python Forum
Recursive function not returning expected output...(Python speech recog module)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive function not returning expected output...(Python speech recog module)
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
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)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(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  =)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a function akbarza 9 1,172 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Recursive regular expressions in Python risu252 2 1,205 Jul-25-2023, 12:59 PM
Last Post: risu252
  with open context inside of a recursive function billykid999 1 570 May-23-2023, 02:37 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,151 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Facing problem with Pycharm - Not getting the expected output amortal03 1 854 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  How to print the output of a defined function bshoushtarian 4 1,278 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Why my function is returning None? PauloDAS 6 1,754 Jul-17-2022, 11:17 PM
Last Post: Skaperen
  output correction using print() function afefDXCTN 3 11,063 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  python prints none in function output chairmanme0wme0w 3 2,205 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  Speech Recognition with timestamps DeanAseraf1 3 6,564 Jun-27-2021, 06:58 PM
Last Post: gh_ad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020