Skip to content

Commit c9b76d7

Browse files
jerjousofisl
authored andcommitted
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
1 parent b190cfa commit c9b76d7

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Cloud Natural Language API Sample
2+
3+
These samples demonstrate the use of the
4+
[Google Cloud Natural Language API](https://cloud.google.com/natural-language/docs/).
5+
6+
`analyze.js` is a command-line program that demonstrates how different methods
7+
of the API can be called.
8+
9+
## Setup
10+
11+
Please follow the [Set Up Your Project](https://cloud-dot-devsite.googleplex.com/natural-language/docs/getting-started#set_up_your_project)
12+
steps in the Quickstart doc to create a project and enable the
13+
Cloud Natural Language API. Following those steps, make sure that you
14+
[Set Up a Service Account](https://cloud.google.com/natural-language/docs/common/auth#set_up_a_service_account),
15+
and export the following environment variable:
16+
17+
```
18+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
19+
```
20+
21+
## Run locally
22+
23+
First install the needed dependencies.
24+
25+
```
26+
npm install
27+
```
28+
29+
To run:
30+
31+
```
32+
node analyze.js <sentiment|entities|syntax> <text>
33+
```
34+
35+
For example, the following command returns all entities found in the text:
36+
37+
```
38+
node analyze.js entities "President Obama is speaking at the White House."
39+
```
40+
41+
```
42+
{
43+
"entities": [
44+
{
45+
"name": "Obama",
46+
"type": "PERSON",
47+
"metadata": {
48+
"wikipedia_url": "http://en.wikipedia.org/wiki/Barack_Obama"
49+
},
50+
"salience": 0.84503114,
51+
"mentions": [
52+
{
53+
"text": {
54+
"content": "Obama",
55+
"beginOffset": 10
56+
}
57+
}
58+
]
59+
},
60+
{
61+
"name": "White House",
62+
"type": "LOCATION",
63+
"metadata": {
64+
"wikipedia_url": "http://en.wikipedia.org/wiki/White_House"
65+
},
66+
"salience": 0.15496887,
67+
"mentions": [
68+
{
69+
"text": {
70+
"content": "White House",
71+
"beginOffset": 35
72+
}
73+
}
74+
]
75+
}
76+
],
77+
"language": "en"
78+
}
79+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "cloud-natural-language-api-samples",
3+
"version": "1.0.0",
4+
"description": "Samples for using the Google Cloud Natural Language API.",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
8+
},
9+
"main": "analyze.js",
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"dependencies": {
14+
"googleapis": "^11.0.0"
15+
},
16+
"author": "Google, Inc.",
17+
"license": "Apache-2.0"
18+
}

0 commit comments

Comments
 (0)