Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.