@@ -215,6 +215,87 @@ function analyzeSyntaxInFile (bucketName, fileName) {
215
215
// [END language_syntax_file]
216
216
}
217
217
218
+ function analyzeEntitySentimentOfText ( text ) {
219
+ // [START language_entity_sentiment_string]
220
+ // Imports the Google Cloud client library
221
+ const Language = require ( '@google-cloud/language' ) . v1beta2 ( ) ;
222
+
223
+ // Instantiates a client
224
+ const language = Language . languageServiceClient ( ) ;
225
+
226
+ // The text to analyze, e.g. "Hello, world!"
227
+ // const text = 'Hello, world!';
228
+
229
+ // Configure a request containing a string
230
+ const request = {
231
+ document : {
232
+ type : 'PLAIN_TEXT' ,
233
+ content : text
234
+ }
235
+ } ;
236
+
237
+ // Detects sentiment of entities in the document
238
+ language . analyzeEntitySentiment ( request )
239
+ . then ( ( results ) => {
240
+ const entities = results [ 0 ] . entities ;
241
+
242
+ console . log ( `Entities and sentiments:` )
243
+ entities . forEach ( ( entity ) => {
244
+ console . log ( ` Name: ${ entity . name } ` ) ;
245
+ console . log ( ` Type: ${ entity . type } ` ) ;
246
+ console . log ( ` Score: ${ entity . sentiment . score } ` ) ;
247
+ console . log ( ` Magnitude: ${ entity . sentiment . magnitude } ` ) ;
248
+ } ) ;
249
+ } )
250
+ . catch ( ( err ) => {
251
+ console . error ( 'ERROR:' , err ) ;
252
+ } ) ;
253
+ // [END language_entity_sentiment_string]
254
+ }
255
+
256
+ function analyzeEntitySentimentInFile ( bucketName , fileName ) {
257
+ // [START language_entity_sentiment_file]
258
+ // Imports the Google Cloud client libraries
259
+ const Language = require ( '@google-cloud/language' ) . v1beta2 ( ) ;
260
+ const Storage = require ( '@google-cloud/storage' ) ;
261
+
262
+ // Instantiates the clients
263
+ const language = Language . languageServiceClient ( ) ;
264
+ const storage = Storage ( ) ;
265
+
266
+ // The name of the bucket where the file resides, e.g. "my-bucket"
267
+ // const bucketName = 'my-bucket';
268
+
269
+ // The name of the file to analyze, e.g. "file.txt"
270
+ // const fileName = 'file.txt';
271
+
272
+ // Configure a request containing a string
273
+ const request = {
274
+ document : {
275
+ type : 'PLAIN_TEXT' ,
276
+ gcsContentUri : `gs://${ bucketName } /${ fileName } `
277
+ }
278
+ } ;
279
+
280
+ // Detects sentiment of entities in the document
281
+ language . analyzeEntitySentiment ( request )
282
+ . then ( ( results ) => {
283
+ const entities = results [ 0 ] . entities ;
284
+
285
+ console . log ( `Entities and sentiments:` )
286
+ entities . forEach ( ( entity ) => {
287
+ console . log ( ` Name: ${ entity . name } ` ) ;
288
+ console . log ( ` Type: ${ entity . type } ` ) ;
289
+ console . log ( ` Score: ${ entity . sentiment . score } ` ) ;
290
+ console . log ( ` Magnitude: ${ entity . sentiment . magnitude } ` ) ;
291
+ } ) ;
292
+ } )
293
+ . catch ( ( err ) => {
294
+ console . error ( 'ERROR:' , err ) ;
295
+ } ) ;
296
+ // [END language_entity_sentiment_file]
297
+ }
298
+
218
299
require ( `yargs` )
219
300
. demand ( 1 )
220
301
. command (
@@ -253,12 +334,26 @@ require(`yargs`)
253
334
{ } ,
254
335
( opts ) => analyzeSyntaxInFile ( opts . bucketName , opts . fileName )
255
336
)
337
+ . command (
338
+ `entity-sentiment-text <text>` ,
339
+ `Detects sentiment of the entities in a string.` ,
340
+ { } ,
341
+ ( opts ) => analyzeEntitySentimentOfText ( opts . text )
342
+ )
343
+ . command (
344
+ `entity-sentiment-file <bucketName> <fileName>` ,
345
+ `Detects sentiment of the entities in a file in Google Cloud Storage.` ,
346
+ { } ,
347
+ ( opts ) => analyzeEntitySentimentInFile ( opts . bucketName , opts . fileName )
348
+ )
256
349
. example ( `node $0 sentiment-text "President Obama is speaking at the White House."` )
257
350
. example ( `node $0 sentiment-file my-bucket file.txt` , `Detects sentiment in gs://my-bucket/file.txt` )
258
351
. example ( `node $0 entities-text "President Obama is speaking at the White House."` )
259
352
. example ( `node $0 entities-file my-bucket file.txt` , `Detects entities in gs://my-bucket/file.txt` )
260
353
. example ( `node $0 syntax-text "President Obama is speaking at the White House."` )
261
354
. example ( `node $0 syntax-file my-bucket file.txt` , `Detects syntax in gs://my-bucket/file.txt` )
355
+ . example ( `node $0 entity-sentiment-text "President Obama is speaking at the White House."` )
356
+ . example ( `node $0 entity-sentiment-file my-bucket file.txt` , `Detects sentiment of entities in gs://my-bucket/file.txt` )
262
357
. wrap ( 120 )
263
358
. recommendCommands ( )
264
359
. epilogue ( `For more information, see https://cloud.google.com/natural-language/docs` )
0 commit comments