Categorize feedback based on text sentiment using Azure Functions

27 Jul, 2019 | 4 minutes read
Types of text analysis

We all want to have information about the feedback that customers leave on the website. Having information about what our customers think about our brand, our products and offers, is crucial for the business since it gives insights into how to make it better. Looking for a sentiment and making a sentiment analysis can answer some questions like “What do the customers want?”, “What can we improve?” etc. We want to categorize the feedback so we can apply our limited resources to the feedback that matters. If the customer is frustrated, we want to know immediately and find a solution about the issue. This can be done by a program – by using the Text Analytics API in Azure. It is about understanding and analyzing unstructured text. It also covers sentiment analysis, keyphrase extraction, language detection, and more.

Text Analytics API

Discover sentiment

The goal for using this API is to make applications more intelligent, engaging and discoverable. The service uses a machine learning classification algorithm to generate sentiment score between 0 and 1. Depending on the score, we can classify the text as positive, negative or neutral. When the sentiment score is near to 1, it means that the sentiment is positive, if it is near 0, we are encountering a negative sentiment. A score close to 0.5 indicates no sentiment or neutral statement.

For sorting the feedback, we can use Azure Functions and create a function for categorization the feedback.

The text document is placed into a queue that is named new-feedback in the diagram. The arrival of a new message containing the text document into the queue will trigger the function. The function reads the message from the input queue and sends it for analysis to the Text Analytics API. Based on the results that the API returns, the new message is placed into an output queue for further processing. The results we get back for each document is a sentiment score. As mentioned before, based on the score we define if the feedback is positive, negative or neutral.  One way for implementing this is using Azure Functions.

What is Azure Functions?

Azure Functions is a serverless compute service that enables running code on-demand without having to explicitly provide or manage infrastructure. It can be used to run a script or a piece of code in response to a variety of events. Functions is a great solution for processing data, integrating systems, working with the internet-of-things (IoT) and building simple APIs and microservices. With serverless, a user can simply create and upload code, then define the triggers or events that will execute the code. Triggers can come from a wide range of sources, including cloud services, databases, notification hubs, etc.

Once a trigger or event occurs, it is the cloud provider’s responsibility to load the code into a suitable execution environment, run the code and then release the computer resources. There are still servers involved, but the user no longer needs to provision compute instances. Azure Functions provides templates with key scenarios like:

  • HTTPTrigger – Trigger the execution of the code by using HTTP request;
  • TimerTrigger – Executes batch tasks on a predefined schedule;
  • BlobTrigger – Process Azure Storage blobs when they are added to containers;
  • QueueTriggers – Respond to message as they arrive in an Azure Storage queue;
  • ServiceBusQueueTrigger – Connects the code to other Azure services or on-premises services by listing to message queue.

Azure Functions does require an Azure Storage account that must support Azure Blob, Queue and Table storage services. Azure Functions uses Azure Storage to manage triggers and log function execution.

Call Text Analytics API from an app

For hosting the function, first we need to create a Function App. In this case, it is the best to create a Queue-triggered function for calling the Text Analytics API, so when a new message arrives in the queue, the function is called. In the function, for calling the API, HTTP module is used, where we pass the access key in the header of every request.


Based on the sentiment score from the response, the message is forwarded to one of the three output queues: positive feedback, negative feedback and neutral feedback.

Example of the responses for different kind of content:

Text 1: Today is a rainy and very depressive day

Text 2:  Great work done. Problem solved by customer care in just one day

Text 3: Although the services are good, the price is very high

When we get the sentiment score from the response, for further usage, we should set a score threshold. For better classification, it is recommended that for positive feedback the score needs to be higher than 0.8, as for negative feedback, the score should be lower than 0.3 and anything with score in between is neutral feedback.

Conclusion

Azure Cognitive Services is a rich suit of intelligent services that can be used to enrich the apps. Text Analytics API has several operations to turn text into meaningful insights. We used it to discover sentiment in the text from customers feedback. This is helpful because based on this we can make statistics to which extent customers are satisfied with the offers, products, etc. Using Azure Functions, the messages are sorted into different buckets or queues for further processing. The steps that we take for easily integrating these intelligent services are:

  • Sign up for an access key
  • Formulate requests using the access key
  • POST request and parsing the responses for insights

Things we can consider for further improvements:

  • Test the solution with more text and document, so we can find more appropriate thresholds for sorting the positive, negative and neutral feedback;
  • Changing the implementation to make it capable of handling text in more languages and finding sentiment in them too.