All right, it's time for an exercise, and I think this is going to be fun.
I want to use what we've learned up until now to build a translator.
Let's say somebody gives you a massive text file.
I mean, in our case, this is going to be the text file, but you can grab any text file that you want.
It doesn't have to be a text file.
It can be anything.
It could be a PDF file.
It could be just a word document and type in a bunch of sentences in here, whatever you want.
And just save it, in my case, I just have this now somebody just told you, hey, I want you to translate
this into, let's say, Japanese.
Based on what you just learned about modules and files in Python, can you build a tool that allows
us to run a command through that file, read it and then translate it into Japanese?
Now, if you really want to get challenged, you can pause the video and try it out, or if you want
a hint, what module or library to use.
Well, then keep watching because I'm going to show you the recommended package to use.
OK, so what I actually did was I Googled Python offline, translate using the PI by index.
Now, I wanted an offline translation service because if you look there's something like Google translation
that you can do.
But what that does is actually make a call to the Google servers to do Google Translate for you.
And that gets pretty complicated when we talk about APIs and API keys, something that we're going to
worry about later on in the course.
I wanted something offline so I can download the translation service offline and just be able to use
it right here.
So the one that I found was, as I was Googling, I think it was called.
Yeah, so this is just the translation service, it looks like it has some stars and forks on GitHub,
so I always like that.
I always like checking out their repository.
So translate Python.
Everything looks good.
You can read about the documentation here.
Hasn't been updated in two years, really, but you know what, it's it's not that bad.
Do the job.
So using this package, you can try and read through the documentation and see if you can figure out
how to solve this problem of translating that file into Japanese.
You can porzel video here, give it a go.
Otherwise, I'm going to provide the answer.
Now, the first thing I'm going to do is before I even install this, I want to make sure I can read
this file.
So once again, I'm going to say.
Try and I'll have a file with.
Open, we'll see where the file is, let's say, else we have the test file right in here so we can
just do.
And say, test, dot text.
And then I'll say as my.
Well, that the mode as.
Read for now, because we just want to read the file, but at least this way we're being explicit and
we're saying, hey, this is exactly what we want to do.
And then finally, I'll just for now print.
My file just, you know, that it's working and we'll add some exceptions in here, it's except not
exception and we can just, let's say, do file not found.
Say, as he and we'll just for fun, say here, print, check your.
File path cyle.
And leave it at that, if I run this code, let's say Python three.
Script, Dotti.
All right, I have my name is Andre Awesome.
Let's install the package, so over here I can see that I have to install translate, that's what the
package is called.
Now, I'll use pipe three in my case to make sure that I'm using the Python version three.
And say and still translate now I've actually done this before just because well.
I tested this out beforehand, so it's already installed.
So let's clear that out and read about how we can use this package so you can see here the usage so
I can use it from the command line.
OK, that's that's interesting, but I want to use it as a module.
OK, here you go.
Use as a python module, so it shows you exactly what to do.
So we import it first.
So let's do that.
I'm going to import.
This library.
All right, so now we have the translator and we can use a translator like this.
All right, so we have the translator, so we create the translator class and we want to say to what
language?
All right, what languages are available?
Let's go to the read the docs here for documentation and see if they have Japanese in here.
All right.
Let's see.
Search docs, Japanese.
All right, that doesn't help us.
Let's look at overview and by the way, I'm showing you the thought process that I usually have when
I look through things, because sometimes people show you what the answer is without showing you the
process.
So hopefully this is useful.
OK, so I'm looking through here.
Doesn't look like they're providing a list of of languages, although I see it here, available languages.
It looks like it's we're using this Wikipedia page as a reference and.
Oh yeah, these are the short terms for the languages that it's using.
All right.
So.
Based on this, it looks like this is the standard that they're using, but actually while reading the
documentation, I see that this is there's a J.
I'm assuming this is Japanese.
So let's do that.
I'm going to say J.
All right.
And then let's see.
Go back to the documentation here.
Let's look at overview.
Let's go back, actually.
OK, so once I've created.
The translator, I've given it the language, all we need to do is say translator, don't translate
and give it what we want to translate.
OK, nice and easy.
So in here.
Because my I read is going to give us the output that we saw.
I'm going to say text equals my file, I read.
And simply do the translation equals to what was it again, it was translator translates So was just
copy that.
So translator translate and we want to give it the text, so let's see that.
Well, that's not I'm to do let's do text.
All right, so if everything works, hopefully does, maybe it doesn't the first time around, let's
give it a go.
If I run Python three script up by.
Well, we definitely forgot to print here.
Looks like we're not getting any errors, which is good.
So let's do print translation.
Let's run this again.
Hey, look at that, what does she know now are under it, Nagoya?
By the way, I feel like I've been talking a lot and you haven't really talked back to me.
But let's get to know each other.
I actually learned Japanese before I even learned English.
Fun fact about me.
All right.
Let's get back to the topic at hand.
This is working.
This is actually pretty cool.
Let's.
Finalize this by actually converting this file to a translated version, so I'm going to say I'm going
to.
Read.
And once I'm done with this translation, I'm going to with open.
I'm going to create a new file.
Call it, let's say, test.
Dash J for Japanese text.
The mode is going to be to write.
And I'm going to say as my file.
And in here, I'm going to simply say, or let's do my file.
Two, so it's not a name conflict and say simply, let's make this a little bit bigger just so you can
see.
And in here, we'll just say my file to dot, right, and write the translation.
All right, let's see if this works, I'm going to run it again.
OK, now if we go to our desktop.
There's a test, Jay text and look at that.
We have a translated file in Japanese, I think my my name should be in katakana, but hey, this this
still works pretty cool, right?
Hopefully you got this far.
If you didn't, you see the power of reading and writing files and also using libraries to do different
things to your data.
Very, very cool.
Let's take a break and I'll see you in the next session.