Google Cloud Natural

Google Cloud Natural

  • Category: Text Analysis


Google Cloud Natural Language API Example Code in JavaScript

The Google Cloud Natural Language API is a Public API that allows you to extract insights from text using Machine Learning models developed by Google. It can analyze text and return information about the sentiment, entities, and syntax of the text.

In this blog post, we will explore how to use the Google Cloud Natural Language API in JavaScript and provide example code snippets.

Requirements

Before we start looking at the code snippets, you need to have the following:

  • A Google Cloud account
  • Access to the Google Cloud Console
  • A project in the Google Cloud Console with the Cloud Natural Language API enabled

Getting Started

To use the Google Cloud Natural Language API in JavaScript, you need to follow these steps:

  1. Install the @google-cloud/language package using npm:

    npm install @google-cloud/language
    
  2. Authenticate with Google Cloud using a service account and the GOOGLE_APPLICATION_CREDENTIALS environment variable. You can create a new service account in the Google Cloud Console and download its JSON key file. Then, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the path of the JSON key file. For example:

    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
    
  3. Write the code to use the API.

Example Code Snippets

Analyze Sentiment

Analyze the sentiment of a piece of text:

const {LanguageServiceClient} = require('@google-cloud/language');
const client = new LanguageServiceClient();

async function analyzeSentiment() {
  const document = {
    content: 'I am so happy and grateful to be alive.',
    type: 'PLAIN_TEXT',
  };

  const [result] = await client.analyzeSentiment({document});
  const sentiment = result.documentSentiment;

  console.log(`Score: ${sentiment.score}`);
  console.log(`Magnitude: ${sentiment.magnitude}`);
}

analyzeSentiment();

Analyze Entities

Extract entities (e.g. people, places, organizations) from a piece of text:

async function analyzeEntities() {
  const document = {
    content: 'Sundar Pichai is the CEO of Google.',
    type: 'PLAIN_TEXT',
  };

  const [result] = await client.analyzeEntities({document});
  const entities = result.entities;

  console.log('Entities:');
  entities.forEach(entity => {
    console.log(entity.name);
    console.log(` - Type: ${entity.type}`);
    console.log(` - Salience: ${entity.salience}`);
  });
}

analyzeEntities();

Analyze Syntax

Extract the syntax (e.g. parts of speech, dependency labels) from a piece of text:

async function analyzeSyntax() {
  const document = {
    content: 'I am learning how to use the Google Cloud Natural Language API.',
    type: 'PLAIN_TEXT',
  };

  const [result] = await client.analyzeSyntax({document});
  const tokens = result.tokens;

  console.log('Tokens:');
  tokens.forEach(token => {
    console.log(token.text.content);
    console.log(` - Part of Speech: ${token.partOfSpeech.tag}`);
    console.log(` - Lemma: ${token.lemma}`);
  });
}

analyzeSyntax();

Conclusion

In this blog post, we explored how to use the Google Cloud Natural Language API in JavaScript and provided example code snippets for analyzing sentiment, entities, and syntax in text. The Google Cloud Natural Language API is a powerful tool that can help you extract valuable insights from text and improve the accuracy of your applications.

Visit to Google Cloud Natural website

Similar APIs of Text Analysis

Name Parser

Name Parser

Text Analysis

Name Parser is an API service that splits a complete name into useful information such as first name, last name, gender and nationality.

nameparsergender

Optical Character Recognition

Optical Character Recognition

Text Analysis

Analysis text from image. The free OCR API provides a simple way of parsing images and multi-page PDF documents (PDF OCR) and getting the extracted text results returned in a JSON format. The API can be used from any internet-connected device (desktop, mobile, iPhone, Android, Windows phone, refrigerator...). For even faster response times and guaranteed uptime PRO plans are available. In addition, we offer a locally (on-premise) installable version with our OCR.space Local Enterprise licenses.

OCRimgtext

Cloudmersive Natural Language Processing

Cloudmersive Natural Language Processing

Text Analysis

Natural language processing and text analysis. The most powerful and cost-effective NLP AI APIs, continuously updated. The most powerful and cost-effective NLP AI APIs, continuously updated. Automatically rephrase, or paraphrase an input sentence into alternate, gramatically correct text that preserves the original meaning. Automatically classify the sentiment of input text into Positive, Negative, or Neutral and a corresponding sentiment strength score. Identify the part of speech of words in a sentence. Pull out all the verbs, nouns, proper nouns, etc. from a sentence. Identify the true meaning. Fully parse a sentence into its constituent structure. Perform analytics over unstructured text to identify sentiment, key words and phrases, and much more. Identify the key entities that are referenced in unstructured text. Automatically detect the language of a text string across the world's 14 most popular languages.

translationrephrasingsentiment

Language Detection API

Language Detection API

Text Analysis

Detects text language. Language identification JSON webservice accepts text and returns detected language code and score. Detects 164 languages Fast, supports batching Supports short phrases and single words Secure

languagefastsupport

Semantira

Semantira

Text Analysis

Text Analytics with sentiment analysis, categorization & named entity extraction. Uncover context-rich patterns and insights hidden within mountains of social comments, surveys, reviews, and any other text documents See who’s talking, what they’re talking about, how they feel, and why.

richcontextinsights

Aylien Text Analysis

Aylien Text Analysis

Text Analysis

A collection of information retrieval, and natural language APIs. A content analysis API used for collecting and analyzing news content at scale. An easy-to-use API used to perform a variety of complex NLP tasks on documents, reviews, social comments, or any other type of text. A user friendly platform that makes training custom NLP models easy.

analysissearchfeed

Watson Natural Language Understanding

Watson Natural Language Understanding

Text Analysis

Natural language processing for advanced text analysis. Analyze various features of text content at scale. Provide text, raw HTML, or a public URL and IBM Watson Natural Language Understanding will give you results for the features you request. The service cleans HTML content before analysis by default, so the results can ignore most advertisements and other unwanted content.

rawhtmlanalytics