Skip to content

Instantly share code, notes, and snippets.

@shinchit
Last active November 21, 2019 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/493a41c1420027688649cd5c6a66ca6d to your computer and use it in GitHub Desktop.
Save shinchit/493a41c1420027688649cd5c6a66ca6d to your computer and use it in GitHub Desktop.
const language = require('@google-cloud/language');
async function isPositive(text) {
// If you want to know detail of LanguageServiceClient,
// See also
// https://cloud.google.com/natural-language/docs/basics#interpreting_sentiment_analysis_values
const lsclient = new language.LanguageServiceClient();
const document = {
content: text,
type: 'PLAIN_TEXT',
};
// The value returned by the then function can be received in a variable with await.
let sentiment = await lsclient.analyzeSentiment({document})
.then(([result]) => {
const sentiment = result.documentSentiment;
return sentiment;
})
.catch((err) => {
throw err;
});
if (sentiment.score > 0.0) {
return true;
} else {
return false;
}
}
let positive = await isPositive('I love you');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment