1 00:00:00,180 --> 00:00:02,310 -: All right, it's time for an exercise. 2 00:00:02,310 --> 00:00:04,350 And I think this is gonna be fun. 3 00:00:04,350 --> 00:00:07,050 I wanna use what we've learned up until now 4 00:00:07,050 --> 00:00:09,540 to build a translator. 5 00:00:09,540 --> 00:00:12,870 Let's say somebody gives you a massive text file. 6 00:00:12,870 --> 00:00:16,140 I mean, in our case, this is gonna be the text file 7 00:00:16,140 --> 00:00:18,390 but you can grab any text file that you want. 8 00:00:18,390 --> 00:00:20,820 It doesn't have to be a text file, it can be anything. 9 00:00:20,820 --> 00:00:22,050 It could be a py file 10 00:00:22,050 --> 00:00:25,350 it could be just a Word document 11 00:00:25,350 --> 00:00:28,650 and type in a bunch of sentences in here, 12 00:00:28,650 --> 00:00:32,340 whatever you want, and just save it. 13 00:00:32,340 --> 00:00:34,950 In my case, I just have this. 14 00:00:34,950 --> 00:00:36,337 Now somebody just told you, 15 00:00:36,337 --> 00:00:39,240 "Hey, I want you to translate this into, 16 00:00:39,240 --> 00:00:40,593 let's say Japanese. 17 00:00:41,580 --> 00:00:43,050 Based on what you just learned 18 00:00:43,050 --> 00:00:47,820 about modules and files in Python, 19 00:00:47,820 --> 00:00:49,770 can you build a tool that allows us 20 00:00:49,770 --> 00:00:52,290 to run a command through that file, 21 00:00:52,290 --> 00:00:55,200 read it and then translate it into Japanese?" 22 00:00:55,200 --> 00:00:57,330 Now, if you really wanna get challenged 23 00:00:57,330 --> 00:01:00,150 you can pause the video and try it out. 24 00:01:00,150 --> 00:01:04,379 Or if you want a hint what module or library to use, 25 00:01:04,379 --> 00:01:06,960 well then keep watching 26 00:01:06,960 --> 00:01:10,470 because I'm gonna show you the recommended package to use. 27 00:01:10,470 --> 00:01:13,500 Okay? So what I actually did 28 00:01:13,500 --> 00:01:17,220 was I Googled Python offline translate 29 00:01:17,220 --> 00:01:20,430 using the PyPI index. 30 00:01:20,430 --> 00:01:23,250 Now, I wanted an offline translation service 31 00:01:23,250 --> 00:01:24,600 because if you look, 32 00:01:24,600 --> 00:01:27,300 there's something like Google translation that you can do 33 00:01:27,300 --> 00:01:30,750 but what that does is actually make a call 34 00:01:30,750 --> 00:01:35,130 to the Google servers to do Google Translate for you. 35 00:01:35,130 --> 00:01:36,900 And that gets pretty complicated 36 00:01:36,900 --> 00:01:39,510 when we talk about APIs and API keys. 37 00:01:39,510 --> 00:01:40,860 Something that we're gonna worry about 38 00:01:40,860 --> 00:01:42,090 later on in the course. 39 00:01:42,090 --> 00:01:43,470 I wanted something offline 40 00:01:43,470 --> 00:01:47,130 so I can download the translation service offline 41 00:01:47,130 --> 00:01:50,010 and just be able to use it right here. 42 00:01:50,010 --> 00:01:52,860 So the one that I found was, as I was Googling 43 00:01:52,860 --> 00:01:56,313 I think it was called this one. 44 00:01:57,810 --> 00:02:01,200 Yeah, so this is just a translation service. 45 00:02:01,200 --> 00:02:04,740 It looks like it has some stars and forks on GitHub. 46 00:02:04,740 --> 00:02:05,730 So I always like that. 47 00:02:05,730 --> 00:02:08,610 I always like checking out their repository. 48 00:02:08,610 --> 00:02:11,670 So translate Python, everything looks good. 49 00:02:11,670 --> 00:02:13,893 You can read about the documentation here. 50 00:02:15,450 --> 00:02:19,440 Hasn't been updated in two years, really, but you know what? 51 00:02:19,440 --> 00:02:22,110 It's not that bad. It'll do the job. 52 00:02:22,110 --> 00:02:25,170 So using this package, you can try 53 00:02:25,170 --> 00:02:27,210 and read through the documentation 54 00:02:27,210 --> 00:02:28,470 and see if you can figure out 55 00:02:28,470 --> 00:02:30,240 how to solve this problem 56 00:02:30,240 --> 00:02:33,690 of translating that file into Japanese. 57 00:02:33,690 --> 00:02:35,580 You can pause the video here, give it a go. 58 00:02:35,580 --> 00:02:37,580 Otherwise, I'm gonna provide the answer. 59 00:02:39,240 --> 00:02:41,040 Now, the first thing I'm gonna do is 60 00:02:41,040 --> 00:02:42,270 before I even install this 61 00:02:42,270 --> 00:02:44,790 I wanna make sure I can read this file. 62 00:02:44,790 --> 00:02:49,680 So once again, I'm going to say try 63 00:02:49,680 --> 00:02:53,453 and I'll have a file with open (), 64 00:02:55,980 --> 00:02:57,480 We'll see where the file is. 65 00:02:57,480 --> 00:02:58,650 Let's say ls, 66 00:02:58,650 --> 00:03:01,740 we the test file right in here. 67 00:03:01,740 --> 00:03:04,147 So we can just do ./ 68 00:03:05,730 --> 00:03:08,527 and say text.txt. 69 00:03:09,630 --> 00:03:11,523 And then I'll say, as my_file. 70 00:03:16,650 --> 00:03:20,460 We'll let the mode as read for now, 71 00:03:20,460 --> 00:03:22,950 because we just wanna read the file. 72 00:03:22,950 --> 00:03:25,320 But at least this way we're being explicit 73 00:03:25,320 --> 00:03:28,530 and we're saying, Hey, this is exactly what we wanna do. 74 00:03:28,530 --> 00:03:31,350 And then finally, I'll just for now 75 00:03:31,350 --> 00:03:33,810 print (my_file) 76 00:03:33,810 --> 00:03:36,400 just to know that it's working 77 00:03:37,380 --> 00:03:39,930 and we'll add some exceptions in here. 78 00:03:39,930 --> 00:03:42,360 It's except not exception. 79 00:03:42,360 --> 00:03:43,980 And we can just, let's say do 80 00:03:43,980 --> 00:03:44,830 FileNotFoundError 81 00:03:49,680 --> 00:03:54,090 say as e, and we'll just for fun, say here, 82 00:03:54,090 --> 00:03:58,500 print ('check your file path silly') 83 00:04:01,950 --> 00:04:03,090 and leave it at that. 84 00:04:03,090 --> 00:04:06,610 If I run this code, let's say python3 85 00:04:07,560 --> 00:04:10,380 script.py 86 00:04:10,380 --> 00:04:12,450 All right, I have, my name is Andre. 87 00:04:12,450 --> 00:04:16,320 Awesome, let's install the package. 88 00:04:16,320 --> 00:04:19,950 So over here I can see that I have to install translate. 89 00:04:19,950 --> 00:04:21,870 That's what the package is called. 90 00:04:21,870 --> 00:04:24,720 Now I'll use pip3 in my case 91 00:04:24,720 --> 00:04:27,460 to make sure that I'm using the Python version three 92 00:04:28,590 --> 00:04:30,963 and say install translate. 93 00:04:31,890 --> 00:04:35,020 Now, I've actually done this before just because, well 94 00:04:36,060 --> 00:04:38,860 I tested this out beforehand, so it's already installed. 95 00:04:40,890 --> 00:04:42,390 So let's clear that out 96 00:04:42,390 --> 00:04:44,790 and read about how we can use this package. 97 00:04:44,790 --> 00:04:46,770 So you can see here the usage. 98 00:04:46,770 --> 00:04:48,720 So I can use it from the command line. 99 00:04:48,720 --> 00:04:50,250 Okay, that's, that's interesting 100 00:04:50,250 --> 00:04:51,960 but I want to use it as a module. 101 00:04:51,960 --> 00:04:52,920 Okay, here you go. 102 00:04:52,920 --> 00:04:56,610 Use as a python module so it shows you exactly what to do. 103 00:04:56,610 --> 00:04:59,070 So we import it first. 104 00:04:59,070 --> 00:05:00,000 So let's do that. 105 00:05:00,000 --> 00:05:04,053 I'm going to import this library. 106 00:05:06,540 --> 00:05:09,270 All right, so now we have the translator 107 00:05:09,270 --> 00:05:13,353 and we can use the translator like this. 108 00:05:15,090 --> 00:05:16,650 All right, so we have the translator. 109 00:05:16,650 --> 00:05:18,990 So we create the translator class. 110 00:05:18,990 --> 00:05:21,990 And we wanna say to what language. 111 00:05:21,990 --> 00:05:24,750 All right, what languages are available? 112 00:05:24,750 --> 00:05:26,970 Let's go to the, read the docs here 113 00:05:26,970 --> 00:05:30,150 for documentation and see if they have Japanese in here. 114 00:05:30,150 --> 00:05:31,320 All right, let's see. 115 00:05:31,320 --> 00:05:33,483 Search docs, Japanese. 116 00:05:35,430 --> 00:05:36,540 All right, that doesn't help us. 117 00:05:36,540 --> 00:05:37,830 Let's look at overview. 118 00:05:37,830 --> 00:05:39,000 And by the way, I'm showing you 119 00:05:39,000 --> 00:05:41,130 the thought process that I usually have 120 00:05:41,130 --> 00:05:43,020 when I look through things 121 00:05:43,020 --> 00:05:45,690 because sometimes people show you what the answer is 122 00:05:45,690 --> 00:05:46,980 without showing you the process. 123 00:05:46,980 --> 00:05:48,300 So hopefully this is useful. 124 00:05:48,300 --> 00:05:50,160 Okay, so I'm looking through here. 125 00:05:50,160 --> 00:05:52,380 It doesn't look like they're providing a list 126 00:05:52,380 --> 00:05:55,470 of languages, although, oh, I see it here. 127 00:05:55,470 --> 00:05:56,610 Available languages. 128 00:05:56,610 --> 00:05:57,870 It looks like it's reusing 129 00:05:57,870 --> 00:06:01,740 this Wikipedia page as a reference. 130 00:06:01,740 --> 00:06:03,660 And oh yeah, these are the short terms 131 00:06:03,660 --> 00:06:05,760 for the languages that it's using. 132 00:06:05,760 --> 00:06:08,820 All right, so based on this, 133 00:06:11,610 --> 00:06:14,250 it looks like this is the standard that they're using. 134 00:06:14,250 --> 00:06:16,800 But actually, while reading the documentation, 135 00:06:16,800 --> 00:06:19,260 I see that this is, there's a 'ja'. 136 00:06:19,260 --> 00:06:21,000 I'm assuming this is Japanese. 137 00:06:21,000 --> 00:06:22,230 So let's do that. 138 00:06:22,230 --> 00:06:25,200 I'm going to say 'ja'. 139 00:06:25,200 --> 00:06:26,033 All right. 140 00:06:26,033 --> 00:06:27,180 And then let's see. 141 00:06:27,180 --> 00:06:30,723 Go back to the documentation here. 142 00:06:33,840 --> 00:06:35,070 Let's look at overview. 143 00:06:35,070 --> 00:06:36,243 Let's go back actually. 144 00:06:39,660 --> 00:06:43,890 Okay, so once I've created the translator 145 00:06:43,890 --> 00:06:45,630 I've given it the language. 146 00:06:45,630 --> 00:06:49,080 All we need to do is say translator.translate 147 00:06:49,080 --> 00:06:50,640 and give it what we want to translate. 148 00:06:50,640 --> 00:06:52,020 Okay? Nice and easy. 149 00:06:52,020 --> 00:06:56,580 So in here, because my_file that I read 150 00:06:56,580 --> 00:06:58,320 is going to give us the output that we saw. 151 00:06:58,320 --> 00:07:02,547 I'm going to say text = my_file.read(). 152 00:07:03,450 --> 00:07:08,450 and simply do the translation equals to, 153 00:07:09,720 --> 00:07:10,560 What was it again? 154 00:07:10,560 --> 00:07:12,360 It was translator.translate. 155 00:07:12,360 --> 00:07:13,563 So we'll just copy that. 156 00:07:16,440 --> 00:07:20,490 So translator translate and we wanna give it the text. 157 00:07:20,490 --> 00:07:22,623 So let's see that. 158 00:07:24,030 --> 00:07:25,350 Whoop, that's not what I have to do. 159 00:07:25,350 --> 00:07:28,620 Let's do text, right? 160 00:07:28,620 --> 00:07:31,380 So if everything works, hopefully it does 161 00:07:31,380 --> 00:07:34,350 maybe it doesn't the first time around, let's give it a go. 162 00:07:34,350 --> 00:07:38,643 If I run python3 script.py, 163 00:07:41,310 --> 00:07:43,620 well we definitely forgot to print here. 164 00:07:43,620 --> 00:07:45,840 Looks like we're not getting any errors, which is good. 165 00:07:45,840 --> 00:07:47,390 So let's do print(translation). 166 00:07:51,420 --> 00:07:52,923 Let's run this again. 167 00:07:54,870 --> 00:07:56,283 Hey, look at that. 168 00:07:56,283 --> 00:07:59,520 (speaks Japanese) 169 00:07:59,520 --> 00:08:01,380 By the way, I feel like I've been talking a lot 170 00:08:01,380 --> 00:08:02,880 and you haven't really talked back to me 171 00:08:02,880 --> 00:08:04,320 but let's get to know each other. 172 00:08:04,320 --> 00:08:07,440 I actually learned Japanese before I even learned English. 173 00:08:07,440 --> 00:08:08,580 Fun fact about me. 174 00:08:08,580 --> 00:08:10,770 All right, let's get back to the topic at hand. 175 00:08:10,770 --> 00:08:11,603 This is working. 176 00:08:11,603 --> 00:08:12,660 This is actually pretty cool. 177 00:08:12,660 --> 00:08:15,540 Let's finalize this 178 00:08:15,540 --> 00:08:19,890 by actually converting this file to a translated version. 179 00:08:19,890 --> 00:08:22,630 So I'm going to say I'm going to 180 00:08:23,580 --> 00:08:25,780 read this file 181 00:08:26,790 --> 00:08:30,300 and once I'm done with this translation, 182 00:08:30,300 --> 00:08:33,837 I'm going to with open(), 183 00:08:34,740 --> 00:08:36,750 I'm going to create a new file, call it 184 00:08:36,750 --> 00:08:41,582 let's say test-ja, for Japanese, .txt. 185 00:08:42,780 --> 00:08:45,460 The mode is going to be to write 186 00:08:47,130 --> 00:08:49,180 and I'm going to say as my_file. 187 00:08:51,360 --> 00:08:54,900 And in here I'm going to simply say, 188 00:08:54,900 --> 00:08:55,960 or let's do my_file2 189 00:08:59,370 --> 00:09:01,830 so it's not a name conflict 190 00:09:01,830 --> 00:09:03,380 and say simply... 191 00:09:04,230 --> 00:09:06,623 Let's make this a little bit bigger just so you can see. 192 00:09:07,560 --> 00:09:12,510 And in here we'll just say my_file2.write() 193 00:09:12,510 --> 00:09:14,073 and write the translation. 194 00:09:15,840 --> 00:09:16,950 All right, Let's see if this works. 195 00:09:16,950 --> 00:09:18,273 I'm going to run it again. 196 00:09:21,150 --> 00:09:23,650 Okay, Now if we go to our desktop 197 00:09:25,260 --> 00:09:27,073 there's a test.ja txt 198 00:09:28,050 --> 00:09:29,133 and look at that. 199 00:09:30,150 --> 00:09:33,840 We have a translated file in Japanese. 200 00:09:33,840 --> 00:09:36,390 I think my name should be in Katakana 201 00:09:36,390 --> 00:09:38,400 but hey, this still works. 202 00:09:38,400 --> 00:09:39,450 Pretty cool, right? 203 00:09:39,450 --> 00:09:40,830 Hopefully you got this far. 204 00:09:40,830 --> 00:09:43,650 If you didn't, you see the power of reading 205 00:09:43,650 --> 00:09:46,410 and writing files and also using libraries to 206 00:09:46,410 --> 00:09:48,753 do different things to your data. 207 00:09:49,800 --> 00:09:51,840 Very, very cool. 208 00:09:51,840 --> 00:09:55,140 Let's take a break and I'll see you in the next session. 209 00:09:55,140 --> 00:09:55,973 Bye-bye.