Many moons ago when I was studying towards my Computer Science degree, the term ‘Netiquette’ was used to define an acceptable way of communicating over the internet, from email to Instant Messaging and so on. It was common place that if you used ALL CAPITALS in an email , then it was the equivalent of SHOUTING at someone! But this term also covered the basics of how you should converse on the internet.
We know that the written text form of communication can sometimes be less helpful in determining ‘intent’ or ‘context’ and therefore the reader can assume their own bias and potentially take a mundane sentence and read into it a sentiment that was not meant. Over the years various other ways have come about to aid this for of communication from formal to less formal utilising ’emojis’ or even animated GIF images to convey emotion and sentiment.
Fast forward to todays world and with AI we have the ability to use trained AI models to detect sentiment in text and this is what I am going to explore as part of this article, and maybe look at how this technology can be utilised in our applications. I am going to focus on sentiment analysis by utilising the Text Analytics API which is a cloud-based service from Microsoft that provides some advanced Natural Language Processing (NLP) over text. We will look at this feature and how we can the testing console to start to look at how we could use this API in our applications.
What is Sentiment Analysis?
In many situations in our daily working or personal lives we are asked for feedback, or we may want to foster feedback for something we have done, a product, a service etc. We have all seen the Sad, Neutral, and happy face buttons in airports and other business premises asking for us to select our feelings towards a service. If we take that idea and focus on when we are looking to deduce sentiment from say a product feedback email, where customers have written a review or given direct feedback in text form, wouldn’t it be nice if we could detect and arrange feedback into groups based on sentiment. Ranging from Happy to Disappointed and all the various feelings in between.
One way of utlising sentiment analysis is to train a machine learning model to detect it. This can be a long and complicated process that would require a lot of data that has been labelled and classified, and would no doubt require retraining as language evolves over time and words morph to have different meanings, for example describing someone as ‘wicked’ could mean that they are bad, but could also mean that they are awesome based on the context and potentially the age of the person using the term. With this being the case, Microsoft and many other companies invest in research in tthis area to create and expose a set of API’s and SDK’s for developers to be able to hook into the services without the need to define and prepare such training materials.
Microsoft has Cognitive Services which is one such offering.
Text Analytics API
As part of the Microsoft Cognitive Services, the Text Analytics API is there to help us extract information from text. We can do things like, detect what language a piece of text is written in, extract any key phrases, detect entities or in our case, discover sentiment.
Sentiment analysis using the Text Analytics API utilises a machine learning classification algorithmic that generates a sentiment score and a value between 0 and 1. The closer to 1 means the more ‘positive’ the sentiment is and the closer to 0 means the more ‘negative’, somewhere around 0.5 means its a neutral response.
The behind the scenes make up of the algorithm isn’t something we need to be concerned with as we can hook into the API to parse some text and get a JSON formatted response with our sentiment score.
Calling the Text Analytics API
The first thing we need to do is login to our Azure Portal and create an access key for use:


Search for Text Analytics and Click Create

For our purposes, we won’t select any preview features and we will Continue to Create your resource

Next we can fill in some basic information about our resource and click on Review and Create

Click Create


Under ‘Resource Management‘ Click on Keys and Endpoint, here you will find the Key that you can copy for use later in this article.
Calling the API from the testing console
Microsoft have provided a test portal for us to take our newly created Text Analytics API resource for a spin!
https://%5Blocation%5D.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0
Replace the {Location} tag with the location you selected for the account. In my Case I selected UK South. so lets browse to that location:
https://uksouth.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0


Select ‘Sentiment’ from the left hand menu

Copy in your Key that you saved from the previous step.
Lets make an API Call
OK So on the testing web page, we can format a request body to include three pieces of text for analysis:
Select
{
"documents": [
{
"language": "en",
"id": "1",
"text": "Hello I think this product is absolutely amazing"
},
{
"language": "en",
"id": "2",
"text": "Hi I think the product is so so but could be made better by removing that boring part"
},
{
"language": "en",
"id": "3",
"text": "This Product is TERRIBLE"
}
]
}

Click Send

Once the JSON formatted text has been sent, we can then see our response:

As you can see, you can use this utility to test for Language, Entity, Key Phase as well as Sentiment analysis.
What next?
Hopefully you can see how you can glean this sentiment analysis from text in a super quick way. The next steps would be to look to include code in your application, or maybe look to use an Azure Function to trigger each time a new piece of text is added to a queue, maybe you could sort our the feedback responses base don sentiment score or send for onward processing base don that score.
Either way, this quick demonstration was to show the initial set up and testing of the Text Analysis API within Azure Cognitive services, how you build this into your application is entirely up to you.
A quick note about billing costs of this service.
As you may have spotted, it comes in 2 tiers:

F0 – Free 5k messages over a 30 day period for each of the analytics services
S – Standard, a maximum of 1000 calls per minute costing £0.75GBP per 1000 text records.
I hope you have enjoyed reading this article.