Skip to content

Commit 5468b07

Browse files
jmdobrygguuss
authored andcommitted
[NL] v1 and v1beta2 updates (GoogleCloudPlatform#470)
* Move entity-level sentiment samples to v1. * Add text classification. * Adds longer text to README.
1 parent e83f4c1 commit 5468b07

File tree

8 files changed

+237
-6991
lines changed

8 files changed

+237
-6991
lines changed

language/README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ __Usage:__ `node analyze.v1.js --help`
4141

4242
```
4343
Commands:
44-
sentiment-text <text> Detects sentiment of a string.
45-
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
46-
entities-text <text> Detects entities in a string.
47-
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
48-
syntax-text <text> Detects syntax of a string.
49-
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
44+
sentiment-text <text> Detects sentiment of a string.
45+
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
46+
entities-text <text> Detects entities in a string.
47+
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
48+
syntax-text <text> Detects syntax of a string.
49+
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
50+
entity-sentiment-text <text> Detects sentiment of the entities in a string.
51+
entity-sentiment-file <bucketName> <fileName> Detects sentiment of the entities in a file in Google Cloud Storage.
5052
5153
Options:
5254
--help Show help [boolean]
@@ -58,6 +60,8 @@ Examples:
5860
node analyze.v1.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
5961
node analyze.v1.js syntax-text "President Obama is speaking at the White House."
6062
node analyze.v1.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt
63+
node analyze.v1.js entity-sentiment-text "President Obama is speaking at the White House."
64+
node analyze.v1.js entity-sentiment-file my-bucket file.txt Detects sentiment of entities in gs://my-bucket/file.txt
6165
6266
For more information, see https://cloud.google.com/natural-language/docs
6367
```
@@ -73,14 +77,14 @@ __Usage:__ `node analyze.v1beta2.js --help`
7377

7478
```
7579
Commands:
76-
sentiment-text <text> Detects sentiment of a string.
77-
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
78-
entities-text <text> Detects entities in a string.
79-
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
80-
syntax-text <text> Detects syntax of a string.
81-
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
82-
entity-sentiment-text <text> Detects sentiment of the entities in a string.
83-
entity-sentiment-file <bucketName> <fileName> Detects sentiment of the entities in a file in Google Cloud Storage.
80+
sentiment-text <text> Detects sentiment of a string.
81+
sentiment-file <bucketName> <fileName> Detects sentiment in a file in Google Cloud Storage.
82+
entities-text <text> Detects entities in a string.
83+
entities-file <bucketName> <fileName> Detects entities in a file in Google Cloud Storage.
84+
syntax-text <text> Detects syntax of a string.
85+
syntax-file <bucketName> <fileName> Detects syntax in a file in Google Cloud Storage.
86+
classify-text <text> Classifies text of a string.
87+
classify-file <bucketName> <fileName> Classifies text in a file in Google Cloud Storage.
8488
8589
Options:
8690
--help Show help [boolean]
@@ -92,9 +96,10 @@ Examples:
9296
node analyze.v1beta2.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
9397
node analyze.v1beta2.js syntax-text "President Obama is speaking at the White House."
9498
node analyze.v1beta2.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt
95-
node analyze.v1beta2.js entity-sentiment-text "President Obama is speaking at the White House."
96-
node analyze.v1beta2.js entity-sentiment-file my-bucket Detects sentiment of entities in gs://my-bucket/file.txt
97-
file.txt
99+
node analyze.v1beta2.js classify-text "Currently the API requires 20 tokens in order \
100+
to return non-empty results. Let's use a longer piece of text for the sample in order to win."
101+
node analyze.v1beta2.js classify-file my-bucket Detects syntax in gs://my-bucket/android_text.txt
102+
android_text.txt
98103
99104
For more information, see https://cloud.google.com/natural-language/docs
100105
```

language/analyze.v1.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,85 @@ function analyzeSyntaxInFile (bucketName, fileName) {
241241
// [END language_syntax_file]
242242
}
243243

244+
function analyzeEntitySentimentOfText (text) {
245+
// [START language_entity_sentiment_string]
246+
// Imports the Google Cloud client library
247+
const Language = require('@google-cloud/language');
248+
249+
// Instantiates the clients
250+
const language = Language();
251+
252+
// The text to analyze, e.g. "Hello, world!"
253+
// const text = 'Hello, world!';
254+
255+
// Configure a request containing a string
256+
const request = {
257+
document: {
258+
type: 'PLAIN_TEXT',
259+
content: text
260+
}
261+
};
262+
263+
// Detects sentiment of entities in the document
264+
language.analyzeEntitySentiment(request)
265+
.then((results) => {
266+
const entities = results[0].entities;
267+
268+
console.log(`Entities and sentiments:`);
269+
entities.forEach((entity) => {
270+
console.log(` Name: ${entity.name}`);
271+
console.log(` Type: ${entity.type}`);
272+
console.log(` Score: ${entity.sentiment.score}`);
273+
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
274+
});
275+
})
276+
.catch((err) => {
277+
console.error('ERROR:', err);
278+
});
279+
// [END language_entity_sentiment_string]
280+
}
281+
282+
function analyzeEntitySentimentInFile (bucketName, fileName) {
283+
// [START language_entity_sentiment_file]
284+
// Imports the Google Cloud client library
285+
const Language = require('@google-cloud/language');
286+
287+
// Instantiates the clients
288+
const language = Language();
289+
290+
// The name of the bucket where the file resides, e.g. "my-bucket"
291+
// const bucketName = 'my-bucket';
292+
293+
// The name of the file to analyze, e.g. "file.txt"
294+
// const fileName = 'file.txt';
295+
296+
// Configure a request containing a string
297+
const request = {
298+
document: {
299+
type: 'PLAIN_TEXT',
300+
gcsContentUri: `gs://${bucketName}/${fileName}`
301+
}
302+
};
303+
304+
// Detects sentiment of entities in the document
305+
language.analyzeEntitySentiment(request)
306+
.then((results) => {
307+
const entities = results[0].entities;
308+
309+
console.log(`Entities and sentiments:`);
310+
entities.forEach((entity) => {
311+
console.log(` Name: ${entity.name}`);
312+
console.log(` Type: ${entity.type}`);
313+
console.log(` Score: ${entity.sentiment.score}`);
314+
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
315+
});
316+
})
317+
.catch((err) => {
318+
console.error('ERROR:', err);
319+
});
320+
// [END language_entity_sentiment_file]
321+
}
322+
244323
require(`yargs`) // eslint-disable-line
245324
.demand(1)
246325
.command(
@@ -279,12 +358,26 @@ require(`yargs`) // eslint-disable-line
279358
{},
280359
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName)
281360
)
361+
.command(
362+
`entity-sentiment-text <text>`,
363+
`Detects sentiment of the entities in a string.`,
364+
{},
365+
(opts) => analyzeEntitySentimentOfText(opts.text)
366+
)
367+
.command(
368+
`entity-sentiment-file <bucketName> <fileName>`,
369+
`Detects sentiment of the entities in a file in Google Cloud Storage.`,
370+
{},
371+
(opts) => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
372+
)
282373
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
283374
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)
284375
.example(`node $0 entities-text "President Obama is speaking at the White House."`)
285376
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`)
286377
.example(`node $0 syntax-text "President Obama is speaking at the White House."`)
287378
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`)
379+
.example(`node $0 entity-sentiment-text "President Obama is speaking at the White House."`)
380+
.example(`node $0 entity-sentiment-file my-bucket file.txt`, `Detects sentiment of entities in gs://my-bucket/file.txt`)
288381
.wrap(120)
289382
.recommendCommands()
290383
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)

language/analyze.v1beta2.js

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ function analyzeSentimentInFile (bucketName, fileName) {
9797
function analyzeEntitiesOfText (text) {
9898
// [START language_entities_string]
9999
// Imports the Google Cloud client library
100-
const language = require('@google-cloud/language').v1beta2();
100+
const Language = require('@google-cloud/language');
101+
102+
// Instantiates the clients
103+
const language = Language({ apiVersion: 'v1beta2' });
101104

102105
// The text to analyze, e.g. "Hello, world!"
103106
// const text = 'Hello, world!';
@@ -131,7 +134,10 @@ function analyzeEntitiesOfText (text) {
131134
function analyzeEntitiesInFile (bucketName, fileName) {
132135
// [START language_entities_file]
133136
// Imports the Google Cloud client libraries
134-
const language = require('@google-cloud/language').v1beta2();
137+
const Language = require('@google-cloud/language');
138+
139+
// Instantiates the clients
140+
const language = Language({ apiVersion: 'v1beta2' });
135141

136142
// The name of the bucket where the file resides, e.g. "my-bucket"
137143
// const bucketName = 'my-bucket';
@@ -168,7 +174,10 @@ function analyzeEntitiesInFile (bucketName, fileName) {
168174
function analyzeSyntaxOfText (text) {
169175
// [START language_syntax_string]
170176
// Imports the Google Cloud client library
171-
const language = require('@google-cloud/language').v1beta2();
177+
const Language = require('@google-cloud/language');
178+
179+
// Instantiates the clients
180+
const language = Language({ apiVersion: 'v1beta2' });
172181

173182
// The text to analyze, e.g. "Hello, world!"
174183
// const text = 'Hello, world!';
@@ -199,7 +208,10 @@ function analyzeSyntaxOfText (text) {
199208
function analyzeSyntaxInFile (bucketName, fileName) {
200209
// [START language_syntax_file]
201210
// Imports the Google Cloud client libraries
202-
const language = require('@google-cloud/language').v1beta2();
211+
const Language = require('@google-cloud/language');
212+
213+
// Instantiates the clients
214+
const language = Language({ apiVersion: 'v1beta2' });
203215

204216
// The name of the bucket where the file resides, e.g. "my-bucket"
205217
// const bucketName = 'my-bucket';
@@ -230,77 +242,73 @@ function analyzeSyntaxInFile (bucketName, fileName) {
230242
// [END language_syntax_file]
231243
}
232244

233-
function analyzeEntitySentimentOfText (text) {
234-
// [START language_entity_sentiment_string]
245+
function classifyTextOfText (text) {
246+
// [START language_classify_string]
235247
// Imports the Google Cloud client library
236-
const language = require('@google-cloud/language').v1beta2();
248+
const Language = require('@google-cloud/language');
249+
250+
// Creates a client
251+
const language = Language.v1beta2();
237252

238253
// The text to analyze, e.g. "Hello, world!"
239254
// const text = 'Hello, world!';
240255

241-
// Configure a request containing a string
242-
const request = {
243-
document: {
244-
type: 'PLAIN_TEXT',
245-
content: text
246-
}
256+
// Instantiates a Document, representing the provided text
257+
const document = {
258+
'content': text,
259+
type: 'PLAIN_TEXT'
247260
};
248261

249-
// Detects sentiment of entities in the document
250-
language.analyzeEntitySentiment(request)
262+
// Classifies text in the document
263+
language.classifyText({ document: document })
251264
.then((results) => {
252-
const entities = results[0].entities;
265+
const classification = results[0];
253266

254-
console.log(`Entities and sentiments:`);
255-
entities.forEach((entity) => {
256-
console.log(` Name: ${entity.name}`);
257-
console.log(` Type: ${entity.type}`);
258-
console.log(` Score: ${entity.sentiment.score}`);
259-
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
267+
console.log('Categories:');
268+
classification.categories.forEach((category) => {
269+
console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
260270
});
261271
})
262272
.catch((err) => {
263273
console.error('ERROR:', err);
264274
});
265-
// [END language_entity_sentiment_string]
275+
// [END language_classify_string]
266276
}
267277

268-
function analyzeEntitySentimentInFile (bucketName, fileName) {
269-
// [START language_entity_sentiment_file]
270-
// Imports the Google Cloud client library
271-
const language = require('@google-cloud/language').v1beta2();
278+
function classifyTextInFile (bucketName, fileName) {
279+
// [START language_classify_file]
280+
// Imports the Google Cloud client libraries
281+
const Language = require('@google-cloud/language');
282+
283+
// Creates a client
284+
const language = Language.v1beta2();
272285

273286
// The name of the bucket where the file resides, e.g. "my-bucket"
274287
// const bucketName = 'my-bucket';
275288

276289
// The name of the file to analyze, e.g. "file.txt"
277290
// const fileName = 'file.txt';
278291

279-
// Configure a request containing a string
280-
const request = {
281-
document: {
282-
type: 'PLAIN_TEXT',
283-
gcsContentUri: `gs://${bucketName}/${fileName}`
284-
}
292+
// Instantiates a Document, representing a text file in Cloud Storage
293+
const document = {
294+
gcsContentUri: `gs://${bucketName}/${fileName}`,
295+
type: 'PLAIN_TEXT'
285296
};
286297

287-
// Detects sentiment of entities in the document
288-
language.analyzeEntitySentiment(request)
298+
// Classifies text in the document
299+
language.classifyText({ document: document })
289300
.then((results) => {
290-
const entities = results[0].entities;
301+
const classification = results[0];
291302

292-
console.log(`Entities and sentiments:`);
293-
entities.forEach((entity) => {
294-
console.log(` Name: ${entity.name}`);
295-
console.log(` Type: ${entity.type}`);
296-
console.log(` Score: ${entity.sentiment.score}`);
297-
console.log(` Magnitude: ${entity.sentiment.magnitude}`);
303+
console.log('Categories:');
304+
classification.categories.forEach((category) => {
305+
console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
298306
});
299307
})
300308
.catch((err) => {
301309
console.error('ERROR:', err);
302310
});
303-
// [END language_entity_sentiment_file]
311+
// [END language_classify_file]
304312
}
305313

306314
require(`yargs`) // eslint-disable-line
@@ -342,25 +350,25 @@ require(`yargs`) // eslint-disable-line
342350
(opts) => analyzeSyntaxInFile(opts.bucketName, opts.fileName)
343351
)
344352
.command(
345-
`entity-sentiment-text <text>`,
346-
`Detects sentiment of the entities in a string.`,
353+
`classify-text <text>`,
354+
`Classifies text of a string.`,
347355
{},
348-
(opts) => analyzeEntitySentimentOfText(opts.text)
356+
(opts) => classifyTextOfText(opts.text)
349357
)
350358
.command(
351-
`entity-sentiment-file <bucketName> <fileName>`,
352-
`Detects sentiment of the entities in a file in Google Cloud Storage.`,
359+
`classify-file <bucketName> <fileName>`,
360+
`Classifies text in a file in Google Cloud Storage.`,
353361
{},
354-
(opts) => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
362+
(opts) => classifyTextInFile(opts.bucketName, opts.fileName)
355363
)
356364
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
357365
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)
358366
.example(`node $0 entities-text "President Obama is speaking at the White House."`)
359367
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`)
360368
.example(`node $0 syntax-text "President Obama is speaking at the White House."`)
361369
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`)
362-
.example(`node $0 entity-sentiment-text "President Obama is speaking at the White House."`)
363-
.example(`node $0 entity-sentiment-file my-bucket file.txt`, `Detects sentiment of entities in gs://my-bucket/file.txt`)
370+
.example(`node $0 classify-text "Android is a mobile operating system developed by Google."`)
371+
.example(`node $0 classify-file my-bucket android_text.txt`, `Detects syntax in gs://my-bucket/android_text.txt`)
364372
.wrap(120)
365373
.recommendCommands()
366374
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)

0 commit comments

Comments
 (0)