Skip to main content

Translating text from one language to another using python

The definition of a translator is a person who helps people who speak different languages to communicate or who take something (such as a speech or a book) in one language and who puts it into a different language for people to understand. This functionality can be achieved using the googletrans module in python.

googletrans module

    The googletrans module is one of the fast and flexible libraries in python that implements Google Translate API, to detect the language of text automatically and to convert text from one language to another. By default, all the languages are translated to English.

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

Popular posts from this blog

Interacting With System Applications Through OS Module In Python

 As we all know, Operating System (OS) is a program that acts as an interface between computer hardware and user. Python has a standard utility module called "os module" to achieve a process of communicating directly to the operating system of your device. Installation The os module is one of the standard libraries in Python 3, so it doesn't need to be installed manually or externally. It comes along with the installation of Python. So it is mandatory to import this os module before using it in your program as it is not a built-in function. Importing module The os module can be used in a program by importing it initially. It is mandatory to import the os module as it is not a built-in function to use it without any importation. To import the os module, use the command as import os Built-in functions inside os module There are a bunch of built-in functions present inside the os module where one can invoke each function after the module gets imported inside a program.  To k...

Easy Way To Build An Audiobook Using Python

  An audiobook is nothing but a book that was recorded in an audio format. It can also be stated as a book that is being read aloud. Audiobook helps in improvising one's vocabulary, comprehension and pronunciation of words. Hope some of you guys are bookaholic but lazy to read it on your own. So it's time to create your own audiobook by using few codes of Python. This will definitely make you enjoy audiobooks without any subscription fee, that was imposed on platforms like Audible, Scribd, etc. Prerequisite Python has a huge number of modules that contains reusable code which performs desired functions when invoked. It must be installed into your system before using its functionalities. So here we going to grab just 2 modules from a bunch of python modules prevailing around. Install PyPDF2 module, using                     pip install PyPDF2 Install pyttsx3 module, using             ...

A Simple Chatbot Using Python

Chatbot or chatterbot is a software program that simulates conversations made by humans through voice or text chats. It is more commonly used in messaging applications.  As we all know Alexa, Zo chatbot, Faketalk, Watson Assistant are some of the chatbots built on Artificial Intelligence and Natural Language Processing. Here am gonna show you how to create a chatbot, to open a system application on your device using python. Steps to follow First import the modules needed to make a chatbot. Then make the chatbot get input from the user ie., to which application he/she needs to open. Next, make the chatbot react to the user input by opening an application specified by the user. Make the chatbot work again and again until the user specifies to "quit". Modules needed Modules needed to build a chatbot are: pyttsx3 module os module pyttsx3 module Modules are nothing but a library that consists of reusable code that performs desired function when it gets invoked. It can be invoked i...