Skip to content

Instantly share code, notes, and snippets.

@jgwill
Created December 7, 2018 13:02
Show Gist options
  • Save jgwill/74dda2287c58eb813ff65df8f08c786c to your computer and use it in GitHub Desktop.
Save jgwill/74dda2287c58eb813ff65df8f08c786c to your computer and use it in GitHub Desktop.
@stcgoal Learn the sentiment of a string using Google Cloud Language API
// Imports the Google Cloud client library
const language = require('@google-cloud/language');
// Instantiates a client
const client = new language.LanguageServiceClient();
// The text to analyze
const text = 'Afelia stopped working and is crashed!';
const document = {
content: text,
type: 'PLAIN_TEXT',
};
// Detects the sentiment of the text
client
.analyzeSentiment({ document: document })
.then(results => {
const sentiment = results[0].documentSentiment;
console.log(`Text: ${text}`);
console.log(`Sentiment score: ${sentiment.score}`);
console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
})
.catch(err => {
console.error('ERROR:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment