Skip to content

Commit 54149c1

Browse files
Add the classifyText method to language (v1). (#22)
1 parent c9ce0d0 commit 54149c1

File tree

3 files changed

+163
-66
lines changed

3 files changed

+163
-66
lines changed

cloud-language/snippets/analyze.v1.js

+117-32
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
function analyzeSentimentOfText(text) {
1919
// [START language_sentiment_string]
2020
// Imports the Google Cloud client library
21-
const LanguageServiceClient = require('@google-cloud/language')
22-
.LanguageServiceClient;
21+
const language = require('@google-cloud/language');
2322

2423
// Creates a client
25-
const language = new LanguageServiceClient();
24+
const client = new language.LanguageServiceClient();
2625

2726
/**
2827
* TODO(developer): Uncomment the following line to run this code.
@@ -36,7 +35,7 @@ function analyzeSentimentOfText(text) {
3635
};
3736

3837
// Detects the sentiment of the document
39-
language
38+
client
4039
.analyzeSentiment({document: document})
4140
.then(results => {
4241
const sentiment = results[0].documentSentiment;
@@ -60,11 +59,10 @@ function analyzeSentimentOfText(text) {
6059
function analyzeSentimentInFile(bucketName, fileName) {
6160
// [START language_sentiment_file]
6261
// Imports the Google Cloud client library
63-
const LanguageServiceClient = require('@google-cloud/language')
64-
.LanguageServiceClient;
62+
const language = require('@google-cloud/language');
6563

6664
// Creates a client
67-
const language = new LanguageServiceClient();
65+
const client = new language.LanguageServiceClient();
6866

6967
/**
7068
* TODO(developer): Uncomment the following lines to run this code
@@ -79,7 +77,7 @@ function analyzeSentimentInFile(bucketName, fileName) {
7977
};
8078

8179
// Detects the sentiment of the document
82-
language
80+
client
8381
.analyzeSentiment({document: document})
8482
.then(results => {
8583
const sentiment = results[0].documentSentiment;
@@ -103,11 +101,10 @@ function analyzeSentimentInFile(bucketName, fileName) {
103101
function analyzeEntitiesOfText(text) {
104102
// [START language_entities_string]
105103
// Imports the Google Cloud client library
106-
const LanguageServiceClient = require('@google-cloud/language')
107-
.LanguageServiceClient;
104+
const language = require('@google-cloud/language');
108105

109106
// Creates a client
110-
const language = new LanguageServiceClient();
107+
const client = new language.LanguageServiceClient();
111108

112109
/**
113110
* TODO(developer): Uncomment the following line to run this code.
@@ -121,7 +118,7 @@ function analyzeEntitiesOfText(text) {
121118
};
122119

123120
// Detects entities in the document
124-
language
121+
client
125122
.analyzeEntities({document: document})
126123
.then(results => {
127124
const entities = results[0].entities;
@@ -144,11 +141,10 @@ function analyzeEntitiesOfText(text) {
144141
function analyzeEntitiesInFile(bucketName, fileName) {
145142
// [START language_entities_file]
146143
// Imports the Google Cloud client library
147-
const LanguageServiceClient = require('@google-cloud/language')
148-
.LanguageServiceClient;
144+
const language = require('@google-cloud/language');
149145

150146
// Creates a client
151-
const language = new LanguageServiceClient();
147+
const client = new language.LanguageServiceClient();
152148

153149
/**
154150
* TODO(developer): Uncomment the following lines to run this code
@@ -163,7 +159,7 @@ function analyzeEntitiesInFile(bucketName, fileName) {
163159
};
164160

165161
// Detects entities in the document
166-
language
162+
client
167163
.analyzeEntities({document: document})
168164
.then(results => {
169165
const entities = results[0].entities;
@@ -186,11 +182,10 @@ function analyzeEntitiesInFile(bucketName, fileName) {
186182
function analyzeSyntaxOfText(text) {
187183
// [START language_syntax_string]
188184
// Imports the Google Cloud client library
189-
const LanguageServiceClient = require('@google-cloud/language')
190-
.LanguageServiceClient;
185+
const language = require('@google-cloud/language');
191186

192187
// Creates a client
193-
const language = new LanguageServiceClient();
188+
const client = new language.LanguageServiceClient();
194189

195190
/**
196191
* TODO(developer): Uncomment the following line to run this code.
@@ -204,7 +199,7 @@ function analyzeSyntaxOfText(text) {
204199
};
205200

206201
// Detects syntax in the document
207-
language
202+
client
208203
.analyzeSyntax({document: document})
209204
.then(results => {
210205
const syntax = results[0];
@@ -224,11 +219,10 @@ function analyzeSyntaxOfText(text) {
224219
function analyzeSyntaxInFile(bucketName, fileName) {
225220
// [START language_syntax_file]
226221
// Imports the Google Cloud client library
227-
const LanguageServiceClient = require('@google-cloud/language')
228-
.LanguageServiceClient;
222+
const language = require('@google-cloud/language');
229223

230224
// Creates a client
231-
const language = new LanguageServiceClient();
225+
const client = new language.LanguageServiceClient();
232226

233227
/**
234228
* TODO(developer): Uncomment the following lines to run this code
@@ -243,7 +237,7 @@ function analyzeSyntaxInFile(bucketName, fileName) {
243237
};
244238

245239
// Detects syntax in the document
246-
language
240+
client
247241
.analyzeSyntax({document: document})
248242
.then(results => {
249243
const syntax = results[0];
@@ -263,11 +257,10 @@ function analyzeSyntaxInFile(bucketName, fileName) {
263257
function analyzeEntitySentimentOfText(text) {
264258
// [START language_entity_sentiment_string]
265259
// Imports the Google Cloud client library
266-
const LanguageServiceClient = require('@google-cloud/language')
267-
.LanguageServiceClient;
260+
const language = require('@google-cloud/language');
268261

269262
// Creates a client
270-
const language = new LanguageServiceClient();
263+
const client = new language.LanguageServiceClient();
271264

272265
/**
273266
* TODO(developer): Uncomment the following line to run this code.
@@ -281,7 +274,7 @@ function analyzeEntitySentimentOfText(text) {
281274
};
282275

283276
// Detects sentiment of entities in the document
284-
language
277+
client
285278
.analyzeEntitySentiment({document: document})
286279
.then(results => {
287280
const entities = results[0].entities;
@@ -303,11 +296,10 @@ function analyzeEntitySentimentOfText(text) {
303296
function analyzeEntitySentimentInFile(bucketName, fileName) {
304297
// [START language_entity_sentiment_file]
305298
// Imports the Google Cloud client library
306-
const LanguageServiceClient = require('@google-cloud/language')
307-
.LanguageServiceClient;
299+
const language = require('@google-cloud/language');
308300

309301
// Creates a client
310-
const language = new LanguageServiceClient();
302+
const client = new language.LanguageServiceClient();
311303

312304
/**
313305
* TODO(developer): Uncomment the following lines to run this code
@@ -322,7 +314,7 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
322314
};
323315

324316
// Detects sentiment of entities in the document
325-
language
317+
client
326318
.analyzeEntitySentiment({document: document})
327319
.then(results => {
328320
const entities = results[0].entities;
@@ -341,6 +333,83 @@ function analyzeEntitySentimentInFile(bucketName, fileName) {
341333
// [END language_entity_sentiment_file]
342334
}
343335

336+
function classifyTextOfText(text) {
337+
// [START language_classify_string]
338+
// Imports the Google Cloud client library
339+
const language = require('@google-cloud/language');
340+
341+
// Creates a client
342+
const client = new language.LanguageServiceClient();
343+
344+
/**
345+
* TODO(developer): Uncomment the following line to run this code.
346+
*/
347+
// const text = 'Your text to analyze, e.g. Hello, world!';
348+
349+
// Prepares a document, representing the provided text
350+
const document = {
351+
content: text,
352+
type: 'PLAIN_TEXT',
353+
};
354+
355+
// Classifies text in the document
356+
client
357+
.classifyText({document: document})
358+
.then(results => {
359+
const classification = results[0];
360+
361+
console.log('Categories:');
362+
classification.categories.forEach(category => {
363+
console.log(
364+
`Name: ${category.name}, Confidence: ${category.confidence}`
365+
);
366+
});
367+
})
368+
.catch(err => {
369+
console.error('ERROR:', err);
370+
});
371+
// [END language_classify_string]
372+
}
373+
374+
function classifyTextInFile(bucketName, fileName) {
375+
// [START language_classify_file]
376+
// Imports the Google Cloud client library.
377+
const language = require('@google-cloud/language');
378+
379+
// Creates a client.
380+
const client = new language.LanguageServiceClient();
381+
382+
/**
383+
* TODO(developer): Uncomment the following lines to run this code
384+
*/
385+
// const bucketName = 'Your bucket name, e.g. my-bucket';
386+
// const fileName = 'Your file name, e.g. my-file.txt';
387+
388+
// Prepares a document, representing a text file in Cloud Storage
389+
const document = {
390+
gcsContentUri: `gs://${bucketName}/${fileName}`,
391+
type: 'PLAIN_TEXT',
392+
};
393+
394+
// Classifies text in the document
395+
client
396+
.classifyText({document: document})
397+
.then(results => {
398+
const classification = results[0];
399+
400+
console.log('Categories:');
401+
classification.categories.forEach(category => {
402+
console.log(
403+
`Name: ${category.name}, Confidence: ${category.confidence}`
404+
);
405+
});
406+
})
407+
.catch(err => {
408+
console.error('ERROR:', err);
409+
});
410+
// [END language_classify_file]
411+
}
412+
344413
require(`yargs`)
345414
.demand(1)
346415
.command(
@@ -385,6 +454,15 @@ require(`yargs`)
385454
{},
386455
opts => analyzeEntitySentimentInFile(opts.bucketName, opts.fileName)
387456
)
457+
.command(`classify-text <text>`, `Classifies text of a string.`, {}, opts =>
458+
classifyTextOfText(opts.text)
459+
)
460+
.command(
461+
`classify-file <bucketName> <fileName>`,
462+
`Classifies text in a file in Google Cloud Storage.`,
463+
{},
464+
opts => classifyTextInFile(opts.bucketName, opts.fileName)
465+
)
388466
.example(
389467
`node $0 sentiment-text "President Obama is speaking at the White House."`
390468
)
@@ -413,6 +491,13 @@ require(`yargs`)
413491
`node $0 entity-sentiment-file my-bucket file.txt`,
414492
`Detects sentiment of entities in gs://my-bucket/file.txt`
415493
)
494+
.example(
495+
`node $0 classify-text "Android is a mobile operating system developed by Google."`
496+
)
497+
.example(
498+
`node $0 classify-file my-bucket android_text.txt`,
499+
`Detects syntax in gs://my-bucket/android_text.txt`
500+
)
416501
.wrap(120)
417502
.recommendCommands()
418503
.epilogue(

0 commit comments

Comments
 (0)