FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
NEW PROJECT
PAGE 1 / 1
Automatic Text Translation
Home >>> Free Online Lessons, Python 3
Whenever you try to translate a word or a sentence from one language to another, it's the Google Translate API that often brings you the desired results in the background. While you can translate anything by simply going to the Google Translate web page, you can also integrate Google's freely offered API into your Python programs.

Installing the package

The best thing about this API is that it is extremely easy to set up and use. Simply open the terminal ("cmd.exe" in Windows) and use the pip command to install the API, just as you would for any other Python library:




pip install googletrans==4.0.0-rc1

We need this version (4.0.0-rc1) to prevent some older bugs. Wait a few moments, and if everything worked correctly, the module has been successfully installed:




Info. Read more details [about PIP].

The official documentation for the Google Translate module for Python can be found [here].

What languages are available?

Well, this might be your first interaction with this module in Python. Check out the code below:

import googletrans
print(googletrans.LANGUAGES)


My program is saved in a file with the py extension, named "google.py":




I executed the program, and a list of all the languages that can be used by the module was displayed, along with their corresponding codes. For example, for Romanian it is "ro", for English it is "en", French is "fr", German is "de", and so on.

A first example of translation

Analyze the code below, as well as the result in the console:

from googletrans import Translator
translator = Translator()
result = translator.translate('Salutare, ma bucur sa te intalnesc!')
print(result.text)





First, we imported the Translator class from the googletrans module. The variable translator holds an instantiation (an object) of the Translator class. The variable result will contain an instance of the googletrans.models.Translated class, which in turn has some useful attributes for us. For example, the translated text is stored in the text attribute, accessed through result.text.

The translate method of the translator object received a single parameter, a text in the form of a string (class str), and the translation was automatically performed into the default language - English. Interestingly, the function automatically detected the source language - Romanian.

We can also display other attributes of the result, as shown in the following example:

...
print("Initial language:", result.src) # source text
print("Result language:", result.dest) # processed text





There are also the attributes origin (the initial text) and pronunciation (the pronunciation of the resulting text), which can be useful in our programs.

Translation result language

We can set the result language using the dest parameter of the translate method:

from googletrans import Translator
translator = Translator()
result = translator.translate('Python is very cool!', dest='fr')
print(result.text)





This time, we chose the French language using the code "fr". If necessary, the source text language can also be specified using the additional src parameter:

result = translator.translate('Python este foarte fain!', src='ro', dest='fr')

Translating list elements

Suppose we have a list containing several string elements (string, class str) and we want to translate them using the Google Translate module, with the results being saved in another list.

Nothing easier, analyze the program below:

from googletrans import Translator
t = Translator()
c = ['rosu', 'verde', 'albastru', 'alb', 'negru', 'galben']
c_fr = []
for i in c:
    c_fr.append(t.translate(i, src='ro', dest='fr').text)
print(c)
print(c_fr)





The translate method accepts text only in string format, so we had to iterate through all the elements of our list. We translated the text contained in each element of the list, adding it at each step to the new list called c_fr. We wrote everything on a single line, passing the result of the translate method as a parameter for the append method. For better code readability, we could also write it like this, using an auxiliary variable:

...
for i in c:
    translated_i = t.translate(i, src='ro', dest='fr').text
    c_fr.append(translated_i)
...


Proposed projects

Ideas and methods are numerous now that we've discovered how it works. We recommend reading the documentation for the Google Translate module, which you can find [here].

1. Create a program that allows users to enter a text in one language and automatically translate it into another using Googletrans.

Suggestions. You can ask the user for the code of the desired language and make sure to check in the code if it exists or not. Another idea would be to display a menu with the most important languages from which the user can choose one. Be creative and make an elegant and useful user interface!

2. Build a program that functions as a multilingual dictionary. Users can enter a word or phrase, and the program will return its translation in different languages.

3. Create a program that can automatically detect the language of a text entered by the user using Googletrans.

4 *. Text Files. Build a program that allows users to upload a subtitle file and automatically translate it into another language using Googletrans. This way, you can create translated subtitles for different videos, etc.

5 **. Text Game. Create an interactive game where the user guesses words in a foreign language. The program can generate random words from a predefined list and automatically translate them using Googletrans. The user must guess the correct translation of the word and receive feedback based on their response.

This section is finished.
 home   list  CONTENTS   perm_identity   arrow_upward