-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cloud Natural Language API sample. (#155)
* Add Cloud Natural Language API sample. This sample makes a request to analyze the entities in text. Change-Id: I387cff7ac70c6f3c00a5b213527b4bd71c6c44dc * Update package.json * Do it right this time
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |