Putting It All Together
Putting it all together: a “Guess the Word” game. Now that you’ve seen the basics of recognizing speech with the SpeechRecognition package, let’s put your newfound knowledge to use and write a small game that picks a random word from a list and gives the user three attempts to guess the word. The full script is included in the course files and is fairly long. The overall structure of the program involves a function that recognizes speech,
recognize_speech_from_mic(), and a main game loop, which chooses a random word and allows the user to make guesses.
The first key,
"success", is a Boolean that indicates whether or not the API request was successful. The second key,
"error", is either
None or an error message indicating that the API is unavailable or the speech was unintelligible.
"transcription" key contains the transcription of the audio recorded by the microphone. The function first checks that the
microphone arguments are of the correct type and it raises a
TypeError if either is invalid.
Next, the function uses the microphone input as a source and firstly calls
.adjust_for_ambient_noise() to calibrate the recognizer to the changing noise conditions before every recording. The
.listen() method is then used to record microphone input.
The success of the API request, any error messages, and the transcribed speech are stored in the
"transcription" keys of the
response dictionary, which is returned by the
for loop is used to manage each user attempt at guessing the chosen word. The first thing inside the
for loop is another
for loop that prompts the user, at most,
PROMPT_LIMIT times for a guess, attempting to recognize the input each time with the
recognize_speech_from_mic() function and storing the dictionary returned to the local variable
Otherwise, the API request was successful, but the speech was unrecognizable. The user is warned and the
for loop repeats, giving the user another chance at the current attempt. Once the inner
for loop terminates, the
guess dictionary is checked for errors. If any occurred, the error message is displayed and the outer
for loop is terminated with
break, which will end the program execution. If there weren’t any errors, the transcription is compared to the randomly selected word.
.lower() method for string objects is used to ensure better matching of the
guess to the chosen word. The API may return speech matched to the word
"apple" with a lower- or uppercase first letter, and either response should count as a correct answer.
Become a Member to join the conversation.