Skip to content

Instantly share code, notes, and snippets.

@dgkanatsios
Last active April 21, 2018 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgkanatsios/58c21a755521fdde880884b4fbf9374c to your computer and use it in GitHub Desktop.
Save dgkanatsios/58c21a755521fdde880884b4fbf9374c to your computer and use it in GitHub Desktop.
Sentiment analysis via Azure Text Analytics API
const request = require('request');
const fs = require('fs');
const url = 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment';
const textanalyticskey = '<YOUR_TEXT_ANALYTICS_KEY>';
const data = JSON.parse(fs.readFileSync('test.json', 'utf8'));
request.post(url,
{
json: {
"documents": data.map(entry => ({ language: "el", id: entry.id, text: entry.text }))
}, "headers": { 'Ocp-Apim-Subscription-Key': textanalyticskey }
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
body.documents.forEach(document => {
console.log(`${data.filter(x=>x.id===document.id)[0].text} has a sentiment of ${document.score}`);
});
}
else {
console.log(error || body);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment