| 296 | } |
| 297 | |
| 298 | async function classifyTextOfText(text) { |
| 299 | // [START language_classify_text] |
| 300 | // Imports the Google Cloud client library |
| 301 | const language = require('@google-cloud/language'); |
| 302 | |
| 303 | // Creates a client |
| 304 | const client = new language.LanguageServiceClient(); |
| 305 | |
| 306 | /** |
| 307 | * TODO(developer): Uncomment the following line to run this code. |
| 308 | */ |
| 309 | // const text = 'Your text to analyze, e.g. Hello, world!'; |
| 310 | |
| 311 | // Prepares a document, representing the provided text |
| 312 | const document = { |
| 313 | content: text, |
| 314 | type: 'PLAIN_TEXT', |
| 315 | }; |
| 316 | |
| 317 | const classificationModelOptions = { |
| 318 | v2Model: { |
| 319 | contentCategoriesVersion: 'V2', |
| 320 | }, |
| 321 | }; |
| 322 | |
| 323 | // Classifies text in the document |
| 324 | const [classification] = await client.classifyText({ |
| 325 | document, |
| 326 | classificationModelOptions, |
| 327 | }); |
| 328 | console.log('Categories:'); |
| 329 | classification.categories.forEach(category => { |
| 330 | console.log(`Name: ${category.name}, Confidence: ${category.confidence}`); |
| 331 | }); |
| 332 | // [END language_classify_text] |
| 333 | } |
| 334 | |
| 335 | async function classifyTextInFile(bucketName, fileName) { |
| 336 | // [START language_classify_gcs] |