Prerequisite: Create a Virtual Machine and setup API on Google Cloud
In this article, we will discuss how to use Google’s Translation and Natural Language Processing features using Google Cloud. Before reading this article, you should have an idea of how to create an instance in a Virtual Machine and how to set up an API (refer this).
Translation API –
- Google Translator API works in the same way as in here.
- First enable the Cloud Translation API and download the .json file containing the credential informations as instructed here.
You need to download the following packages –
pip install google.cloud pip install google.cloud.translate
credetials.json file in the same folder as the
.py file with the Python code. We need to save the path of credentials.json’ (C:\Users\…) as ‘GOOGLE_APPLICATION_CREDENTIALS’ which has been done in line-5 of the following code.
The input and output .txt files should be in the same folder as the Python file, else the whole path address needs to be provided. The input file should have the source text in any language. The user does not need to specify the language as Google detects that automatically. The target language, however, needs to be provided in the form of ISO 639-1 code (for example, the above code translates the text into English (coded by ‘en’)).
Natural Langauge Processing –
Enable the Cloud Natural Language API and download the ‘credentials.json’ file as explained here. You need to download the following package –
pip install google.cloud.language
Google’s Natural Language Processing API provides several methods for analyzing text. All of them are valuable aspects of Language Analysis.
It analyses the text and understands the emotional opinion of the text. The output of Sentiment Analysis is a score within a range of -1 to 1, where -1 signifies 100% negative emotion, 1 signifies 100% positive emotion and 0 signifies neutral. It also outputs a magnitude with a range from 0 to infinity indicating the overall strength of emotion.
The text should be present in the file titled
filename_input.txt. The above code will analyze and publish the sentiment of the text line by line and will also provide the overall sentiment.
Clearly Positive -> Score: 0.8, Magnitude: 3.0 Clearly Negative -> Score: -0.6, Magnitude: 4.0 Neutral -> Score: 0.1, Magnitude: 0.0 Mixed -> Score: 0.0, Magnitude: 4.0
This is the approximate nature of emotions attached to the texts via Sentiment Analysis.
Entity Analysis provides information about entities in the text, which generally refer to named “things” such as famous individuals, landmarks, common objects, etc.
The above code will extract all entities from the above text, name its type, salience (i.e. the prominence of the entity) and its metadata (present mostly for proper nouns, along with the Wikipedia link for that entity)
Syntax Analysis breaks up the given text into tokens (by default a series of words) and provides linguistic information about those tokens.
The above code provides a list of all words and its Syntax, whether it is a noun, verb, pronoun, punctuation etc. For further information, visit Google Natural Language API documentation here.
Thus Google Cloud APIs provides high functionality services which are easy to use, portable, short and clear.
Sometimes, the above programs will result in an error “ImportError: Cannot import name ‘cygrpc'” and problem arises when we try to install it using
pip install cygrpc or sudo apt-get install cygrpc
Instead use the following command :
python -m pip install grpcio --ignore-installed
- Introduction to Natural Language Processing
- ML | Natural Language Processing using Deep Learning
- How to use Vision API from Google Cloud
- How to use Vision API from Google Cloud | Set-2
- How to use Google Colaboratory for Video Processing
- ML | Understanding Data Processing
- Processing text using NLP | Basics
- NLP | Parallel list processing with execnet
- Understanding Tensor Processing Units
- Processing of Raw Data to Tidy Data in R
- Why is Python the Best-Suited Programming Language for Machine Learning?
- Project Idea | Audio to Sign Language Translator
- How to use Google Colab
- How Does Google Use Machine Learning?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.