LOADING

Type to search

Cloud Google Natural Language Python Uncategorized

Google Cloud Natural Language API: Getting Started

Share

Natural Language uses machine learning to disclose the structure and meaning of text. You can extract information about people, places, and proceedings, and better understand social media sentiment and customer dialogues. Natural Language empowers you to analyze text and also integrate it with your document storage on Google Cloud Storage. The post will help you start using the Google Cloud Natural Language API and will take you from account creation to using the API with Python.

The powerful pre-trained models of the Google Cloud Natural Language API lets developers work with natural language-understanding features, such as sentiment analysis, entity analysis, entity sentiment analysis, content classification, and syntax analysis.

Initial Google Cloud and Natural Language API Setup

We are going to run out setup on a Windows 64 bit machine. The first step is to create a Google Cloud Platform project, if you don’t have one already. Select a new project.

GCP Account Setup

Provide a name for your project and click on create. It might ask you to setup billing if you have not done it before. Google provides you with 300$ worth free credit.

GCP Create Project

To use any of the googles APIs we first need to enable them. Select your project that we have just created and select Enable APIs and services. This will open a bouquet of APIs and services that google has to offer.

GCP Select Project

We will select Google Natural language API and on the next page we will enable it. This finishes our setup requirements on Google Console but we are not finished yet. Moving on, we need to set up is Google Cloud SDK.

Select Natural Language API
Enable Natural Language API

Get the SDK

Download the Google Cloud SDK installer. Follow the instructions and installs it.

Google Cloud SDK Installer

Make sure that the following are selected:

  • Start Google Cloud SDK Shell
  • Run ‘gcloud init’

After the installations is done it will open a terminal window and runs the cloud init command.

Run SDK Setup Tasks

Use the gcloud init command to perform several common SDK setup tasks. These include authorizing the SDK tools to access Google Cloud Platform using your user account credentials and setting up the default SDK configuration.

Accept the option to log in using your Google user account. It will automatically open a browser window wherein you have to login with the same google account credentials which we used to setup our Google console with and click Allow to grant permission to access Google Cloud Platform resources.

gcloud init

After you have successfully logged in, on the terminal it will populate a list of projects in your account. If you only have one project, gcloud init selects it for you.

Next, you need to add the path to the google-cloud-sdk bin to your environment path variable.

Get Your Google API Key

Now our setup is completed but to utilize the google API we still require a key. To get our key we will first create a service account. I will be running these commands from inside my Pycharm IDE.

Create the service account. Replace [NAME] with a name for the service account.

gcloud iam service-accounts create [NAME]

Next, grant permissions to the service account. Replace [PROJECT_ID] with your project ID.

gcloud projects add-iam-policy-binding [PROJECT_ID] --member "serviceAccount:[NAME]@[PROJECT_ID].iam.gserviceaccount.com" --role "roles/owner"

Generate the key file. Replace [FILE_NAME] with a name for the key file:

gcloud iam service-accounts keys create [FILE_NAME].json --iam-account
[NAME]@[PROJECT_ID].iam.gserviceaccount.com
Generate Key File

Use the Google Cloud Natural Language API

We are finally all set to use the Google natural language API. Each sample will be nearly identical except for the inputted text. We will show the sentiment analysis after each example.

First Sentiment Analysis

The below python script calls the analyze sentiment API to get the sentiments of a text. You can try this code and see the results for yourself.

Code

# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"\nlpapikey.json"

# Instantiates a client
client = language.LanguageServiceclient()

# The text to analyze
text = u'Good job everyone!'
document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

Response

The result of the above print statements shoud look something like this:

Text: Hello, world!
Sentiment: 0.30000001192092896, 0.3000000119202896

Process finished with exit code 0

Second Sentiment Analysis

Code

# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"\nlpapikey.json"

# Instantiates a client
client = language.LanguageServiceclient()

# The text to analyze
text = u'it\'s overly sentimental and at times terribly mushy , not to mention very manipulative'
document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

Second Sentiment Response

Text: it's overly sentimental and at times terribly mushy , not to mention very manipulative
Sentiment: -0.8999999761581421,  -0.8999999761581421 

Process finished with exit code 0 

Third Sentiment Analysis

Code

# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"\nlpapikey.json"

# Instantiates a client
client = language.LanguageServiceclient()

# The text to analyze
text = u'the songs are all very good '
document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

Third Sentiment Response

Text: the songs are all very good 
Sentiment: 0.800000011920929,  0.800000011920929 

Process finished with exit code 0  

Conclusion

Did you try it out? Let us know! We’re more than happy to refine our examples based on any input you have.

Tags: