18
18
function analyzeSentimentOfText ( text ) {
19
19
// [START language_sentiment_string]
20
20
// Imports the Google Cloud client library
21
- const LanguageServiceClient = require ( '@google-cloud/language' )
22
- . LanguageServiceClient ;
21
+ const language = require ( '@google-cloud/language' ) ;
23
22
24
23
// Creates a client
25
- const language = new LanguageServiceClient ( ) ;
24
+ const client = new language . LanguageServiceClient ( ) ;
26
25
27
26
/**
28
27
* TODO(developer): Uncomment the following line to run this code.
@@ -36,7 +35,7 @@ function analyzeSentimentOfText(text) {
36
35
} ;
37
36
38
37
// Detects the sentiment of the document
39
- language
38
+ client
40
39
. analyzeSentiment ( { document : document } )
41
40
. then ( results => {
42
41
const sentiment = results [ 0 ] . documentSentiment ;
@@ -60,11 +59,10 @@ function analyzeSentimentOfText(text) {
60
59
function analyzeSentimentInFile ( bucketName , fileName ) {
61
60
// [START language_sentiment_file]
62
61
// Imports the Google Cloud client library
63
- const LanguageServiceClient = require ( '@google-cloud/language' )
64
- . LanguageServiceClient ;
62
+ const language = require ( '@google-cloud/language' ) ;
65
63
66
64
// Creates a client
67
- const language = new LanguageServiceClient ( ) ;
65
+ const client = new language . LanguageServiceClient ( ) ;
68
66
69
67
/**
70
68
* TODO(developer): Uncomment the following lines to run this code
@@ -79,7 +77,7 @@ function analyzeSentimentInFile(bucketName, fileName) {
79
77
} ;
80
78
81
79
// Detects the sentiment of the document
82
- language
80
+ client
83
81
. analyzeSentiment ( { document : document } )
84
82
. then ( results => {
85
83
const sentiment = results [ 0 ] . documentSentiment ;
@@ -103,11 +101,10 @@ function analyzeSentimentInFile(bucketName, fileName) {
103
101
function analyzeEntitiesOfText ( text ) {
104
102
// [START language_entities_string]
105
103
// Imports the Google Cloud client library
106
- const LanguageServiceClient = require ( '@google-cloud/language' )
107
- . LanguageServiceClient ;
104
+ const language = require ( '@google-cloud/language' ) ;
108
105
109
106
// Creates a client
110
- const language = new LanguageServiceClient ( ) ;
107
+ const client = new language . LanguageServiceClient ( ) ;
111
108
112
109
/**
113
110
* TODO(developer): Uncomment the following line to run this code.
@@ -121,7 +118,7 @@ function analyzeEntitiesOfText(text) {
121
118
} ;
122
119
123
120
// Detects entities in the document
124
- language
121
+ client
125
122
. analyzeEntities ( { document : document } )
126
123
. then ( results => {
127
124
const entities = results [ 0 ] . entities ;
@@ -144,11 +141,10 @@ function analyzeEntitiesOfText(text) {
144
141
function analyzeEntitiesInFile ( bucketName , fileName ) {
145
142
// [START language_entities_file]
146
143
// Imports the Google Cloud client library
147
- const LanguageServiceClient = require ( '@google-cloud/language' )
148
- . LanguageServiceClient ;
144
+ const language = require ( '@google-cloud/language' ) ;
149
145
150
146
// Creates a client
151
- const language = new LanguageServiceClient ( ) ;
147
+ const client = new language . LanguageServiceClient ( ) ;
152
148
153
149
/**
154
150
* TODO(developer): Uncomment the following lines to run this code
@@ -163,7 +159,7 @@ function analyzeEntitiesInFile(bucketName, fileName) {
163
159
} ;
164
160
165
161
// Detects entities in the document
166
- language
162
+ client
167
163
. analyzeEntities ( { document : document } )
168
164
. then ( results => {
169
165
const entities = results [ 0 ] . entities ;
@@ -186,11 +182,10 @@ function analyzeEntitiesInFile(bucketName, fileName) {
186
182
function analyzeSyntaxOfText ( text ) {
187
183
// [START language_syntax_string]
188
184
// Imports the Google Cloud client library
189
- const LanguageServiceClient = require ( '@google-cloud/language' )
190
- . LanguageServiceClient ;
185
+ const language = require ( '@google-cloud/language' ) ;
191
186
192
187
// Creates a client
193
- const language = new LanguageServiceClient ( ) ;
188
+ const client = new language . LanguageServiceClient ( ) ;
194
189
195
190
/**
196
191
* TODO(developer): Uncomment the following line to run this code.
@@ -204,7 +199,7 @@ function analyzeSyntaxOfText(text) {
204
199
} ;
205
200
206
201
// Detects syntax in the document
207
- language
202
+ client
208
203
. analyzeSyntax ( { document : document } )
209
204
. then ( results => {
210
205
const syntax = results [ 0 ] ;
@@ -224,11 +219,10 @@ function analyzeSyntaxOfText(text) {
224
219
function analyzeSyntaxInFile ( bucketName , fileName ) {
225
220
// [START language_syntax_file]
226
221
// Imports the Google Cloud client library
227
- const LanguageServiceClient = require ( '@google-cloud/language' )
228
- . LanguageServiceClient ;
222
+ const language = require ( '@google-cloud/language' ) ;
229
223
230
224
// Creates a client
231
- const language = new LanguageServiceClient ( ) ;
225
+ const client = new language . LanguageServiceClient ( ) ;
232
226
233
227
/**
234
228
* TODO(developer): Uncomment the following lines to run this code
@@ -243,7 +237,7 @@ function analyzeSyntaxInFile(bucketName, fileName) {
243
237
} ;
244
238
245
239
// Detects syntax in the document
246
- language
240
+ client
247
241
. analyzeSyntax ( { document : document } )
248
242
. then ( results => {
249
243
const syntax = results [ 0 ] ;
@@ -263,11 +257,10 @@ function analyzeSyntaxInFile(bucketName, fileName) {
263
257
function analyzeEntitySentimentOfText ( text ) {
264
258
// [START language_entity_sentiment_string]
265
259
// Imports the Google Cloud client library
266
- const LanguageServiceClient = require ( '@google-cloud/language' )
267
- . LanguageServiceClient ;
260
+ const language = require ( '@google-cloud/language' ) ;
268
261
269
262
// Creates a client
270
- const language = new LanguageServiceClient ( ) ;
263
+ const client = new language . LanguageServiceClient ( ) ;
271
264
272
265
/**
273
266
* TODO(developer): Uncomment the following line to run this code.
@@ -281,7 +274,7 @@ function analyzeEntitySentimentOfText(text) {
281
274
} ;
282
275
283
276
// Detects sentiment of entities in the document
284
- language
277
+ client
285
278
. analyzeEntitySentiment ( { document : document } )
286
279
. then ( results => {
287
280
const entities = results [ 0 ] . entities ;
@@ -303,11 +296,10 @@ function analyzeEntitySentimentOfText(text) {
303
296
function analyzeEntitySentimentInFile ( bucketName , fileName ) {
304
297
// [START language_entity_sentiment_file]
305
298
// Imports the Google Cloud client library
306
- const LanguageServiceClient = require ( '@google-cloud/language' )
307
- . LanguageServiceClient ;
299
+ const language = require ( '@google-cloud/language' ) ;
308
300
309
301
// Creates a client
310
- const language = new LanguageServiceClient ( ) ;
302
+ const client = new language . LanguageServiceClient ( ) ;
311
303
312
304
/**
313
305
* TODO(developer): Uncomment the following lines to run this code
@@ -322,7 +314,7 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
322
314
} ;
323
315
324
316
// Detects sentiment of entities in the document
325
- language
317
+ client
326
318
. analyzeEntitySentiment ( { document : document } )
327
319
. then ( results => {
328
320
const entities = results [ 0 ] . entities ;
@@ -341,6 +333,83 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
341
333
// [END language_entity_sentiment_file]
342
334
}
343
335
336
+ function classifyTextOfText ( text ) {
337
+ // [START language_classify_string]
338
+ // Imports the Google Cloud client library
339
+ const language = require ( '@google-cloud/language' ) ;
340
+
341
+ // Creates a client
342
+ const client = new language . LanguageServiceClient ( ) ;
343
+
344
+ /**
345
+ * TODO(developer): Uncomment the following line to run this code.
346
+ */
347
+ // const text = 'Your text to analyze, e.g. Hello, world!';
348
+
349
+ // Prepares a document, representing the provided text
350
+ const document = {
351
+ content : text ,
352
+ type : 'PLAIN_TEXT' ,
353
+ } ;
354
+
355
+ // Classifies text in the document
356
+ client
357
+ . classifyText ( { document : document } )
358
+ . then ( results => {
359
+ const classification = results [ 0 ] ;
360
+
361
+ console . log ( 'Categories:' ) ;
362
+ classification . categories . forEach ( category => {
363
+ console . log (
364
+ `Name: ${ category . name } , Confidence: ${ category . confidence } `
365
+ ) ;
366
+ } ) ;
367
+ } )
368
+ . catch ( err => {
369
+ console . error ( 'ERROR:' , err ) ;
370
+ } ) ;
371
+ // [END language_classify_string]
372
+ }
373
+
374
+ function classifyTextInFile ( bucketName , fileName ) {
375
+ // [START language_classify_file]
376
+ // Imports the Google Cloud client library.
377
+ const language = require ( '@google-cloud/language' ) ;
378
+
379
+ // Creates a client.
380
+ const client = new language . LanguageServiceClient ( ) ;
381
+
382
+ /**
383
+ * TODO(developer): Uncomment the following lines to run this code
384
+ */
385
+ // const bucketName = 'Your bucket name, e.g. my-bucket';
386
+ // const fileName = 'Your file name, e.g. my-file.txt';
387
+
388
+ // Prepares a document, representing a text file in Cloud Storage
389
+ const document = {
390
+ gcsContentUri : `gs://${ bucketName } /${ fileName } ` ,
391
+ type : 'PLAIN_TEXT' ,
392
+ } ;
393
+
394
+ // Classifies text in the document
395
+ client
396
+ . classifyText ( { document : document } )
397
+ . then ( results => {
398
+ const classification = results [ 0 ] ;
399
+
400
+ console . log ( 'Categories:' ) ;
401
+ classification . categories . forEach ( category => {
402
+ console . log (
403
+ `Name: ${ category . name } , Confidence: ${ category . confidence } `
404
+ ) ;
405
+ } ) ;
406
+ } )
407
+ . catch ( err => {
408
+ console . error ( 'ERROR:' , err ) ;
409
+ } ) ;
410
+ // [END language_classify_file]
411
+ }
412
+
344
413
require ( `yargs` )
345
414
. demand ( 1 )
346
415
. command (
@@ -385,6 +454,15 @@ require(`yargs`)
385
454
{ } ,
386
455
opts => analyzeEntitySentimentInFile ( opts . bucketName , opts . fileName )
387
456
)
457
+ . command ( `classify-text <text>` , `Classifies text of a string.` , { } , opts =>
458
+ classifyTextOfText ( opts . text )
459
+ )
460
+ . command (
461
+ `classify-file <bucketName> <fileName>` ,
462
+ `Classifies text in a file in Google Cloud Storage.` ,
463
+ { } ,
464
+ opts => classifyTextInFile ( opts . bucketName , opts . fileName )
465
+ )
388
466
. example (
389
467
`node $0 sentiment-text "President Obama is speaking at the White House."`
390
468
)
@@ -413,6 +491,13 @@ require(`yargs`)
413
491
`node $0 entity-sentiment-file my-bucket file.txt` ,
414
492
`Detects sentiment of entities in gs://my-bucket/file.txt`
415
493
)
494
+ . example (
495
+ `node $0 classify-text "Android is a mobile operating system developed by Google."`
496
+ )
497
+ . example (
498
+ `node $0 classify-file my-bucket android_text.txt` ,
499
+ `Detects syntax in gs://my-bucket/android_text.txt`
500
+ )
416
501
. wrap ( 120 )
417
502
. recommendCommands ( )
418
503
. epilogue (
0 commit comments