Detect sentiment with Text Analytics

2019-02-25 | Toni Pohl

Azure Cognitive Services offer a bunch of services. One is the Sentiment Analysis API that allows to analyze unstructured text for tasks such as sentiment analysis based on Microsoft machine learning algorithms. See the first steps how to use that here.

The test works in the same way with Postman as in the previous article.The goal here is to analyze some text to get a positive or negative result of the meaning.

So, we just need some Azure logic and a tool to send and get the data. How to detect sentiment with Text Analytics informs about the service we want to consume. Again, we need to create a new Cognitive Service and to get our own access data from the Azure Portal. Once created, we get that data in the service overview and in the keys menu.

image


Then, calling the Computer Vision API is easy: We need a HTTP POST request against the API and modify just the region and the subscription key.

https://<region>.api.cognitive.microsoft.com/analytics/v2.0/sentiment?subscription-key=<subscription-key>

So, my sample starts with https://westeurope.api.cognitive.microsoft.com/analytics/v2.0/sentiment…

In Postman, we just create a new POST request with that URL and some parameters as here:

image

As Headers we add a key "Content-Type" with value "application/json" and the body text that shall be uploaded with that post.

image

As defined, the data we want to submit can be formatted JSON type, as here. First, there's a positive, then there's a negative text, in english language.

{
         "documents": [
             {
                 "language": "en",
                 "id": "1",
                 "text": "We love this trail and make the trip every year. The views are breathtaking and well worth the hike!"
             },
             {
                 "language": "en",
                 "id": "2",
                 "text": "Poorly marked trails! I thought we were goners. Worst hike ever."
             }
         ]
}

See Language and region support for the Text Analytics API for more info.

When clicking on the "Send" button, the request should be sent to the API. After some seconds of analyzing, the result will be shown as body, similar as here. Here's the output in JSON format.

{
     "documents": [
         {
             "id": "1",
             "score": 0.92014169692993164
         },
         {
             "id": "2",
             "score": 0.05087512731552124
         }
     ],
     "errors": []
}

The return data says, that the first text with id 1 is positive (92%), the second text with id 2 is very negative (5%). A neutral value would be 0.5 (50%).

Happy analyzing your text with the Sentiment Analysis API!

Categories: AI, Azure, Cloud, Cognitive Services, Developer, English, Microsoft, Tools

Source: https://blog.atwork.at/post/2019/02/25/Detect-sentiment-with-Text-Analytics