- [Instructor] Next let's look at
the speech_to_text function.
Which as you saw we call twice
in our run translator function.
Once to get English-spoken audio into text
and once to get Spanish-spoken audio into text.
And in each case we are going to need
to create a SpeechToTextV1 object.
Now as you can see here when we create the object,
we need to give it our API key
in order to access the Watson Speech to Text service.
So here's our keys module that we imported
at the beginning of the script.
And inside that module we have some variable names
that we defined where you stored your versions
of the API keys.
Now we take this object that we've created
and assign it to the variable stt
which is a common shorthand for speech to text.
And we're now going to use that to invoke the Web service.
And you can see we're doing that here
in the context of a with statement.
The with statement is going to open a file
that we specified as an argument
to our speech to text function.
And that file is going to be opened
for reading in binary format.
So we're going to get the data out of that file.
We're going to call that object
that we use to manipulate the data audio_file.
And the result that we're going to get back
is going to be the result of calling these speech to text
object's recognize function or method rather.
Now as you can see, we're using several arguments here.
We have three of them in particular,
and there are other ones as well
that you'll find in the online documentation.
But here we're using three key ones.
The audio argument specifies the file
from which we're going to get the bytes
that are going to get sent over
to the speech to text service.
The content_type argument is the so-called media type.
That used to be called the MIME type which was a shorthand
for Multipurpose Internet Mail Extensions.
That was relatively recently renamed media type
for more modern uses.
So MIME was something that's been around
for a couple of decades now for attachments on email.
And then finally the last argument that we specified here
is the model argument, which if you recall
from the earlier discussion for going from English speech
to English text, we're going to take advantage
of the US English Broadband model.
So again if you go way back up here,
you can see in Step 2 the actual name
of that model that's going to be passed through
to the Web service.
So coming back down to our speech_to_text function here.
So this call to recognize
is actually what invokes the Web service,
and what we're going to get back from that
is what they call a detailed response object.
And the detailed response object
is actually a JavaScript object notation object
that looks like this.
And here in this diagram,
we've associated with these boxes
that you see on the screen the source code line numbers
where corresponding statements are going to access
this JavaScript object notation representation.
Now this is a JSON representation which,
when it gets converted into a Python object,
is basically a set of nested dictionaries and lists.
This particular dictionary has two key value pairs.
The first one is called results which is a list,
and the second one is called result_index.
And in the results list,
you're going to have the transcription results.
Now one of the things that's interesting
about the speech to text service
is it can give you back final results
which is what we are getting here.
Final equals true in this case.
But it can also give back intermediate results.
So for instance if you've ever watched a live newscast
that has closed captions showing up at the bottom
of the screen, you'll sometimes notice
that the transcription results are not as accurate
as they could be, and then they'll suddenly change.
And that's because they are using tools
that are dynamically processing the spoken word into text,
and as they get a better representation of that text,
it will actually get updated automatically at that point.
Now if you provide the arguments to the recognize method
that enable you to get intermediate responses,
you might actually have multiple elements
within the results list.
In our case we have just one dictionary in that results list
which represents the final transcription for demo purposes.
Now within that dictionary
there's an alternatives key value pair
that has a list object in it,
and the only element we got back
in this case was the final transcription.
And you can see there's two different key value pairs
in this object.
One is the confidence level of the transcription.
So according to IBM they're 98.3 percent sure
that they got our transcription correct,
and the transcript that they gave us back
for the question I asked is "where is the closest bathroom"
which is precisely what I asked in that video.
So unlike what we did in the data mining Twitter chapter
or lesson rather where the tweepy module gave us properties
for accessing all of this information
in the context of Watson, we do have to actually navigate
into this structure to get down to the the transcript level
and access the information
that is coming back to us as text.
So with that said
let me switch back over to the source code here.
So what's going to happen
is we will get back from this recognize method
the detailed response object representing the JSON,
and then on that object we call get_result
which gives you back the actual JavaScript object notation
but as a Python object consisting of dictionaries and lists.
We store that in the variable called result,
and then we can start to pick apart the information
in that JSON object.
So first thing we do,
and we broke it down into a bunch of statements here
to make it easier for you to see what's going on.
So the first thing we do
is we access the main object's results key,
and I'm gonna bounce back and forth
between my diagram and the code here
so you see what's going on.
So we get the results key
which is a list containing dictionaries.
So now we have that list.
Next up we go into the results list and access element zero
which is the element
that contains the speech recognition result for us.
So again we have a list, and it contains only one object
which is another dictionary.
Next up we go into.
Whoops.
Next up we go into that list.
(clears throat) Excuse me.
I'm sorry.
Dictionary rather and we access its alternatives key.
So showing you again here's the alternatives key
which is a list that contains one dictionary.
Then we go into that list and get the only dictionary
that's in there at element zero.
And then we go into that dictionary,
and we access its transcript key
which is going to give us back the string
"where is the closest bathroom" in our example.
Now once we have that string, we're going to return it
from our function speech_to_text,
and that's when it will get displayed to you
as part of the script's execution.