Skip to content

Commit f8f7e62

Browse files
authored
Bring ML APIs up to standard. (#346)
1 parent 96af50f commit f8f7e62

File tree

10 files changed

+365
-261
lines changed

10 files changed

+365
-261
lines changed

cloud-language/snippets/analyze.js

Lines changed: 120 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, Google, Inc.
2+
* Copyright 2017, Google, Inc.
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -15,172 +15,205 @@
1515

1616
'use strict';
1717

18-
const Language = require('@google-cloud/language');
19-
const Storage = require('@google-cloud/storage');
20-
21-
// [START language_sentiment_string]
2218
function analyzeSentimentOfText (text) {
19+
// [START language_sentiment_string]
20+
// Imports the Google Cloud client library
21+
const Language = require('@google-cloud/language');
22+
2323
// Instantiates a client
2424
const language = Language();
2525

26+
// The text to analyze, e.g. "Hello, world!"
27+
// const text = 'Hello, world!';
28+
2629
// Instantiates a Document, representing the provided text
27-
const document = language.document({
28-
// The document text, e.g. "Hello, world!"
29-
content: text
30-
});
30+
const document = language.document({ content: text });
3131

3232
// Detects the sentiment of the document
33-
return document.detectSentiment()
33+
document.detectSentiment()
3434
.then((results) => {
3535
const sentiment = results[0];
36-
37-
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);
38-
39-
return sentiment;
36+
console.log(`Score: ${sentiment.score}`);
37+
console.log(`Magnitude: ${sentiment.magnitude}`);
38+
})
39+
.catch((err) => {
40+
console.error('ERROR:', err);
4041
});
42+
// [END language_sentiment_string]
4143
}
42-
// [END language_sentiment_string]
4344

44-
// [START language_sentiment_file]
4545
function analyzeSentimentInFile (bucketName, fileName) {
46-
// Instantiates clients
46+
// [START language_sentiment_file]
47+
// Imports the Google Cloud client libraries
48+
const Language = require('@google-cloud/language');
49+
const Storage = require('@google-cloud/storage');
50+
51+
// Instantiates the clients
4752
const language = Language();
4853
const storage = Storage();
4954

50-
// The bucket where the file resides, e.g. "my-bucket"
51-
const bucket = storage.bucket(bucketName);
52-
// The text file to analyze, e.g. "file.txt"
53-
const file = bucket.file(fileName);
55+
// The name of the bucket where the file resides, e.g. "my-bucket"
56+
// const bucketName = 'my-bucket';
57+
58+
// The name of the file to analyze, e.g. "file.txt"
59+
// const fileName = 'file.txt';
5460

5561
// Instantiates a Document, representing a text file in Cloud Storage
5662
const document = language.document({
57-
// The GCS file
58-
content: file
63+
// The Google Cloud Storage file
64+
content: storage.bucket(bucketName).file(fileName)
5965
});
6066

6167
// Detects the sentiment of the document
62-
return document.detectSentiment()
68+
document.detectSentiment()
6369
.then((results) => {
6470
const sentiment = results[0];
65-
66-
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);
67-
68-
return sentiment;
71+
console.log(`Score: ${sentiment.score}`);
72+
console.log(`Magnitude: ${sentiment.magnitude}`);
73+
})
74+
.catch((err) => {
75+
console.error('ERROR:', err);
6976
});
77+
// [END language_sentiment_file]
7078
}
71-
// [END language_sentiment_file]
7279

73-
// [START language_entities_string]
7480
function analyzeEntitiesOfText (text) {
81+
// [START language_entities_string]
82+
// Imports the Google Cloud client library
83+
const Language = require('@google-cloud/language');
84+
7585
// Instantiates a client
7686
const language = Language();
7787

88+
// The text to analyze, e.g. "Hello, world!"
89+
// const text = 'Hello, world!';
90+
7891
// Instantiates a Document, representing the provided text
79-
const document = language.document({
80-
// The document text, e.g. "Hello, world!"
81-
content: text
82-
});
92+
const document = language.document({ content: text });
8393

8494
// Detects entities in the document
85-
return document.detectEntities()
95+
document.detectEntities()
8696
.then((results) => {
8797
const entities = results[0];
8898

8999
console.log('Entities:');
90-
for (let type in entities) {
91-
console.log(`${type}:`, entities[type]);
92-
}
93-
94-
return entities;
100+
entities.forEach((entity) => {
101+
console.log(entity.name);
102+
console.log(` - Type: ${entity.type}, Salience: ${entity.salience}`);
103+
});
104+
})
105+
.catch((err) => {
106+
console.error('ERROR:', err);
95107
});
108+
// [END language_entities_string]
96109
}
97-
// [END language_entities_string]
98110

99-
// [START language_entities_file]
100111
function analyzeEntitiesInFile (bucketName, fileName) {
101-
// Instantiates clients
112+
// [START language_entities_file]
113+
// Imports the Google Cloud client libraries
114+
const Language = require('@google-cloud/language');
115+
const Storage = require('@google-cloud/storage');
116+
117+
// Instantiates the clients
102118
const language = Language();
103119
const storage = Storage();
104120

105-
// The bucket where the file resides, e.g. "my-bucket"
106-
const bucket = storage.bucket(bucketName);
107-
// The text file to analyze, e.g. "file.txt"
108-
const file = bucket.file(fileName);
121+
// The name of the bucket where the file resides, e.g. "my-bucket"
122+
// const bucketName = 'my-bucket';
123+
124+
// The name of the file to analyze, e.g. "file.txt"
125+
// const fileName = 'file.txt';
109126

110127
// Instantiates a Document, representing a text file in Cloud Storage
111128
const document = language.document({
112-
// The GCS file
113-
content: file
129+
// The Google Cloud Storage file
130+
content: storage.bucket(bucketName).file(fileName)
114131
});
115132

116133
// Detects entities in the document
117-
return document.detectEntities()
134+
document.detectEntities()
118135
.then((results) => {
119136
const entities = results[0];
120137

121138
console.log('Entities:');
122-
for (let type in entities) {
123-
console.log(`${type}:`, entities[type]);
124-
}
125-
126-
return entities;
139+
entities.forEach((entity) => {
140+
console.log(entity.name);
141+
console.log(` - Type: ${entity.type}, Salience: ${entity.salience}`);
142+
});
143+
})
144+
.catch((err) => {
145+
console.error('ERROR:', err);
127146
});
147+
// [END language_entities_file]
128148
}
129-
// [END language_entities_file]
130149

131-
// [START language_syntax_string]
132150
function analyzeSyntaxOfText (text) {
151+
// [START language_syntax_string]
152+
// Imports the Google Cloud client library
153+
const Language = require('@google-cloud/language');
154+
133155
// Instantiates a client
134156
const language = Language();
135157

158+
// The text to analyze, e.g. "Hello, world!"
159+
// const text = 'Hello, world!';
160+
136161
// Instantiates a Document, representing the provided text
137-
const document = language.document({
138-
// The document text, e.g. "Hello, world!"
139-
content: text
140-
});
162+
const document = language.document({ content: text });
141163

142164
// Detects syntax in the document
143-
return document.detectSyntax()
165+
document.detectSyntax()
144166
.then((results) => {
145167
const syntax = results[0];
146168

147-
console.log('Tags:');
148-
syntax.forEach((part) => console.log(part.tag));
149-
150-
return syntax;
169+
console.log('Parts of speech:');
170+
syntax.forEach((part) => {
171+
console.log(`${part.partOfSpeech.tag}:\t ${part.text.content}`);
172+
});
173+
})
174+
.catch((err) => {
175+
console.error('ERROR:', err);
151176
});
177+
// [END language_syntax_string]
152178
}
153-
// [END language_syntax_string]
154179

155-
// [START language_syntax_file]
156180
function analyzeSyntaxInFile (bucketName, fileName) {
157-
// Instantiates clients
181+
// [START language_syntax_file]
182+
// Imports the Google Cloud client libraries
183+
const Language = require('@google-cloud/language');
184+
const Storage = require('@google-cloud/storage');
185+
186+
// Instantiates the clients
158187
const language = Language();
159188
const storage = Storage();
160189

161-
// The bucket where the file resides, e.g. "my-bucket"
162-
const bucket = storage.bucket(bucketName);
163-
// The text file to analyze, e.g. "file.txt"
164-
const file = bucket.file(fileName);
190+
// The name of the bucket where the file resides, e.g. "my-bucket"
191+
// const bucketName = 'my-bucket';
192+
193+
// The name of the file to analyze, e.g. "file.txt"
194+
// const fileName = 'file.txt';
165195

166196
// Instantiates a Document, representing a text file in Cloud Storage
167197
const document = language.document({
168-
// The GCS file
169-
content: file
198+
// The Google Cloud Storage file
199+
content: storage.bucket(bucketName).file(fileName)
170200
});
171201

172202
// Detects syntax in the document
173-
return document.detectSyntax()
203+
document.detectSyntax()
174204
.then((results) => {
175205
const syntax = results[0];
176206

177-
console.log('Tags:');
178-
syntax.forEach((part) => console.log(part.tag));
179-
180-
return syntax;
207+
console.log('Parts of speech:');
208+
syntax.forEach((part) => {
209+
console.log(`${part.partOfSpeech.tag}:\t ${part.text.content}`);
210+
});
211+
})
212+
.catch((err) => {
213+
console.error('ERROR:', err);
181214
});
215+
// [END language_syntax_file]
182216
}
183-
// [END language_syntax_file]
184217

185218
require(`yargs`)
186219
.demand(1)
@@ -191,10 +224,10 @@ require(`yargs`)
191224
(opts) => analyzeSentimentOfText(opts.text)
192225
)
193226
.command(
194-
`sentiment-file <bucket> <filename>`,
227+
`sentiment-file <bucketName> <fileName>`,
195228
`Detects sentiment in a file in Google Cloud Storage.`,
196229
{},
197-
(opts) => analyzeSentimentInFile(opts.bucket, opts.filename)
230+
(opts) => analyzeSentimentInFile(opts.bucketName, opts.fileName)
198231
)
199232
.command(
200233
`entities-text <text>`,
@@ -203,10 +236,10 @@ require(`yargs`)
203236
(opts) => analyzeEntitiesOfText(opts.text)
204237
)
205238
.command(
206-
`entities-file <bucket> <filename>`,
239+
`entities-file <bucketName> <fileName>`,
207240
`Detects entities in a file in Google Cloud Storage.`,
208241
{},
209-
(opts) => analyzeEntitiesInFile(opts.bucket, opts.filename)
242+
(opts) => analyzeEntitiesInFile(opts.bucketName, opts.fileName)
210243
)
211244
.command(
212245
`syntax-text <text>`,
@@ -215,10 +248,10 @@ require(`yargs`)
215248
(opts) => analyzeSyntaxOfText(opts.text)
216249
)
217250
.command(
218-
`syntax-file <bucket> <filename>`,
251+
`syntax-file <bucketName> <fileName>`,
219252
`Detects syntax in a file in Google Cloud Storage.`,
220253
{},
221-
(opts) => analyzeSyntaxInFile(opts.bucket, opts.filename)
254+
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName)
222255
)
223256
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
224257
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)

cloud-language/snippets/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"test": "cd ..; npm run st -- --verbose language/system-test/*.test.js"
99
},
1010
"dependencies": {
11-
"@google-cloud/language": "0.8.0",
12-
"@google-cloud/storage": "0.7.0",
13-
"yargs": "6.6.0"
11+
"@google-cloud/language": "0.10.2",
12+
"@google-cloud/storage": "1.0.0",
13+
"yargs": "7.0.2"
1414
},
1515
"engines": {
1616
"node": ">=4.3.2"

cloud-language/snippets/quickstart.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ const Language = require('@google-cloud/language');
2323
const projectId = 'YOUR_PROJECT_ID';
2424

2525
// Instantiates a client
26-
const languageClient = Language({
26+
const language = Language({
2727
projectId: projectId
2828
});
2929

3030
// The text to analyze
3131
const text = 'Hello, world!';
3232

3333
// Detects the sentiment of the text
34-
languageClient.detectSentiment(text)
34+
language.detectSentiment(text)
3535
.then((results) => {
3636
const sentiment = results[0];
3737

3838
console.log(`Text: ${text}`);
39-
console.log(`Sentiment: ${sentiment}`);
39+
console.log(`Sentiment score: ${sentiment.score}`);
40+
console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
41+
})
42+
.catch((err) => {
43+
console.error('ERROR:', err);
4044
});
4145
// [END language_quickstart]

0 commit comments

Comments
 (0)