Just a week ago, it was announced that Azure Text Analytics API has added 16 more languages that can be parsed for sentiment analysis, Greek language being one of them (currently in preview). So, I thought I could give it a try to see how well it’s working with some random Greek phrases. Well, I have to say that the outcome was pretty neat!
If you want to give Azure Analytics Text API a shot for yourself, check here for an easy way to test it via a simple browser demo. Otherwise, if you want to get your hands dirty and write some code, check here for the quick-start process to get it working. Basically, you will need to navigate to the Azure portal, get a text analytics key (you can use the free tier just to get started) and then run a simple piece of code for a POST request to the text analytics sentiment endpoint. Free tier allows you to do 5000 transactions/month which is really convenient. Moreover, do not forget to check here for the detailed API documentation of the sentiment analysis API.
So, let’s cut to the chase! Here is a Node.js script to call the sentiment analysis API for a simple JSON file
This file contains 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); | |
} | |
}); |
As sample data, we will use the following JSON file. This file contains real data from ParkAround’s airport parking lot reviews.
This file contains 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
[ | |
{ | |
"id": "1", | |
"text": "Δυσκολεύομαι να πιστέψω ότι υπάρχουν πράγματα που λειτουργούν τέλεια στην Ελλάδα. Ευχαριστώ και συγχαρητήρια για την προσπάθειά σας!" | |
}, | |
{ | |
"id": "2", | |
"text": "Όλα καλά. Ευχαριστώ πολύ!!" | |
}, | |
{ | |
"id": "3", | |
"text": "Πολύ καλή εξυπηρέτηση. Άμεσοι, φιλικοί και πολύ πρόθυμοι Εξαιρετική επιλογή και σε πολύ καλή τιμή!" | |
}, | |
{ | |
"id": "4", | |
"text": "Για την πληρωμή έπρεπε να πάω στο κεντρικό ταμείο και όχι στα αυτόματα μηχανήματα πληρωμών, όπου θα πλήρωνα μεγαλύτερο ποσό όπως με ενημέρωσε η ταμίας." | |
}, | |
{ | |
"id": "5", | |
"text": "Καλη τοποθεσία, Γρηγόρη εξυπηρέτηση" | |
}, | |
{ | |
"id": "6", | |
"text": "Κατά την ώρα της πληρωμής, δεν μπορούσαν να βρουν τα στοιχεία κράτησης online." | |
}, | |
{ | |
"id": "7", | |
"text": "Ο υπάλληλος δεν ήταν ενημερωμένος για την κράτησή μου." | |
}, | |
{ | |
"id": "8", | |
"text": "Υπήρξε αργοπορία κατά την παραλαβή του αυτοκινήτου μου κατά 45λεπτα" | |
} | |
] |
Output from the Node.js script will be
- Δυσκολεύομαι να πιστέψω ότι υπάρχουν πράγματα που λειτουργούν τέλεια στην Ελλάδα. Ευχαριστώ και συγχαρητήρια για την προσπάθειά σας! has a sentiment of 0.99655755
- Όλα καλά. Ευχαριστώ πολύ!! has a sentiment of 0.9814104
- Πολύ καλή εξυπηρέτηση. Άμεσοι, φιλικοί και πολύ πρόθυμοι Εξαιρετική επιλογή και σε πολύ καλή τιμή! has a sentiment of 1
- Για την πληρωμή έπρεπε να πάω στο κεντρικό ταμείο και όχι στα αυτόματα μηχανήματα πληρωμών, όπου θα πλήρωνα μεγαλύτερο ποσό όπως με
ενημέρωσε η ταμίας. has a sentiment of 0.17022656000000003 - Καλη τοποθεσία, Γρηγόρη εξυπηρέτηση has a sentiment of 0.65071435
Κατά την ώρα της πληρωμής, δεν μπορούσαν να βρουν τα στοιχεία κράτησης online. has a sentiment of 0.38731345 - Ο υπάλληλος δεν ήταν ενημερωμένος για την κράτησή μου. has a sentiment of 0.36413165000000003
- Υπήρξε αργοπορία κατά την παραλαβή του αυτοκινήτου μου κατά 45λεπτα has a sentiment of 0.451555805
(Scores close to 1 indicate positive sentiment and scores close to 0 indicate negative sentiment).
If you know Greek you can see that results are quite good for correctly spelled sentences (if you don’t know Greek, you can take my word for it!). Worth mentioning is the fact that you can submit more phrases (“documents”), up to 1000 ones with a maximum total input size of 1 MB. If you have any feedback for the team, you can leave it here.