googletrans module
Installation
This module can be installed by using,
pip install googletrans
googletrans can be used after installation and importation of the module
import googletrans or
from googletrans import Translator
Built-in functions inside googletrans module
To look for built-in functions inside the googletrans module, use the command as
dir(googletrans)
Languages supported by googletrans module
To view the list of languages supported by the googletrans module, use the command as,
print(googletrans.LANGUAGES)
Detecting language of the text
The native language of the text can be detected using detect(text) in the googletrans module.
- Follow the code snippet on detecting the native language of the text,
# import statementimport googletrans # object instantiation for Translator trans = googletrans.Translator() # detect() , detects the language of text passed inside as argumentsdetecting_lang = trans.detect("omnia vincit amor") print(detecting_lang)# output: Detected(lang=la, confidence=1.0)
In the above code snippet, the detect( ) detects the native language of "Omnia Vincit Amor" as "la" which is the code of the language "Latin".
Translating from one language to another
To translate from one language to another, use syntax as
translate(text, dest = "lang")
where dest is the destination language to translate.
Look into the following code snippet for clear understanding,
# after detecting the native language of the text ie., refer above code snippet#translation from Latin to English can be done bytranslated = trans.translate("omnia vincit amor" , dest ="en")print(translated.text)#output : Love conquers all
In the above code snippet, the text "Omnia Vincit Amor" was translated from its native language to "English".
Comments
Post a Comment