1
1
/**
2
- * Copyright 2016 , Google, Inc.
2
+ * Copyright 2017 , Google, Inc.
3
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License.
5
5
* You may obtain a copy of the License at
15
15
16
16
'use strict' ;
17
17
18
- const Language = require ( '@google-cloud/language' ) ;
19
- const Storage = require ( '@google-cloud/storage' ) ;
20
-
21
- // [START language_sentiment_string]
22
18
function analyzeSentimentOfText ( text ) {
19
+ // [START language_sentiment_string]
20
+ // Imports the Google Cloud client library
21
+ const Language = require ( '@google-cloud/language' ) ;
22
+
23
23
// Instantiates a client
24
24
const language = Language ( ) ;
25
25
26
+ // The text to analyze, e.g. "Hello, world!"
27
+ // const text = 'Hello, world!';
28
+
26
29
// 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 } ) ;
31
31
32
32
// Detects the sentiment of the document
33
- return document . detectSentiment ( )
33
+ document . detectSentiment ( )
34
34
. then ( ( results ) => {
35
35
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 ) ;
40
41
} ) ;
42
+ // [END language_sentiment_string]
41
43
}
42
- // [END language_sentiment_string]
43
44
44
- // [START language_sentiment_file]
45
45
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
47
52
const language = Language ( ) ;
48
53
const storage = Storage ( ) ;
49
54
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';
54
60
55
61
// Instantiates a Document, representing a text file in Cloud Storage
56
62
const document = language . document ( {
57
- // The GCS file
58
- content : file
63
+ // The Google Cloud Storage file
64
+ content : storage . bucket ( bucketName ) . file ( fileName )
59
65
} ) ;
60
66
61
67
// Detects the sentiment of the document
62
- return document . detectSentiment ( )
68
+ document . detectSentiment ( )
63
69
. then ( ( results ) => {
64
70
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 ) ;
69
76
} ) ;
77
+ // [END language_sentiment_file]
70
78
}
71
- // [END language_sentiment_file]
72
79
73
- // [START language_entities_string]
74
80
function analyzeEntitiesOfText ( text ) {
81
+ // [START language_entities_string]
82
+ // Imports the Google Cloud client library
83
+ const Language = require ( '@google-cloud/language' ) ;
84
+
75
85
// Instantiates a client
76
86
const language = Language ( ) ;
77
87
88
+ // The text to analyze, e.g. "Hello, world!"
89
+ // const text = 'Hello, world!';
90
+
78
91
// 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 } ) ;
83
93
84
94
// Detects entities in the document
85
- return document . detectEntities ( )
95
+ document . detectEntities ( )
86
96
. then ( ( results ) => {
87
97
const entities = results [ 0 ] ;
88
98
89
99
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 ) ;
95
107
} ) ;
108
+ // [END language_entities_string]
96
109
}
97
- // [END language_entities_string]
98
110
99
- // [START language_entities_file]
100
111
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
102
118
const language = Language ( ) ;
103
119
const storage = Storage ( ) ;
104
120
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';
109
126
110
127
// Instantiates a Document, representing a text file in Cloud Storage
111
128
const document = language . document ( {
112
- // The GCS file
113
- content : file
129
+ // The Google Cloud Storage file
130
+ content : storage . bucket ( bucketName ) . file ( fileName )
114
131
} ) ;
115
132
116
133
// Detects entities in the document
117
- return document . detectEntities ( )
134
+ document . detectEntities ( )
118
135
. then ( ( results ) => {
119
136
const entities = results [ 0 ] ;
120
137
121
138
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 ) ;
127
146
} ) ;
147
+ // [END language_entities_file]
128
148
}
129
- // [END language_entities_file]
130
149
131
- // [START language_syntax_string]
132
150
function analyzeSyntaxOfText ( text ) {
151
+ // [START language_syntax_string]
152
+ // Imports the Google Cloud client library
153
+ const Language = require ( '@google-cloud/language' ) ;
154
+
133
155
// Instantiates a client
134
156
const language = Language ( ) ;
135
157
158
+ // The text to analyze, e.g. "Hello, world!"
159
+ // const text = 'Hello, world!';
160
+
136
161
// 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 } ) ;
141
163
142
164
// Detects syntax in the document
143
- return document . detectSyntax ( )
165
+ document . detectSyntax ( )
144
166
. then ( ( results ) => {
145
167
const syntax = results [ 0 ] ;
146
168
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 ) ;
151
176
} ) ;
177
+ // [END language_syntax_string]
152
178
}
153
- // [END language_syntax_string]
154
179
155
- // [START language_syntax_file]
156
180
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
158
187
const language = Language ( ) ;
159
188
const storage = Storage ( ) ;
160
189
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';
165
195
166
196
// Instantiates a Document, representing a text file in Cloud Storage
167
197
const document = language . document ( {
168
- // The GCS file
169
- content : file
198
+ // The Google Cloud Storage file
199
+ content : storage . bucket ( bucketName ) . file ( fileName )
170
200
} ) ;
171
201
172
202
// Detects syntax in the document
173
- return document . detectSyntax ( )
203
+ document . detectSyntax ( )
174
204
. then ( ( results ) => {
175
205
const syntax = results [ 0 ] ;
176
206
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 ) ;
181
214
} ) ;
215
+ // [END language_syntax_file]
182
216
}
183
- // [END language_syntax_file]
184
217
185
218
require ( `yargs` )
186
219
. demand ( 1 )
@@ -191,10 +224,10 @@ require(`yargs`)
191
224
( opts ) => analyzeSentimentOfText ( opts . text )
192
225
)
193
226
. command (
194
- `sentiment-file <bucket > <filename >` ,
227
+ `sentiment-file <bucketName > <fileName >` ,
195
228
`Detects sentiment in a file in Google Cloud Storage.` ,
196
229
{ } ,
197
- ( opts ) => analyzeSentimentInFile ( opts . bucket , opts . filename )
230
+ ( opts ) => analyzeSentimentInFile ( opts . bucketName , opts . fileName )
198
231
)
199
232
. command (
200
233
`entities-text <text>` ,
@@ -203,10 +236,10 @@ require(`yargs`)
203
236
( opts ) => analyzeEntitiesOfText ( opts . text )
204
237
)
205
238
. command (
206
- `entities-file <bucket > <filename >` ,
239
+ `entities-file <bucketName > <fileName >` ,
207
240
`Detects entities in a file in Google Cloud Storage.` ,
208
241
{ } ,
209
- ( opts ) => analyzeEntitiesInFile ( opts . bucket , opts . filename )
242
+ ( opts ) => analyzeEntitiesInFile ( opts . bucketName , opts . fileName )
210
243
)
211
244
. command (
212
245
`syntax-text <text>` ,
@@ -215,10 +248,10 @@ require(`yargs`)
215
248
( opts ) => analyzeSyntaxOfText ( opts . text )
216
249
)
217
250
. command (
218
- `syntax-file <bucket > <filename >` ,
251
+ `syntax-file <bucketName > <fileName >` ,
219
252
`Detects syntax in a file in Google Cloud Storage.` ,
220
253
{ } ,
221
- ( opts ) => analyzeSyntaxInFile ( opts . bucket , opts . filename )
254
+ ( opts ) => analyzeSyntaxInFile ( opts . bucketName , opts . fileName )
222
255
)
223
256
. example ( `node $0 sentiment-text "President Obama is speaking at the White House."` )
224
257
. example ( `node $0 sentiment-file my-bucket file.txt` , `Detects sentiment in gs://my-bucket/file.txt` )
0 commit comments