- [Instructor] Next up, let's take a look at our
translate function which once again
is going to get called twice in this application.
Once to convert the English text into Spanish text,
and once to covert Spanish text into English text.
Now, just like we did for converting speech to text,
we do need to create an object of the appropriate
Watson developer cloud class
for the purpose of interacting
with the language translator service.
And the name of that class is LanguageTranslatorV3.
This particular object requires a couple
of arguments.
One is a version string, and the other is your API key,
and I'll point out again that depending on
where you are in the world,
there could be versions of the Watson Language Translator
that still require username and password
rather than API key.
So once you sign up for the service
the way I demonstrated earlier,
if you see a username and password
rather than an API key,
then that's what you're going to need to use
as arguments when you create
your LanguageTranslatorV3 object.
Now as far as this version string is concerned,
as of the time of this recording,
this is the most up to date version string.
And basically what IBM does
is if they ever make breaking changes,
they will create a new version string,
and that way if you have code that's dependent
on this version, as long as you keep using
the correct version string,
your code will continue to work.
And they talk about that in the online documentation
for this service.
So we create the object,
which we're calling language translator,
and this is the key line of code
that is going to submit the text
to the server for the purpose of translation.
And then we will get back,
just like in the preceding piece of this code,
a detailed response object containing
the JSON, the JavaScript object notation,
representing the translated data.
So as you can see here,
we call the objects translate method.
We give it two arguments in this case,
the text that we wish to translate,
and again we're going to give it a model
which says what the server should use
to perform the translation.
So our models are going to be en-es
as a string, if we're going from English to Spanish.
Or they're going to be es-en as a string
if we're going from Spanish to English.
So this is going to return the detailed response object,
the get result method of that object
gives us back the JavaScript object notation.
So with that said, for a moment I'm going to switch back
over to the PowerPoint slides where I have a diagram
once again that shows you what this response looks like.
So again, we get back what appears to be a Python dictionary
and this dictionary contains a few key value pairs.
The translations key value pair
is a list of translations.
If you give a list of strings to translate
to the service, you'll get back a list of dictionaries
representing those translations.
And it has a couple of additional key value pairs
of information as well.
But the key thing that we need
is this translations key value pair.
Now, this translations key value pair
is a list that happens to contain one object,
so we're going to navigate our way
through this nested data structure
to access the translation string,
which contains the Spanish representation
in our example of "where is the closest bathroom?"
So let's switch back over to the code.
And we've once again broken this down
into a series of steps for the purpose of--
whoops, sorry about that.
For the purpose of accessing the pieces of information.
So first we get the key translations
which gives us back the overall translations list.
Then from the translations list we grab element zero,
and again, we only have one element in this example.
But had we supplied a list of strings
to translate, then we would have a list of objects
containing the translated results.
And then finally, for that first object,
we get its translation key,
which gives us back the actual string representation
of that translation to either Spanish or English
depending on which phase of the application we're in.