| 16 | |
| 17 | // [START language_quickstart] |
| 18 | async function quickstart() { |
| 19 | // Imports the Google Cloud client library |
| 20 | const language = require('@google-cloud/language'); |
| 21 | |
| 22 | // Instantiates a client |
| 23 | const client = new language.LanguageServiceClient(); |
| 24 | |
| 25 | // The text to analyze |
| 26 | const text = 'Hello, world!'; |
| 27 | |
| 28 | const document = { |
| 29 | content: text, |
| 30 | type: 'PLAIN_TEXT', |
| 31 | }; |
| 32 | |
| 33 | // Detects the sentiment of the text |
| 34 | const [result] = await client.analyzeSentiment({document: document}); |
| 35 | const sentiment = result.documentSentiment; |
| 36 | |
| 37 | console.log(`Text: ${text}`); |
| 38 | console.log(`Sentiment score: ${sentiment.score}`); |
| 39 | console.log(`Sentiment magnitude: ${sentiment.magnitude}`); |
| 40 | } |
| 41 | // [END language_quickstart] |
| 42 | |
| 43 | quickstart().catch(console.error); |