@@ -97,7 +97,10 @@ function analyzeSentimentInFile (bucketName, fileName) {
97
97
function analyzeEntitiesOfText ( text ) {
98
98
// [START language_entities_string]
99
99
// 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' } ) ;
101
104
102
105
// The text to analyze, e.g. "Hello, world!"
103
106
// const text = 'Hello, world!';
@@ -131,7 +134,10 @@ function analyzeEntitiesOfText (text) {
131
134
function analyzeEntitiesInFile ( bucketName , fileName ) {
132
135
// [START language_entities_file]
133
136
// 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' } ) ;
135
141
136
142
// The name of the bucket where the file resides, e.g. "my-bucket"
137
143
// const bucketName = 'my-bucket';
@@ -168,7 +174,10 @@ function analyzeEntitiesInFile (bucketName, fileName) {
168
174
function analyzeSyntaxOfText ( text ) {
169
175
// [START language_syntax_string]
170
176
// 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' } ) ;
172
181
173
182
// The text to analyze, e.g. "Hello, world!"
174
183
// const text = 'Hello, world!';
@@ -199,7 +208,10 @@ function analyzeSyntaxOfText (text) {
199
208
function analyzeSyntaxInFile ( bucketName , fileName ) {
200
209
// [START language_syntax_file]
201
210
// 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' } ) ;
203
215
204
216
// The name of the bucket where the file resides, e.g. "my-bucket"
205
217
// const bucketName = 'my-bucket';
@@ -230,77 +242,73 @@ function analyzeSyntaxInFile (bucketName, fileName) {
230
242
// [END language_syntax_file]
231
243
}
232
244
233
- function analyzeEntitySentimentOfText ( text ) {
234
- // [START language_entity_sentiment_string ]
245
+ function classifyTextOfText ( text ) {
246
+ // [START language_classify_string ]
235
247
// 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 ( ) ;
237
252
238
253
// The text to analyze, e.g. "Hello, world!"
239
254
// const text = 'Hello, world!';
240
255
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'
247
260
} ;
248
261
249
- // Detects sentiment of entities in the document
250
- language . analyzeEntitySentiment ( request )
262
+ // Classifies text in the document
263
+ language . classifyText ( { document : document } )
251
264
. then ( ( results ) => {
252
- const entities = results [ 0 ] . entities ;
265
+ const classification = results [ 0 ] ;
253
266
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 } ` ) ;
260
270
} ) ;
261
271
} )
262
272
. catch ( ( err ) => {
263
273
console . error ( 'ERROR:' , err ) ;
264
274
} ) ;
265
- // [END language_entity_sentiment_string ]
275
+ // [END language_classify_string ]
266
276
}
267
277
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 ( ) ;
272
285
273
286
// The name of the bucket where the file resides, e.g. "my-bucket"
274
287
// const bucketName = 'my-bucket';
275
288
276
289
// The name of the file to analyze, e.g. "file.txt"
277
290
// const fileName = 'file.txt';
278
291
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'
285
296
} ;
286
297
287
- // Detects sentiment of entities in the document
288
- language . analyzeEntitySentiment ( request )
298
+ // Classifies text in the document
299
+ language . classifyText ( { document : document } )
289
300
. then ( ( results ) => {
290
- const entities = results [ 0 ] . entities ;
301
+ const classification = results [ 0 ] ;
291
302
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 } ` ) ;
298
306
} ) ;
299
307
} )
300
308
. catch ( ( err ) => {
301
309
console . error ( 'ERROR:' , err ) ;
302
310
} ) ;
303
- // [END language_entity_sentiment_file ]
311
+ // [END language_classify_file ]
304
312
}
305
313
306
314
require ( `yargs` ) // eslint-disable-line
@@ -342,25 +350,25 @@ require(`yargs`) // eslint-disable-line
342
350
( opts ) => analyzeSyntaxInFile ( opts . bucketName , opts . fileName )
343
351
)
344
352
. 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.` ,
347
355
{ } ,
348
- ( opts ) => analyzeEntitySentimentOfText ( opts . text )
356
+ ( opts ) => classifyTextOfText ( opts . text )
349
357
)
350
358
. 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.` ,
353
361
{ } ,
354
- ( opts ) => analyzeEntitySentimentInFile ( opts . bucketName , opts . fileName )
362
+ ( opts ) => classifyTextInFile ( opts . bucketName , opts . fileName )
355
363
)
356
364
. example ( `node $0 sentiment-text "President Obama is speaking at the White House."` )
357
365
. example ( `node $0 sentiment-file my-bucket file.txt` , `Detects sentiment in gs://my-bucket/file.txt` )
358
366
. example ( `node $0 entities-text "President Obama is speaking at the White House."` )
359
367
. example ( `node $0 entities-file my-bucket file.txt` , `Detects entities in gs://my-bucket/file.txt` )
360
368
. example ( `node $0 syntax-text "President Obama is speaking at the White House."` )
361
369
. 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` )
364
372
. wrap ( 120 )
365
373
. recommendCommands ( )
366
374
. epilogue ( `For more information, see https://cloud.google.com/natural-language/docs` )
0 commit comments