1 00:00:00,150 --> 00:00:03,580 All right, it's time for an exercise, and I think this is going to be fun. 2 00:00:04,320 --> 00:00:08,700 I want to use what we've learned up until now to build a translator. 3 00:00:09,540 --> 00:00:12,110 Let's say somebody gives you a massive text file. 4 00:00:12,810 --> 00:00:18,210 I mean, in our case, this is going to be the text file, but you can grab any text file that you want. 5 00:00:18,360 --> 00:00:19,920 It doesn't have to be a text file. 6 00:00:19,920 --> 00:00:20,850 It can be anything. 7 00:00:20,850 --> 00:00:22,070 It could be a PDF file. 8 00:00:22,080 --> 00:00:29,580 It could be just a word document and type in a bunch of sentences in here, whatever you want. 9 00:00:30,570 --> 00:00:38,190 And just save it, in my case, I just have this now somebody just told you, hey, I want you to translate 10 00:00:38,190 --> 00:00:40,470 this into, let's say, Japanese. 11 00:00:41,580 --> 00:00:49,620 Based on what you just learned about modules and files in Python, can you build a tool that allows 12 00:00:49,620 --> 00:00:54,700 us to run a command through that file, read it and then translate it into Japanese? 13 00:00:55,170 --> 00:01:00,810 Now, if you really want to get challenged, you can pause the video and try it out, or if you want 14 00:01:00,810 --> 00:01:04,140 a hint, what module or library to use. 15 00:01:04,350 --> 00:01:09,390 Well, then keep watching because I'm going to show you the recommended package to use. 16 00:01:10,430 --> 00:01:20,030 OK, so what I actually did was I Googled Python offline, translate using the PI by index. 17 00:01:20,420 --> 00:01:26,540 Now, I wanted an offline translation service because if you look there's something like Google translation 18 00:01:26,540 --> 00:01:27,110 that you can do. 19 00:01:27,290 --> 00:01:34,540 But what that does is actually make a call to the Google servers to do Google Translate for you. 20 00:01:35,090 --> 00:01:40,310 And that gets pretty complicated when we talk about APIs and API keys, something that we're going to 21 00:01:40,310 --> 00:01:41,930 worry about later on in the course. 22 00:01:42,050 --> 00:01:48,950 I wanted something offline so I can download the translation service offline and just be able to use 23 00:01:48,950 --> 00:01:49,490 it right here. 24 00:01:50,000 --> 00:01:53,870 So the one that I found was, as I was Googling, I think it was called. 25 00:01:57,790 --> 00:02:04,780 Yeah, so this is just the translation service, it looks like it has some stars and forks on GitHub, 26 00:02:04,790 --> 00:02:05,740 so I always like that. 27 00:02:05,740 --> 00:02:07,720 I always like checking out their repository. 28 00:02:08,590 --> 00:02:09,970 So translate Python. 29 00:02:10,700 --> 00:02:11,710 Everything looks good. 30 00:02:11,710 --> 00:02:13,720 You can read about the documentation here. 31 00:02:15,500 --> 00:02:20,660 Hasn't been updated in two years, really, but you know what, it's it's not that bad. 32 00:02:20,840 --> 00:02:21,420 Do the job. 33 00:02:22,100 --> 00:02:28,520 So using this package, you can try and read through the documentation and see if you can figure out 34 00:02:28,520 --> 00:02:33,260 how to solve this problem of translating that file into Japanese. 35 00:02:33,680 --> 00:02:35,590 You can porzel video here, give it a go. 36 00:02:35,600 --> 00:02:37,340 Otherwise, I'm going to provide the answer. 37 00:02:39,220 --> 00:02:44,140 Now, the first thing I'm going to do is before I even install this, I want to make sure I can read 38 00:02:44,140 --> 00:02:44,630 this file. 39 00:02:44,770 --> 00:02:47,050 So once again, I'm going to say. 40 00:02:48,500 --> 00:02:52,760 Try and I'll have a file with. 41 00:02:54,360 --> 00:03:02,520 Open, we'll see where the file is, let's say, else we have the test file right in here so we can 42 00:03:02,520 --> 00:03:02,970 just do. 43 00:03:05,690 --> 00:03:08,060 And say, test, dot text. 44 00:03:09,580 --> 00:03:11,410 And then I'll say as my. 45 00:03:16,620 --> 00:03:18,450 Well, that the mode as. 46 00:03:19,830 --> 00:03:25,500 Read for now, because we just want to read the file, but at least this way we're being explicit and 47 00:03:25,500 --> 00:03:27,600 we're saying, hey, this is exactly what we want to do. 48 00:03:28,500 --> 00:03:31,830 And then finally, I'll just for now print. 49 00:03:33,190 --> 00:03:40,960 My file just, you know, that it's working and we'll add some exceptions in here, it's except not 50 00:03:40,960 --> 00:03:45,880 exception and we can just, let's say, do file not found. 51 00:03:49,640 --> 00:03:56,780 Say, as he and we'll just for fun, say here, print, check your. 52 00:03:57,930 --> 00:03:59,700 File path cyle. 53 00:04:01,910 --> 00:04:06,560 And leave it at that, if I run this code, let's say Python three. 54 00:04:07,530 --> 00:04:09,520 Script, Dotti. 55 00:04:10,350 --> 00:04:12,900 All right, I have my name is Andre Awesome. 56 00:04:14,480 --> 00:04:20,390 Let's install the package, so over here I can see that I have to install translate, that's what the 57 00:04:20,390 --> 00:04:21,120 package is called. 58 00:04:21,910 --> 00:04:27,410 Now, I'll use pipe three in my case to make sure that I'm using the Python version three. 59 00:04:28,550 --> 00:04:34,910 And say and still translate now I've actually done this before just because well. 60 00:04:36,010 --> 00:04:38,620 I tested this out beforehand, so it's already installed. 61 00:04:40,920 --> 00:04:46,950 So let's clear that out and read about how we can use this package so you can see here the usage so 62 00:04:46,950 --> 00:04:48,540 I can use it from the command line. 63 00:04:48,750 --> 00:04:51,960 OK, that's that's interesting, but I want to use it as a module. 64 00:04:51,990 --> 00:04:52,680 OK, here you go. 65 00:04:52,920 --> 00:04:56,660 Use as a python module, so it shows you exactly what to do. 66 00:04:56,670 --> 00:04:59,100 So we import it first. 67 00:04:59,100 --> 00:05:00,020 So let's do that. 68 00:05:00,030 --> 00:05:01,740 I'm going to import. 69 00:05:03,120 --> 00:05:03,870 This library. 70 00:05:06,500 --> 00:05:13,190 All right, so now we have the translator and we can use a translator like this. 71 00:05:15,040 --> 00:05:21,010 All right, so we have the translator, so we create the translator class and we want to say to what 72 00:05:21,010 --> 00:05:21,820 language? 73 00:05:22,000 --> 00:05:24,400 All right, what languages are available? 74 00:05:24,760 --> 00:05:30,070 Let's go to the read the docs here for documentation and see if they have Japanese in here. 75 00:05:30,130 --> 00:05:30,400 All right. 76 00:05:30,400 --> 00:05:30,880 Let's see. 77 00:05:31,330 --> 00:05:33,310 Search docs, Japanese. 78 00:05:35,400 --> 00:05:36,550 All right, that doesn't help us. 79 00:05:36,570 --> 00:05:41,340 Let's look at overview and by the way, I'm showing you the thought process that I usually have when 80 00:05:41,340 --> 00:05:46,560 I look through things, because sometimes people show you what the answer is without showing you the 81 00:05:46,560 --> 00:05:46,990 process. 82 00:05:46,990 --> 00:05:48,240 So hopefully this is useful. 83 00:05:48,300 --> 00:05:50,210 OK, so I'm looking through here. 84 00:05:50,240 --> 00:05:56,600 Doesn't look like they're providing a list of of languages, although I see it here, available languages. 85 00:05:56,610 --> 00:06:02,280 It looks like it's we're using this Wikipedia page as a reference and. 86 00:06:02,280 --> 00:06:05,460 Oh yeah, these are the short terms for the languages that it's using. 87 00:06:05,720 --> 00:06:06,180 All right. 88 00:06:06,870 --> 00:06:07,470 So. 89 00:06:10,800 --> 00:06:15,930 Based on this, it looks like this is the standard that they're using, but actually while reading the 90 00:06:15,930 --> 00:06:19,180 documentation, I see that this is there's a J. 91 00:06:19,290 --> 00:06:21,000 I'm assuming this is Japanese. 92 00:06:21,000 --> 00:06:22,000 So let's do that. 93 00:06:22,170 --> 00:06:24,610 I'm going to say J. 94 00:06:25,160 --> 00:06:25,620 All right. 95 00:06:25,620 --> 00:06:27,160 And then let's see. 96 00:06:27,180 --> 00:06:30,540 Go back to the documentation here. 97 00:06:33,800 --> 00:06:34,940 Let's look at overview. 98 00:06:35,090 --> 00:06:36,110 Let's go back, actually. 99 00:06:39,610 --> 00:06:41,440 OK, so once I've created. 100 00:06:42,820 --> 00:06:49,120 The translator, I've given it the language, all we need to do is say translator, don't translate 101 00:06:49,120 --> 00:06:50,620 and give it what we want to translate. 102 00:06:50,690 --> 00:06:52,050 OK, nice and easy. 103 00:06:52,060 --> 00:06:53,260 So in here. 104 00:06:54,800 --> 00:06:58,310 Because my I read is going to give us the output that we saw. 105 00:06:58,340 --> 00:07:02,330 I'm going to say text equals my file, I read. 106 00:07:03,400 --> 00:07:12,850 And simply do the translation equals to what was it again, it was translator translates So was just 107 00:07:12,850 --> 00:07:13,420 copy that. 108 00:07:16,430 --> 00:07:22,520 So translator translate and we want to give it the text, so let's see that. 109 00:07:23,990 --> 00:07:26,300 Well, that's not I'm to do let's do text. 110 00:07:27,460 --> 00:07:32,980 All right, so if everything works, hopefully does, maybe it doesn't the first time around, let's 111 00:07:32,980 --> 00:07:33,480 give it a go. 112 00:07:34,330 --> 00:07:38,500 If I run Python three script up by. 113 00:07:41,290 --> 00:07:43,330 Well, we definitely forgot to print here. 114 00:07:43,640 --> 00:07:45,810 Looks like we're not getting any errors, which is good. 115 00:07:45,850 --> 00:07:48,400 So let's do print translation. 116 00:07:51,420 --> 00:07:52,760 Let's run this again. 117 00:07:54,820 --> 00:07:58,660 Hey, look at that, what does she know now are under it, Nagoya? 118 00:07:59,500 --> 00:08:02,920 By the way, I feel like I've been talking a lot and you haven't really talked back to me. 119 00:08:02,920 --> 00:08:04,260 But let's get to know each other. 120 00:08:04,300 --> 00:08:07,420 I actually learned Japanese before I even learned English. 121 00:08:07,450 --> 00:08:08,350 Fun fact about me. 122 00:08:08,560 --> 00:08:08,920 All right. 123 00:08:08,950 --> 00:08:10,450 Let's get back to the topic at hand. 124 00:08:10,810 --> 00:08:11,590 This is working. 125 00:08:11,600 --> 00:08:12,690 This is actually pretty cool. 126 00:08:12,710 --> 00:08:13,090 Let's. 127 00:08:14,250 --> 00:08:22,080 Finalize this by actually converting this file to a translated version, so I'm going to say I'm going 128 00:08:22,080 --> 00:08:22,590 to. 129 00:08:23,570 --> 00:08:24,050 Read. 130 00:08:26,710 --> 00:08:33,610 And once I'm done with this translation, I'm going to with open. 131 00:08:34,690 --> 00:08:35,930 I'm going to create a new file. 132 00:08:36,130 --> 00:08:37,750 Call it, let's say, test. 133 00:08:38,790 --> 00:08:41,400 Dash J for Japanese text. 134 00:08:42,790 --> 00:08:45,430 The mode is going to be to write. 135 00:08:47,120 --> 00:08:49,130 And I'm going to say as my file. 136 00:08:51,330 --> 00:08:56,460 And in here, I'm going to simply say, or let's do my file. 137 00:08:58,830 --> 00:09:05,820 Two, so it's not a name conflict and say simply, let's make this a little bit bigger just so you can 138 00:09:05,820 --> 00:09:06,000 see. 139 00:09:07,520 --> 00:09:13,970 And in here, we'll just say my file to dot, right, and write the translation. 140 00:09:15,780 --> 00:09:18,080 All right, let's see if this works, I'm going to run it again. 141 00:09:21,100 --> 00:09:23,590 OK, now if we go to our desktop. 142 00:09:25,230 --> 00:09:28,980 There's a test, Jay text and look at that. 143 00:09:30,140 --> 00:09:37,790 We have a translated file in Japanese, I think my my name should be in katakana, but hey, this this 144 00:09:37,790 --> 00:09:39,170 still works pretty cool, right? 145 00:09:39,470 --> 00:09:40,610 Hopefully you got this far. 146 00:09:40,820 --> 00:09:47,240 If you didn't, you see the power of reading and writing files and also using libraries to do different 147 00:09:47,240 --> 00:09:48,650 things to your data. 148 00:09:49,810 --> 00:09:51,010 Very, very cool. 149 00:09:51,850 --> 00:09:54,240 Let's take a break and I'll see you in the next session.