All of us are aware of Google Translate. Today, we will provide examples of how we can use the googletrans which is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate .
The first thing that we need to do is to install the googletrans library. I suggest to use the conda install command.
conda install -c conda-forge googletrans
How to get the supported languages
Google Translate supports more than 100 languages. Below, we give an example of how you can get the supported language and their corresponding code:
import pandas as pd import googletrans from googletrans import Translator translator = Translator() pd.set_option('max_colwidth', 300) # how to get the supported language and their corresponing code lang_df = pd.DataFrame.from_dict(googletrans.LANGUAGES, orient='index', columns=['Language']) lang_df
Language af afrikaans sq albanian am amharic ar arabic hy armenian .. ... yi yiddish yo yoruba zu zulu fil Filipino he Hebrew [106 rows x 1 columns]
Let’s say that we want to get the code of specific languages, like ‘English‘, ‘Italian‘, ‘Spanish‘, ‘French‘, and ‘Greek‘.
# find the code for english, french, italian, spanish and greek lang_df[lang_df.Language.isin(['english', 'french', 'italian', 'spanish', 'greek'])]
Language en english fr french el greek it italian es spanish
How to detect the language
Google Translate can detect the language. Let’s give an example:
my_example = translator.detect("Cerco un centro di gravità permanente") print(my_example)
Detected(lang=it, confidence=1.0)
As we can see, Google detected that the “Cerco un centro di gravità permanente” is Italian with a confidence of 100%
How to translate from source language to destination language
The translate function translates from source language to destination language taking as default “English” as destination language and as a source language it tries to detect the language.
translator.translate(text, dest='en', src='auto')
In our example, we will define the source and the destination language. Let’s say that we want to translate the above “Italian” sentence to “English”.
my_translation = translator.translate("Cerco un centro di gravità permanente", src='it', dest='en') print(my_translation.text)
I am seeking a permanent center of gravity
The object returned by the translate()
method has the following attributes:
src
: The source language, which is set to Italin (it)dest
: Destination language, which is set to English (en)origin
: Original text, that is ‘Cerco un centro di gravità permanente‘ in our exampletext
: Translated text, which was ‘I am seeking a permanent center of gravity‘ in our casepronunciation
: Pronunciation of the translated text
How to translate multiple documents at once
Let’s assume that you have the English documents in a pandas data frame, and you want to get the translation in Italian. Let’s provide a data frame with English documents.
my_example = pd.DataFrame({'EnglishText':['Police in France say they have seized 140,000 face masks that were destined for sale on the black market.', 'Officers say they discovered the haul when they spotted a businessman unloading the masks from a lorry into a house in St Denis, north of Paris.', 'Prime Minister Boris Johnson could possibly lead the daily news conference on Monday but, if not then, it will be pretty soon afterwards.']}) my_example
EnglishText 0 Police in France say they have seized 140,000 face masks that were destined for sale on the black market. 1 Officers say they discovered the haul when they spotted a businessman unloading the masks from a lorry into a house in St Denis, north of Paris. 2 Prime Minister Boris Johnson could possibly lead the daily news conference on Monday but, if not then, it will be pretty soon afterwards.
my_example['ItalianText'] = my_example['EnglishText'].apply(lambda x: translator.translate(x, src='en', dest='it').text ) my_example
EnglishText | ItalianText | |
---|---|---|
0 | Police in France say they have seized 140,000 face masks that were destined for sale on the black market. | La polizia in Francia dicono di aver sequestrato 140.000 maschere per il viso che erano destinati per la vendita sul mercato nero. |
1 | Officers say they discovered the haul when they spotted a businessman unloading the masks from a lorry into a house in St Denis, north of Paris. | Gli ufficiali dicono che hanno scoperto il raggio quando hanno avvistato un uomo d’affari scarico le maschere da un camion in una casa a St Denis, a nord di Parigi. |
2 | Prime Minister Boris Johnson could possibly lead the daily news conference on Monday but, if not then, it will be pretty soon afterwards. | Il primo ministro Boris Johnson potrebbe condurre la conferenza stampa quotidiana il Lunedi, ma, se non allora, sarà abbastanza presto dopo. |
Happy Translations!