MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

cloud-language/analyze_sentiment_v2_text.js:17–55  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(text) {
18 async function analyzeSentimentOfText() {
19 // [START language_sentiment_text]
20 // Imports the Google Cloud client library
21 const language = require('@google-cloud/language').v2;
22
23 // Creates a client
24 const client = new language.LanguageServiceClient();
25
26 /**
27 * TODO(developer): Uncomment the following line to run this code.
28 */
29 // const text = 'Your text to analyze, e.g. Hello, world!';
30
31 // Prepares a document, representing the provided text
32 const document = {
33 content: text,
34 type: 'PLAIN_TEXT',
35 };
36
37 // Detects the sentiment of the document
38 const [result] = await client.analyzeSentiment({document});
39
40 const sentiment = result.documentSentiment;
41 console.log('Document sentiment:');
42 console.log(` Score: ${sentiment.score}`);
43 console.log(` Magnitude: ${sentiment.magnitude}`);
44
45 const sentences = result.sentences;
46 sentences.forEach(sentence => {
47 console.log(`Sentence: ${sentence.text.content}`);
48 console.log(` Score: ${sentence.sentiment.score}`);
49 console.log(` Magnitude: ${sentence.sentiment.magnitude}`);
50 });
51
52 // [END language_sentiment_text]
53 }
54 await analyzeSentimentOfText();
55}
56
57main(...process.argv.slice(2)).catch(err => {
58 console.error(err);

Callers 1

Calls 1

analyzeSentimentOfTextFunction · 0.70

Tested by

no test coverage detected