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