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

Function main

cloud-language/analyze_sentiment_v2_file.js:17–55  ·  view source on GitHub ↗
(bucketName, fileName)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(bucketName, fileName) {
18 async function analyzeSentimentInFile() {
19 // [START language_sentiment_gcs]
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 lines to run this code
28 */
29 // const bucketName = 'Your bucket name, e.g. my-bucket';
30 // const fileName = 'Your file name, e.g. my-file.txt';
31
32 // Prepares a document, representing a text file in Cloud Storage
33 const document = {
34 gcsContentUri: `gs://${bucketName}/${fileName}`,
35 type: 'PLAIN_TEXT',
36 };
37
38 // Detects the sentiment of the document
39 const [result] = await client.analyzeSentiment({document});
40
41 const sentiment = result.documentSentiment;
42 console.log('Document sentiment:');
43 console.log(` Score: ${sentiment.score}`);
44 console.log(` Magnitude: ${sentiment.magnitude}`);
45
46 const sentences = result.sentences;
47 sentences.forEach(sentence => {
48 console.log(`Sentence: ${sentence.text.content}`);
49 console.log(` Score: ${sentence.sentiment.score}`);
50 console.log(` Magnitude: ${sentence.sentiment.magnitude}`);
51 });
52 // [END language_sentiment_gcs]
53 }
54 await analyzeSentimentInFile();
55}
56
57main(...process.argv.slice(2)).catch(err => {
58 console.error(err);

Callers 1

Calls 1

analyzeSentimentInFileFunction · 0.70

Tested by

no test coverage detected