-: All right, it's time for an exercise.
And I think this is gonna be fun.
I wanna 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 gonna 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 py 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 wanna 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 gonna show you the recommended package to use.
Okay? So what I actually did
was I Googled Python offline translate
using the PyPI 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 gonna 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 this one.
Yeah, so this is just a 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 not that bad. It'll 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 pause the video here, give it a go.
Otherwise, I'm gonna provide the answer.
Now, the first thing I'm gonna do is
before I even install this
I wanna 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 ls,
we the test file right in here.
So we can just do ./
and say text.txt.
And then I'll say, as my_file.
We'll let the mode as read for now,
because we just wanna read the file.
But at least this way we're being explicit
and we're saying, Hey, this is exactly what we wanna do.
And then finally, I'll just for now
print (my_file)
just to 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
FileNotFoundError
say as e, and we'll just for fun, say here,
print ('check your file path silly')
and leave it at that.
If I run this code, let's say python3
script.py
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 pip3 in my case
to make sure that I'm using the Python version three
and say install 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.
Okay, that's, that's interesting
but I want to use it as a module.
Okay, 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 the translator like this.
All right, so we have the translator.
So we create the translator class.
And we wanna 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.
Okay, so I'm looking through here.
It doesn't look like they're providing a list
of languages, although, oh, I see it here.
Available languages.
It looks like it's reusing
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 'ja'.
I'm assuming this is Japanese.
So let's do that.
I'm going to say 'ja'.
All right.
And then let's see.
Go back to the documentation here.
Let's look at overview.
Let's go back actually.
Okay, so once I've created the translator
I've given it the language.
All we need to do is say translator.translate
and give it what we want to translate.
Okay? Nice and easy.
So in here, because my_file that I read
is going to give us the output that we saw.
I'm going to say text = my_file.read().
and simply do the translation equals to,
What was it again?
It was translator.translate.
So we'll just copy that.
So translator translate and we wanna give it the text.
So let's see that.
Whoop, that's not what I have to do.
Let's do text, right?
So if everything works, hopefully it does
maybe it doesn't the first time around, let's give it a go.
If I run python3 script.py,
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.
(speaks Japanese)
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 this file
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-ja, for Japanese, .txt.
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_file2
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_file2.write()
and write the translation.
All right, Let's see if this works.
I'm going to run it again.
Okay, Now if we go to our desktop
there's a test.ja txt
and look at that.
We have a translated file in Japanese.
I think my name should be in Katakana
but hey, 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.
Bye-bye.