The program will speak the name of a common animal backwards and the user will have to say (with their voice) what they think it is in the form “I think it is X.” If they get it right, their guess will be displayed in green, if they get it wrong, their guess will be displayed in red.
slaminaactivities folder in your repository folder (create it if necessary)index.html to Slamina!The idea here is really simple, but it has a few moving parts. We’ll need to obtain a list of animal names in a useful format to make the user guess. We’ll need to figure out how to make p5.speech say an animal’s name backwards. And we’ll need to have it listen to a user command representing a guess. Finally, we’ll need to display whether they got it right somehow.
Here’s a plan:
When we work on a project like this, one thing we need is nicely formatted data (the animal names) that we can easily use in our program. Fortunately for us, Darius Kazemi (a wonderful creative programmer worth looking up) has a corpora project that contains all kinds of lists in easily accessible form. We’ll use an animal list he provides.
animals in your script and assign the array (paste it in)Now we have a nice long list of animal names we can use for our guessing game.
At the heart of this experience is having the computer say an animal’s name backwards. The novelty being that it can be quite hard to work out what animal it is in this case. So we need to start the game by choosing an animal’s name from the array, and then say it with p5.speech’s speech synthesis, but backwards! Given that it’s best to trigger audio from a user event, we’ll do this when the user clicks.
Because it’s a little more complicated than is easy to just work out, we’ll have the string reversing part provided below.
First we need to have a way to track the current guess…
currentAnimal and set it to an empty string. This is where we’ll store the animal the user is guessing.Now we need to be able to reverse a string of text.
/**
Reverses the provided string
*/
function reverseString(string) {
// Split the string into an array of characters
let characters = string.split(``);
// Reverse the array of characters
let reverseCharacters = characters.reverse();
// Join the array of characters back into a string
let result = reverseCharacters.join(``);
// Return the result
return result;
}
// You could also combine all this into
// return string.split(``).reverse().join(``);
// But that's for sure more confusing to learn from
Finally, we need to trigger p5.speech to say the reversed animal name…
index.html (feel free to use the CDN version or the downloaded version)const called speechSynthesizer at the top of the program and assign it a p5.speech synthesizer objectmousePressed() function and in itanimals array to currentAnimal (remember you can use p5’s random() function for this)reverseAnimal and assign the reverse of currentAnimal to it by using reverseString(currentAnimal)speechSynthesizer to speak reverseAnimalNow when the program starts, if the user clicks, they will hear the name of a random animal backwards.
We want p5.speech to listen to the user and hear when they make a guess. To do this we need to be able to listen to a guessing command like “I think it is…” and then capture what the actual guess is. This is kind of non-trivial, but we’ll get into it. In order to give some feedback, we’ll display the guess in the console.
First we want to set up the recognizer…
const called speechRecognizer at the top of the program and assign it a p5.speech recognizer objectsetup()continuous property to truehandleSpeechInput (we’ll write that function in a moment)start() methodNow we need to handle guessing…
currentAnswer containing an empty string (they haven’t guessed yet!)mousePressed() functionhandleSpeechInput()In it we need to extract the animal name the user hopefully said so we can store it in currentAnimal. This bit is kind of intense, so a bit more hand holding here
guessedAnimal and give it a default value that will make sense if the user sees it after the program didn’t understand them for some reason, something like… "what??"speechRecognizer.resultValue is true (this will help us avoid situations where the recognizer didn’t really hear something well).split method to break a lowercase version of the user’s command into two parts, the part before “i think it is” and the part after “i think it is”, which should be the animal name. split will return an array, ideally with two elements (the before part and the after part). This is by no means easy, so here’s what that would look like:let lowerCaseResult = speechRecognizer.resultString.toLowerCase();
let parts = lowerCaseResult.split(`i think it is `);
(Post-video) Notice the space after “i think it is” here. That’s really important because we want the part after that to be the start of the actual answer. So we have to “eat” the space by including it in the split.
parts has a length greater than 1 (e.g. it managed to break the string into two parts, meaning it found “i think it is “ inside the string).parts[1] to guessedAnimal (that is, the part of the user’s command after “I think it is”, which is their answer)handleSpeechInput() function, assign guessedAnimal to currentAnswerconsole.log() to print out currentAnswer so you can see what it isNow if the user starts the program and clicks, they should be able to say “I think it is dog” (for example) and you should see “dog” in the console
Finally, we want to display the user’s guess to the user themselves so they can understand whether they were right or wrong!
setup()draw()if statement that checks if the current answer is correct (does it equal the current animal?). If it does, set the fill (for the text) to green. If it doesn’t, set the fill (for the text) to redcurrentAnswer variable as text on the canvasNow if the user starts the program and clicks, they should be able to say “I think it is dog” (for example) and see the word “dog” appear on the canvas in green (if it’s right) or red (if it’s wrong). Hey presto! Our guessing game is complete!
Currently our program works, but there are some places where we could make it nicer. Consider breaking the code down into smaller functions like sayAnimalBackwards(animal), displayAnswer(), and nextQuestion() to ask a new question.
As with any simple program, we can imagine all kinds of improvements and changes?
}