1 00:00:00,900 --> 00:00:02,620 - [Instructor] Next up, let's take a look at our 2 00:00:02,620 --> 00:00:04,350 translate function which once again 3 00:00:04,350 --> 00:00:07,430 is going to get called twice in this application. 4 00:00:07,430 --> 00:00:11,260 Once to convert the English text into Spanish text, 5 00:00:11,260 --> 00:00:15,240 and once to covert Spanish text into English text. 6 00:00:15,240 --> 00:00:19,760 Now, just like we did for converting speech to text, 7 00:00:19,760 --> 00:00:22,920 we do need to create an object of the appropriate 8 00:00:22,920 --> 00:00:25,250 Watson developer cloud class 9 00:00:25,250 --> 00:00:26,860 for the purpose of interacting 10 00:00:26,860 --> 00:00:28,900 with the language translator service. 11 00:00:28,900 --> 00:00:32,680 And the name of that class is LanguageTranslatorV3. 12 00:00:32,680 --> 00:00:35,520 This particular object requires a couple 13 00:00:35,520 --> 00:00:37,415 of arguments. 14 00:00:37,415 --> 00:00:41,570 One is a version string, and the other is your API key, 15 00:00:41,570 --> 00:00:43,830 and I'll point out again that depending on 16 00:00:43,830 --> 00:00:45,168 where you are in the world, 17 00:00:45,168 --> 00:00:50,168 there could be versions of the Watson Language Translator 18 00:00:50,560 --> 00:00:53,990 that still require username and password 19 00:00:53,990 --> 00:00:55,450 rather than API key. 20 00:00:55,450 --> 00:00:57,600 So once you sign up for the service 21 00:00:57,600 --> 00:00:59,480 the way I demonstrated earlier, 22 00:00:59,480 --> 00:01:01,974 if you see a username and password 23 00:01:01,974 --> 00:01:03,750 rather than an API key, 24 00:01:03,750 --> 00:01:06,150 then that's what you're going to need to use 25 00:01:06,150 --> 00:01:07,990 as arguments when you create 26 00:01:07,990 --> 00:01:10,670 your LanguageTranslatorV3 object. 27 00:01:10,670 --> 00:01:13,590 Now as far as this version string is concerned, 28 00:01:13,590 --> 00:01:16,240 as of the time of this recording, 29 00:01:16,240 --> 00:01:19,150 this is the most up to date version string. 30 00:01:19,150 --> 00:01:21,210 And basically what IBM does 31 00:01:21,210 --> 00:01:24,450 is if they ever make breaking changes, 32 00:01:24,450 --> 00:01:28,140 they will create a new version string, 33 00:01:28,140 --> 00:01:31,300 and that way if you have code that's dependent 34 00:01:31,300 --> 00:01:34,530 on this version, as long as you keep using 35 00:01:34,530 --> 00:01:36,270 the correct version string, 36 00:01:36,270 --> 00:01:38,370 your code will continue to work. 37 00:01:38,370 --> 00:01:42,060 And they talk about that in the online documentation 38 00:01:42,060 --> 00:01:43,710 for this service. 39 00:01:43,710 --> 00:01:45,060 So we create the object, 40 00:01:45,060 --> 00:01:46,950 which we're calling language translator, 41 00:01:46,950 --> 00:01:49,170 and this is the key line of code 42 00:01:49,170 --> 00:01:51,310 that is going to submit the text 43 00:01:51,310 --> 00:01:54,170 to the server for the purpose of translation. 44 00:01:54,170 --> 00:01:55,600 And then we will get back, 45 00:01:55,600 --> 00:01:58,780 just like in the preceding piece of this code, 46 00:01:58,780 --> 00:02:02,100 a detailed response object containing 47 00:02:02,100 --> 00:02:05,300 the JSON, the JavaScript object notation, 48 00:02:05,300 --> 00:02:08,180 representing the translated data. 49 00:02:08,180 --> 00:02:09,470 So as you can see here, 50 00:02:09,470 --> 00:02:12,310 we call the objects translate method. 51 00:02:12,310 --> 00:02:14,550 We give it two arguments in this case, 52 00:02:14,550 --> 00:02:17,120 the text that we wish to translate, 53 00:02:17,120 --> 00:02:19,900 and again we're going to give it a model 54 00:02:19,900 --> 00:02:22,640 which says what the server should use 55 00:02:22,640 --> 00:02:24,630 to perform the translation. 56 00:02:24,630 --> 00:02:28,240 So our models are going to be en-es 57 00:02:28,240 --> 00:02:31,950 as a string, if we're going from English to Spanish. 58 00:02:31,950 --> 00:02:36,120 Or they're going to be es-en as a string 59 00:02:36,120 --> 00:02:38,680 if we're going from Spanish to English. 60 00:02:38,680 --> 00:02:42,040 So this is going to return the detailed response object, 61 00:02:42,040 --> 00:02:44,820 the get result method of that object 62 00:02:44,820 --> 00:02:48,110 gives us back the JavaScript object notation. 63 00:02:48,110 --> 00:02:50,490 So with that said, for a moment I'm going to switch back 64 00:02:50,490 --> 00:02:53,480 over to the PowerPoint slides where I have a diagram 65 00:02:53,480 --> 00:02:57,620 once again that shows you what this response looks like. 66 00:02:57,620 --> 00:03:02,620 So again, we get back what appears to be a Python dictionary 67 00:03:03,035 --> 00:03:07,970 and this dictionary contains a few key value pairs. 68 00:03:07,970 --> 00:03:10,290 The translations key value pair 69 00:03:10,290 --> 00:03:12,320 is a list of translations. 70 00:03:12,320 --> 00:03:15,640 If you give a list of strings to translate 71 00:03:15,640 --> 00:03:20,020 to the service, you'll get back a list of dictionaries 72 00:03:20,020 --> 00:03:22,970 representing those translations. 73 00:03:22,970 --> 00:03:25,580 And it has a couple of additional key value pairs 74 00:03:25,580 --> 00:03:26,880 of information as well. 75 00:03:26,880 --> 00:03:28,560 But the key thing that we need 76 00:03:28,560 --> 00:03:31,760 is this translations key value pair. 77 00:03:31,760 --> 00:03:33,740 Now, this translations key value pair 78 00:03:33,740 --> 00:03:37,410 is a list that happens to contain one object, 79 00:03:37,410 --> 00:03:39,980 so we're going to navigate our way 80 00:03:39,980 --> 00:03:42,310 through this nested data structure 81 00:03:42,310 --> 00:03:44,820 to access the translation string, 82 00:03:44,820 --> 00:03:47,550 which contains the Spanish representation 83 00:03:47,550 --> 00:03:52,220 in our example of "where is the closest bathroom?" 84 00:03:52,220 --> 00:03:54,710 So let's switch back over to the code. 85 00:03:54,710 --> 00:03:57,740 And we've once again broken this down 86 00:03:57,740 --> 00:04:01,110 into a series of steps for the purpose of-- 87 00:04:01,110 --> 00:04:02,400 whoops, sorry about that. 88 00:04:02,400 --> 00:04:05,490 For the purpose of accessing the pieces of information. 89 00:04:05,490 --> 00:04:09,470 So first we get the key translations 90 00:04:09,470 --> 00:04:13,740 which gives us back the overall translations list. 91 00:04:13,740 --> 00:04:16,800 Then from the translations list we grab element zero, 92 00:04:16,800 --> 00:04:19,930 and again, we only have one element in this example. 93 00:04:19,930 --> 00:04:22,390 But had we supplied a list of strings 94 00:04:22,390 --> 00:04:25,680 to translate, then we would have a list of objects 95 00:04:25,680 --> 00:04:27,790 containing the translated results. 96 00:04:27,790 --> 00:04:30,870 And then finally, for that first object, 97 00:04:30,870 --> 00:04:33,200 we get its translation key, 98 00:04:33,200 --> 00:04:36,730 which gives us back the actual string representation 99 00:04:36,730 --> 00:04:41,260 of that translation to either Spanish or English 100 00:04:41,260 --> 00:04:44,403 depending on which phase of the application we're in.