@@ -33,7 +33,9 @@ function analyzeSentimentOfText (text) {
3333 return document . detectSentiment ( )
3434 . then ( ( results ) => {
3535 const sentiment = results [ 0 ] ;
36+
3637 console . log ( `Sentiment: ${ sentiment >= 0 ? 'positive' : 'negative' } .` ) ;
38+
3739 return sentiment ;
3840 } ) ;
3941}
@@ -60,7 +62,9 @@ function analyzeSentimentInFile (bucketName, fileName) {
6062 return document . detectSentiment ( )
6163 . then ( ( results ) => {
6264 const sentiment = results [ 0 ] ;
65+
6366 console . log ( `Sentiment: ${ sentiment >= 0 ? 'positive' : 'negative' } .` ) ;
67+
6468 return sentiment ;
6569 } ) ;
6670}
@@ -81,10 +85,12 @@ function analyzeEntitiesOfText (text) {
8185 return document . detectEntities ( )
8286 . then ( ( results ) => {
8387 const entities = results [ 0 ] ;
88+
8489 console . log ( 'Entities:' ) ;
8590 for ( let type in entities ) {
8691 console . log ( `${ type } :` , entities [ type ] ) ;
8792 }
93+
8894 return entities ;
8995 } ) ;
9096}
@@ -111,10 +117,12 @@ function analyzeEntitiesInFile (bucketName, fileName) {
111117 return document . detectEntities ( )
112118 . then ( ( results ) => {
113119 const entities = results [ 0 ] ;
120+
114121 console . log ( 'Entities:' ) ;
115122 for ( let type in entities ) {
116123 console . log ( `${ type } :` , entities [ type ] ) ;
117124 }
125+
118126 return entities ;
119127 } ) ;
120128}
@@ -135,8 +143,10 @@ function analyzeSyntaxOfText (text) {
135143 return document . detectSyntax ( )
136144 . then ( ( results ) => {
137145 const syntax = results [ 0 ] ;
146+
138147 console . log ( 'Tags:' ) ;
139148 syntax . forEach ( ( part ) => console . log ( part . tag ) ) ;
149+
140150 return syntax ;
141151 } ) ;
142152}
@@ -163,59 +173,62 @@ function analyzeSyntaxInFile (bucketName, fileName) {
163173 return document . detectSyntax ( )
164174 . then ( ( results ) => {
165175 const syntax = results [ 0 ] ;
176+
166177 console . log ( 'Tags:' ) ;
167178 syntax . forEach ( ( part ) => console . log ( part . tag ) ) ;
179+
168180 return syntax ;
169181 } ) ;
170182}
171183// [END language_syntax_file]
172184
173- // The command-line program
174- const cli = require ( `yargs` ) ;
175-
176- const program = module . exports = {
177- analyzeSentimentOfText,
178- analyzeSentimentInFile,
179- analyzeEntitiesOfText,
180- analyzeEntitiesInFile,
181- analyzeSyntaxOfText,
182- analyzeSyntaxInFile,
183- main : ( args ) => {
184- // Run the command-line program
185- cli . help ( ) . strict ( ) . parse ( args ) . argv ;
186- }
187- } ;
188-
189- cli
185+ require ( `yargs` )
190186 . demand ( 1 )
191- . command ( `sentimentOfText <text>` , `Detect the sentiment of a block of text.` , { } , ( opts ) => {
192- program . analyzeSentimentOfText ( opts . text ) ;
193- } )
194- . command ( `sentimentInFile <bucket> <filename>` , `Detect the sentiment of text in a GCS file.` , { } , ( opts ) => {
195- program . analyzeSentimentInFile ( opts . bucket , opts . filename ) ;
196- } )
197- . command ( `entitiesOfText <text>` , `Detect the entities of a block of text.` , { } , ( opts ) => {
198- program . analyzeEntitiesOfText ( opts . text ) ;
199- } )
200- . command ( `entitiesInFile <bucket> <filename>` , `Detect the entities of text in a GCS file.` , { } , ( opts ) => {
201- program . analyzeEntitiesInFile ( opts . bucket , opts . filename ) ;
202- } )
203- . command ( `syntaxOfText <text>` , `Detect the syntax of a block of text.` , { } , ( opts ) => {
204- program . analyzeSyntaxOfText ( opts . text ) ;
205- } )
206- . command ( `syntaxInFile <bucket> <filename>` , `Detect the syntax of text in a GCS file.` , { } , ( opts ) => {
207- program . analyzeSyntaxInFile ( opts . bucket , opts . filename ) ;
208- } )
209- . example ( `node $0 sentimentOfText "President Obama is speaking at the White House."` , `` )
210- . example ( `node $0 sentimentInFile my-bucket file.txt` , `` )
211- . example ( `node $0 entitiesOfText "President Obama is speaking at the White House."` , `` )
212- . example ( `node $0 entitiesInFile my-bucket file.txt` , `` )
213- . example ( `node $0 syntaxOfText "President Obama is speaking at the White House."` , `` )
214- . example ( `node $0 syntaxInFile my-bucket file.txt` , `` )
187+ . command (
188+ `sentiment-text <text>` ,
189+ `Detects sentiment of a string.` ,
190+ { } ,
191+ ( opts ) => analyzeSentimentOfText ( opts . text )
192+ )
193+ . command (
194+ `sentiment-file <bucket> <filename>` ,
195+ `Detects sentiment in a file in Google Cloud Storage.` ,
196+ { } ,
197+ ( opts ) => analyzeSentimentInFile ( opts . bucket , opts . filename )
198+ )
199+ . command (
200+ `entities-text <text>` ,
201+ `Detects entities in a string.` ,
202+ { } ,
203+ ( opts ) => analyzeEntitiesOfText ( opts . text )
204+ )
205+ . command (
206+ `entities-file <bucket> <filename>` ,
207+ `Detects entities in a file in Google Cloud Storage.` ,
208+ { } ,
209+ ( opts ) => analyzeEntitiesInFile ( opts . bucket , opts . filename )
210+ )
211+ . command (
212+ `syntax-text <text>` ,
213+ `Detects syntax of a string.` ,
214+ { } ,
215+ ( opts ) => analyzeSyntaxOfText ( opts . text )
216+ )
217+ . command (
218+ `syntax-file <bucket> <filename>` ,
219+ `Detects syntax in a file in Google Cloud Storage.` ,
220+ { } ,
221+ ( opts ) => analyzeSyntaxInFile ( opts . bucket , opts . filename )
222+ )
223+ . example ( `node $0 sentiment-text "President Obama is speaking at the White House."` )
224+ . example ( `node $0 sentiment-file my-bucket file.txt` , `Detects sentiment in gs://my-bucket/file.txt` )
225+ . example ( `node $0 entities-text "President Obama is speaking at the White House."` )
226+ . example ( `node $0 entities-file my-bucket file.txt` , `Detects entities in gs://my-bucket/file.txt` )
227+ . example ( `node $0 syntax-text "President Obama is speaking at the White House."` )
228+ . example ( `node $0 syntax-file my-bucket file.txt` , `Detects syntax in gs://my-bucket/file.txt` )
215229 . wrap ( 120 )
216230 . recommendCommands ( )
217- . epilogue ( `For more information, see https://cloud.google.com/natural-language/docs` ) ;
218-
219- if ( module === require . main ) {
220- program . main ( process . argv . slice ( 2 ) ) ;
221- }
231+ . epilogue ( `For more information, see https://cloud.google.com/natural-language/docs` )
232+ . help ( )
233+ . strict ( )
234+ . argv ;
0 commit comments