Translate Using Python

Have you ever tried to translate any text using google, it’s straightforward and helpful to do so when I used to study the German Language google helped me alot in my homework. Do you know how Google does that? Google developers have created an API that does it all. Now, what if you want to translate using Python? Python being a straightforward and general-purpose programming language provides packages for almost every task.

To translate using Python, we have a package called googletrans, which helps us to access the Google Translation API to translate using Python. This article will take you through how we can use this package to bring using Python. I will translate the Hindi language to English.

Translate Using Python

For the translation of any language to your desired language, you are required to install a package known as googletrans, which can be easily installed using the pip command- pip install googletrans. I hope you have easily installed this package, now let’s start with our task to translate Hindi to English using Python.

I will use a dataset, which is a set of some names of vegetables in Hindi. You can easily download the dataset from here. Now let’s start with importing the necessary packages we need for this task:

import pandas as pd from googletrans import Translator

Now I will read the CSV using the pandas library to look at the data to know what we are going to work with:

data = pd.read_csv("hindi.csv") print(data)
Vegetable Names
0 गाजर
1 शिमला मिर्च
2 भिन्डी
3 मक्का
4 लाल मिर्च
5 खीरा
6 कढ़ी पत्ता
7 बैगन
8 लहसुन
9 अदरक

You can see the dataset contains ten values. They are names of some common vegetables, now let’s move forward to translate these words into English. I will store all the elements into a dataframe then translate every item into English using Python. Let’s see how we can do this:

translator = Translator() translations = {} for column in data.columns: unique = data[column].unique() for element in unique: translations[element] = translator.translate(element).text for i in translations.items(): print(i)
('गाजर ', 'carrot')
('शिमला मिर्च', 'capsicum')
('भिन्डी ', 'Lady finger')
('मक्का ', 'Corn')
('लाल मिर्च', 'Red chilly')
('खीरा ', 'Cucumber')
('कढ़ी पत्ता', 'Curry leaves')
('बैगन ', 'Brinjal')
('लहसुन ', 'Garlic')
('अदरक ', 'Ginger')

As you can see, all the words are now stored with their translation into English in a dictionary. Now let’s add all these translated words into a pandas dataframe:

data.replace(translations, inplace=True) print(data)
Vegetable Names
0          carrot
1        capsicum
2     Lady finger
3            Corn
4      Red chilly
5        Cucumber
6    Curry leaves
7         Brinjal
8          Garlic
9          Ginger

Also, Read – The Role of Analytics in Organization.

So we are successful in translating Hindi to English using Python. I hope you liked this article on translate using Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.

Also, Read – Candlestick Chart with Python.

Follow Us:

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply