Sentiment Analysis With Azure Cognitive Service using REST API
By: Senura Vihan Jayadeva
Tags: Sentiment Analysis, Azure Cognitive Service, Rest Api, Text Analytics, Senura Jayadeva

Hello guys, I’m Senura Jayadeva. In this article, I will introduce you to another azure cognitive service called Text Analytics.

Actually, what is Azure Cognitive Service? Azure Cognitive Services is a family of APIs and SDKs used to create intelligent applications by opening up capabilities of AI to everyone, including people who lack machine-learning expertise.

The Text Analytics service is a cloud-based service that provides advanced natural language processing over raw text for sentiment analysis, key phrase extraction, named entity recognition, and language detection.

Here we are going to focus on sentiment analysis. Let’s look at what is sentiment analysis means.

Sentiment analysis

Sentiment analysis (or opinion mining) is a natural language processing technique used to determine whether data is positive, negative, or neutral. Sentiment analysis is often performed on textual data to help businesses monitor brand and product sentiment in customer feedback, and understand customer needs.

Using sentiment analysis we can analyze automatically 1,000+ reviews about our product could help us to discover if customers are happy about our pricing plans and customer service .

I hope now you have a proper idea about the sentiment analysis.

Okay, let’s see how to use this Azure Cognitive Service.

The first thing you have to do is logging in to the Azure portal. There you will be able to see a lot of azure services. Now click on the Create a resource option.

Then in the search Bar type Text Analytics and select it. That is the azure cognitive service that we are going to use in this article.

After that, you will be redirected to a new page that includes all the details about the service, and if you are satisfied with the pricing and service click on the create button.

Then you have to fill a form. Give a name for your service, select your subscription, location, pricing tier, and resource group. If you don't have any resource group you can easily create a resource group by click on that Create new button which is below the resource group text field.

If all the fields are filled click on the Create button.

It took some time to complete the deployment process.

After the deployment is completed click on the Go to resource.

Then you will be redirected to a page like below.

Here you can see they given all the documentation for each language. In this article, I’m going to test this API service with Postman Software. Postman is a great way to test APIs like this.

First, we have to identify the URL to call, HTTP method, headers, request body, and params we should pass.

Request URL :

https://{endpoint}/text/analytics/v3.1-preview.1/sentiment[?model-version][&showStats][&opinionMining][&stringIndexType]

Ex : https://eastus.api.cognitive.microsoft.com/text/analytics/v3.1-preview.1/sentiment

HTTP Request Method:

POST

Query parameters:

model-version

showStats

opinionMining

stringIndexType

Note : All these query parameters are optional

Headers:

Content-Type

Ocp-Apim-Subscription-Key

In the left sidebar under RESOURCE MANAGEMENT, you can see Keys and Endpoint. There you can find the Ocp-Apim-Subscription-Key.

Request body:

Request body for one document like this

{

"documents": [

{

"language": "en",

"id": "1",

"text": "Great atmosphere. Close to plenty of restaurants, hotels, and transit! Staff are not friendly and helpful."

}

]

}

Or we can analyze a collection of documents mentioned below.

{

"documents": [

{

"language": "en",

"id": "1",

"text": "Great atmosphere. Close to plenty of restaurants, hotels, and transit! Staff are friendly and helpful."

},

{

"language": "en",

"id": "2",

"text": "Bad atmosphere. Not close to plenty of restaurants, hotels, and transit! Staff are not friendly and helpful."

}

]

}

Okay now we know the URL to call, HTTP method, headers, request body, and params to use. Let’s try this on Postman.

Now click on the send button and now you will get a response like below

{

"documents": [{

"id": "1",

"sentiment": "mixed",

"confidenceScores": {

"positive": 0.5,

"neutral": 0.0,

"negative": 0.5

},

"sentences": [{

"sentiment": "positive",

"confidenceScores": {

"positive": 1.0,

"neutral": 0.0,

"negative": 0.0

},

"offset": 0,

"length": 17,

"text": "Great atmosphere.",

"aspects": [{

"sentiment": "positive",

"confidenceScores": {

"positive": 1.0,

"negative": 0.0

},

"offset": 6,

"length": 10,

"text": "atmosphere",

"relations": [{

"relationType": "opinion",

"ref": "#/documents/0/sentences/0/opinions/0"

}]

}],

"opinions": [{

"sentiment": "positive",

"confidenceScores": {

"positive": 1.0,

"negative": 0.0

},

"offset": 0,

"length": 5,

"text": "great",

"isNegated": false

}]

}, {

"sentiment": "neutral",

"confidenceScores": {

"positive": 0.13,

"neutral": 0.86,

"negative": 0.01

},

"offset": 18,

"length": 52,

"text": "Close to plenty of restaurants, hotels, and transit!",

"aspects": [{

"sentiment": "positive",

"confidenceScores": {

"positive": 0.99,

"negative": 0.01

},

"offset": 37,

"length": 11,

"text": "restaurants",

"relations": [{

"relationType": "opinion",

"ref": "#/documents/0/sentences/1/opinions/0"

}]

}, {

"sentiment": "positive",

"confidenceScores": {

"positive": 0.99,

"negative": 0.01

},

"offset": 50,

"length": 6,

"text": "hotels",

"relations": [{

"relationType": "opinion",

"ref": "#/documents/0/sentences/1/opinions/0"

}]

}],

"opinions": [{

"sentiment": "positive",

"confidenceScores": {

"positive": 0.99,

"negative": 0.01

},

"offset": 18,

"length": 15,

"text": "close to plenty",

"isNegated": false

}]

}, {

"sentiment": "negative",

"confidenceScores": {

"positive": 0.0,

"neutral": 0.0,

"negative": 1.0

},

"offset": 71,

"length": 35,

"text": "Staff are not friendly and helpful.",

"aspects": [{

"sentiment": "negative",

"confidenceScores": {

"positive": 0.0,

"negative": 1.0

},

"offset": 71,

"length": 5,

"text": "Staff",

"relations": [{

"relationType": "opinion",

"ref": "#/documents/0/sentences/2/opinions/0"

}, {

"relationType": "opinion",

"ref": "#/documents/0/sentences/2/opinions/1"

}]

}],

"opinions": [{

"sentiment": "negative",

"confidenceScores": {

"positive": 0.0,

"negative": 1.0

},

"offset": 85,

"length": 8,

"text": "friendly",

"isNegated": true

}, {

"sentiment": "negative",

"confidenceScores": {

"positive": 0.0,

"negative": 1.0

},

"offset": 98,

"length": 7,

"text": "helpful",

"isNegated": true

}]

}],

"warnings": []

}],

"errors": [],

"modelVersion": "2020-04-01"

}

“Great atmosphere. Close to plenty of restaurants, hotels, and transit! Staff are not friendly and helpful.”

When we look at the response, we can see Azure API analyzed our whole text and it gives the positivity and negativity about the atmosphere, restaurants, and Staff.

Here you can see the atmosphere has positive feedback, restaurants and hotels have positive feedback, and Staff have negative feedback.

So now you can understand how important this Azure API for a developer. Especially when implementing a system to analyze customer feedback. Not only that but also you can do lots of things using this service.

I hope now you have a good idea about Text Analytics Cognitive Service. As I mentioned above in this article I focused only on Sentiment Analytics. Other than that you have a lot of options like Detect Language, Entities containing personal information, Key Phrases, and Named Entity Recognition.

you can implement this Azure API for your React, Angular, Java, c# projects and etc.

I hope you learn something about this azure cognitive service. Thank you!