Skip to content

Commit

Permalink
Add Cloud Natural Language API sample.
Browse files Browse the repository at this point in the history
This sample makes a request to analyze the entities in text.

Change-Id: I387cff7ac70c6f3c00a5b213527b4bd71c6c44dc
  • Loading branch information
tswast authored and Jerjou Cheng committed Jul 19, 2016
1 parent dc54846 commit c487bef
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 0 deletions.
79 changes: 79 additions & 0 deletions language/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Cloud Natural Language API Sample

These samples demonstrate the use of the
[Google Cloud Natural Language API](https://cloud.google.com/natural-language/docs/).

`analyze.js` is a command-line program that demonstrates how different methods
of the API can be called.

## Setup

Please follow the [Set Up Your Project](https://cloud-dot-devsite.googleplex.com/natural-language/docs/getting-started#set_up_your_project)
steps in the Quickstart doc to create a project and enable the
Cloud Natural Language API. Following those steps, make sure that you
[Set Up a Service Account](https://cloud.google.com/natural-language/docs/common/auth#set_up_a_service_account),
and export the following environment variable:

```
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
```

## Run locally

First install the needed dependencies.

```
npm install
```

To run:

```
node analyze.js <sentiment|entities|syntax> <text>
```

For example, the following command returns all entities found in the text:

```
node analyze.js entities "President Obama is speaking at the White House."
```

```
{
"entities": [
{
"name": "Obama",
"type": "PERSON",
"metadata": {
"wikipedia_url": "http://en.wikipedia.org/wiki/Barack_Obama"
},
"salience": 0.84503114,
"mentions": [
{
"text": {
"content": "Obama",
"beginOffset": 10
}
}
]
},
{
"name": "White House",
"type": "LOCATION",
"metadata": {
"wikipedia_url": "http://en.wikipedia.org/wiki/White_House"
},
"salience": 0.15496887,
"mentions": [
{
"text": {
"content": "White House",
"beginOffset": 35
}
}
]
}
],
"language": "en"
}
```
171 changes: 171 additions & 0 deletions language/analyze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright 2016, Google, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Command-line program to demonstrate how to call different
* methods in Cloud Natural Language API.
*
* To run this example, install npm:
* npm install
*
* You must also set up to authenticate with the Cloud APIs using your
* project's service account credentials. See the README for details.
*
* To run:
* node analyze.js <sentiment|entities|syntax> <text>
*
* Here is an example:
* node analyze.js entities "President Obama is speaking at the White House."
*/
'use strict';

var google = require('googleapis');

var languageScopes = ['https://www.googleapis.com/auth/cloud-platform'];

/**
* Gets a client that is connected to the Google Cloud Natural Language API.
*/
function getLanguageService (callback) {
google.auth.getApplicationDefault(function (err, authClient) {
if (err) {
return callback(err);
}

// Depending on the environment that provides the default credentials
// (e.g. Compute Engine, App Engine), the credentials retrieved may
// require you to specify the scopes you need explicitly.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped(languageScopes);
}

// Load the discovery document for the natural language api service, using
// the acquired credentials.
console.log('Loading language service...');
google.discoverAPI({
url: 'https://language.googleapis.com/$discovery/rest',
version: 'v1beta1',
auth: authClient
}, function (err, languageService) {
if (err) {
return callback(err);
}
callback(null, languageService, authClient);
});
});
}

function analyzeSentiment (inputText, languageService, authClient, callback) {
languageService.documents.analyzeSentiment(
{
auth: authClient,
resource: { // Resource is used as the body for the API call.
document: {
content: inputText,
type: 'PLAIN_TEXT'
}
}
},
function (err, result) {
if (err) {
return callback(err);
}
callback(null, result);
});
}

function analyzeEntities (inputText, languageService, authClient, callback) {
languageService.documents.analyzeEntities(
{
auth: authClient,
resource: { // Resource is used as the body for the API call.
document: {
content: inputText,
type: 'PLAIN_TEXT'
},
encoding_type: 'UTF16'
}
},
function (err, result) {
if (err) {
return callback(err);
}
callback(null, result);
});
}

function analyzeSyntax (inputText, languageService, authClient, callback) {
languageService.documents.annotateText(
{
auth: authClient,
resource: { // Resource is used as the body for the API call.
document: {
content: inputText,
type: 'PLAIN_TEXT'
},
features: {
extract_syntax: 'TRUE'
},
encoding_type: 'UTF16'
}
},
function (err, result) {
if (err) {
return callback(err);
}
callback(null, result);
});
}

// Run the examples.
exports.main = function (command, inputText, callback) {
getLanguageService(function (err, languageService, authClient) {
if (err) {
return callback(err);
}

var resultCallback = function (err, result) {
if (err) {
return callback(err);
}
callback(null, result);
};
if (command === 'sentiment') {
analyzeSentiment(inputText, languageService, authClient, resultCallback);
} else if (command === 'entities') {
analyzeEntities(inputText, languageService, authClient, resultCallback);
} else if (command === 'syntax') {
analyzeSyntax(inputText, languageService, authClient, resultCallback);
} else {
return callback(err);
}
});
};

if (require.main === module) {
var args = process.argv.slice(2);
if (args.length !== 2) {
console.log('Incorrect number of arguments. ' +
'Usage: node analyze.js <sentiment|entities|syntax> <text>');
process.exit(1);
}
if (['sentiment', 'entities', 'syntax'].indexOf(args[0]) === -1) {
console.log('Incorrect command. ' +
'Usage: node analyze.js <sentiment|entities|syntax> <text>');
process.exit(1);
}
exports.main(args[0], args[1], function (result) {
console.log(JSON.stringify(result, null, ' '));
});
}
18 changes: 18 additions & 0 deletions language/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cloud-natural-language-api-samples",
"version": "1.0.0",
"description": "Samples for using the Google Cloud Natural Language API.",
"repository": {
"type": "git",
"url": "git://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"main": "analyze.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"googleapis": "^11.0.0"
},
"author": "Google, Inc.",
"license": "Apache-2.0"
}
62 changes: 62 additions & 0 deletions test/language/analyze.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

var analyzeExample = require('../../language/analyze');

describe('language:analyze', function () {
it('should analyze sentiment in text', function (done) {
analyzeExample.main(
'sentiment',
'This gazinga pin is bad and it should feel bad',
function (err, result) {
assert(!err);
assert(result);
assert(result.documentSentiment);
assert(result.documentSentiment.polarity < 0);
done();
}
);
});
it('should analyze entities in text', function (done) {
analyzeExample.main(
'entities',
'Mark Twain is the author of a book called Tom Sawyer',
function (err, result) {
assert(!err);
assert(result);
assert(result.entities && result.entities.length);
assert(result.entities[0].name === 'Mark Twain');
assert(result.entities[0].type === 'PERSON');
done();
}
);
});
it('should analyze syntax in text', function (done) {
analyzeExample.main(
'syntax',
'Betty bought a bit of bitter butter. But she said, "This butter\'s ' +
'bitter! If I put it in my batter, it will make my batter bitter. If I ' +
'buy some better butter - better than this bitter butter - it will ' +
'make my batter better."',
function (err, result) {
assert(!err);
assert(result);
assert(result.sentences && result.sentences.length);
assert(result.tokens && result.tokens.length > 5);
done();
}
);
});
});

0 comments on commit c487bef

Please sign in to comment.