@@ -26,18 +26,20 @@ function analyzeSentimentOfText (text) {
26
26
// The text to analyze, e.g. "Hello, world!"
27
27
// const text = 'Hello, world!';
28
28
29
- // Instantiates a Document, representing the provided text
30
- const document = language . document ( { content : text } ) ;
29
+ const document = {
30
+ 'content' : text ,
31
+ type : 'PLAIN_TEXT'
32
+ } ;
31
33
32
34
// Detects the sentiment of the document
33
- document . detectSentiment ( )
35
+ language . analyzeSentiment ( { document : document } )
34
36
. then ( ( results ) => {
35
- const sentiment = results [ 1 ] . documentSentiment ;
37
+ const sentiment = results [ 0 ] . documentSentiment ;
36
38
console . log ( `Document sentiment:` ) ;
37
39
console . log ( ` Score: ${ sentiment . score } ` ) ;
38
40
console . log ( ` Magnitude: ${ sentiment . magnitude } ` ) ;
39
41
40
- const sentences = results [ 1 ] . sentences ;
42
+ const sentences = results [ 0 ] . sentences ;
41
43
sentences . forEach ( ( sentence ) => {
42
44
console . log ( `Sentence: ${ sentence . text . content } ` ) ;
43
45
console . log ( ` Score: ${ sentence . sentiment . score } ` ) ;
@@ -54,11 +56,9 @@ function analyzeSentimentInFile (bucketName, fileName) {
54
56
// [START language_sentiment_file]
55
57
// Imports the Google Cloud client libraries
56
58
const Language = require ( '@google-cloud/language' ) ;
57
- const Storage = require ( '@google-cloud/storage' ) ;
58
59
59
60
// Instantiates the clients
60
61
const language = Language ( ) ;
61
- const storage = Storage ( ) ;
62
62
63
63
// The name of the bucket where the file resides, e.g. "my-bucket"
64
64
// const bucketName = 'my-bucket';
@@ -67,20 +67,20 @@ function analyzeSentimentInFile (bucketName, fileName) {
67
67
// const fileName = 'file.txt';
68
68
69
69
// Instantiates a Document, representing a text file in Cloud Storage
70
- const document = language . document ( {
71
- // The Google Cloud Storage file
72
- content : storage . bucket ( bucketName ) . file ( fileName )
73
- } ) ;
70
+ const document = {
71
+ gcsContentUri : `gs:// ${ bucketName } / ${ fileName } ` ,
72
+ type : 'PLAIN_TEXT'
73
+ } ;
74
74
75
75
// Detects the sentiment of the document
76
- document . detectSentiment ( )
76
+ language . analyzeSentiment ( { document : document } )
77
77
. then ( ( results ) => {
78
- const sentiment = results [ 1 ] . documentSentiment ;
78
+ const sentiment = results [ 0 ] . documentSentiment ;
79
79
console . log ( `Document sentiment:` ) ;
80
80
console . log ( ` Score: ${ sentiment . score } ` ) ;
81
81
console . log ( ` Magnitude: ${ sentiment . magnitude } ` ) ;
82
82
83
- const sentences = results [ 1 ] . sentences ;
83
+ const sentences = results [ 0 ] . sentences ;
84
84
sentences . forEach ( ( sentence ) => {
85
85
console . log ( `Sentence: ${ sentence . text . content } ` ) ;
86
86
console . log ( ` Score: ${ sentence . sentiment . score } ` ) ;
@@ -105,12 +105,15 @@ function analyzeEntitiesOfText (text) {
105
105
// const text = 'Hello, world!';
106
106
107
107
// Instantiates a Document, representing the provided text
108
- const document = language . document ( { content : text } ) ;
108
+ const document = {
109
+ 'content' : text ,
110
+ type : 'PLAIN_TEXT'
111
+ } ;
109
112
110
113
// Detects entities in the document
111
- document . detectEntities ( )
114
+ language . analyzeEntities ( { document : document } )
112
115
. then ( ( results ) => {
113
- const entities = results [ 1 ] . entities ;
116
+ const entities = results [ 0 ] . entities ;
114
117
115
118
console . log ( 'Entities:' ) ;
116
119
entities . forEach ( ( entity ) => {
@@ -131,11 +134,9 @@ function analyzeEntitiesInFile (bucketName, fileName) {
131
134
// [START language_entities_file]
132
135
// Imports the Google Cloud client libraries
133
136
const Language = require ( '@google-cloud/language' ) ;
134
- const Storage = require ( '@google-cloud/storage' ) ;
135
137
136
138
// Instantiates the clients
137
139
const language = Language ( ) ;
138
- const storage = Storage ( ) ;
139
140
140
141
// The name of the bucket where the file resides, e.g. "my-bucket"
141
142
// const bucketName = 'my-bucket';
@@ -144,15 +145,15 @@ function analyzeEntitiesInFile (bucketName, fileName) {
144
145
// const fileName = 'file.txt';
145
146
146
147
// Instantiates a Document, representing a text file in Cloud Storage
147
- const document = language . document ( {
148
- // The Google Cloud Storage file
149
- content : storage . bucket ( bucketName ) . file ( fileName )
150
- } ) ;
148
+ const document = {
149
+ gcsContentUri : `gs:// ${ bucketName } / ${ fileName } ` ,
150
+ type : 'PLAIN_TEXT'
151
+ } ;
151
152
152
153
// Detects entities in the document
153
- document . detectEntities ( )
154
+ language . analyzeEntities ( { document : document } )
154
155
. then ( ( results ) => {
155
- const entities = results [ 0 ] ;
156
+ const entities = results [ 0 ] . entities ;
156
157
157
158
console . log ( 'Entities:' ) ;
158
159
entities . forEach ( ( entity ) => {
@@ -181,15 +182,18 @@ function analyzeSyntaxOfText (text) {
181
182
// const text = 'Hello, world!';
182
183
183
184
// Instantiates a Document, representing the provided text
184
- const document = language . document ( { content : text } ) ;
185
+ const document = {
186
+ 'content' : text ,
187
+ type : 'PLAIN_TEXT'
188
+ } ;
185
189
186
190
// Detects syntax in the document
187
- document . detectSyntax ( )
191
+ language . analyzeSyntax ( { document : document } )
188
192
. then ( ( results ) => {
189
193
const syntax = results [ 0 ] ;
190
194
191
- console . log ( 'Parts of speech :' ) ;
192
- syntax . forEach ( ( part ) => {
195
+ console . log ( 'Tokens :' ) ;
196
+ syntax . tokens . forEach ( ( part ) => {
193
197
console . log ( `${ part . partOfSpeech . tag } : ${ part . text . content } ` ) ;
194
198
console . log ( `Morphology:` , part . partOfSpeech ) ;
195
199
} ) ;
@@ -204,11 +208,9 @@ function analyzeSyntaxInFile (bucketName, fileName) {
204
208
// [START language_syntax_file]
205
209
// Imports the Google Cloud client libraries
206
210
const Language = require ( '@google-cloud/language' ) ;
207
- const Storage = require ( '@google-cloud/storage' ) ;
208
211
209
212
// Instantiates the clients
210
213
const language = Language ( ) ;
211
- const storage = Storage ( ) ;
212
214
213
215
// The name of the bucket where the file resides, e.g. "my-bucket"
214
216
// const bucketName = 'my-bucket';
@@ -217,18 +219,18 @@ function analyzeSyntaxInFile (bucketName, fileName) {
217
219
// const fileName = 'file.txt';
218
220
219
221
// Instantiates a Document, representing a text file in Cloud Storage
220
- const document = language . document ( {
221
- // The Google Cloud Storage file
222
- content : storage . bucket ( bucketName ) . file ( fileName )
223
- } ) ;
222
+ const document = {
223
+ gcsContentUri : `gs:// ${ bucketName } / ${ fileName } ` ,
224
+ type : 'PLAIN_TEXT'
225
+ } ;
224
226
225
227
// Detects syntax in the document
226
- document . detectSyntax ( )
228
+ language . analyzeSyntax ( { document : document } )
227
229
. then ( ( results ) => {
228
230
const syntax = results [ 0 ] ;
229
231
230
232
console . log ( 'Parts of speech:' ) ;
231
- syntax . forEach ( ( part ) => {
233
+ syntax . tokens . forEach ( ( part ) => {
232
234
console . log ( `${ part . partOfSpeech . tag } : ${ part . text . content } ` ) ;
233
235
console . log ( `Morphology:` , part . partOfSpeech ) ;
234
236
} ) ;
0 commit comments