Last active
April 21, 2018 20:12
-
-
Save dgkanatsios/58c21a755521fdde880884b4fbf9374c to your computer and use it in GitHub Desktop.
Sentiment analysis via Azure Text Analytics API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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