How to perform Sentiment Analysis using Azure Cognitive Services?

Sriram
4 min readMar 20, 2022

--

Introduction

The purpose of this article is to outline how we can leverage Azure Cognitive Service for Language in order to analyze the sentiment of a text. This is one of the use cases along with many others of this service.

Here are a few other use cases:

1. Broad Entity Recognition

Recognize significant ideas in a text, including key expressions and named elements like individuals, occasions, and associations.

2. Powerful Sentiment Analysis

Analyze the sentiment around specific topics by analyzing what customers are saying about your brand.

3. Document Summarization

Identify sentences that collectively convey the essence of a document.

4. Process Medical Text

Insights from the unstructured medical text can be extracted and processed in real-time and batch mode.

Now let's see how we can build one simple service with best practices.

  1. Azure Cognitive Services for Language Service
  2. Azure Function App
  3. Azure Storage Account
  4. Managed Identity

Let’s see the steps now

  1. Log into Azure Portal
  2. Create a Basic Function App

3. Open Cognitive Services and click on Create as below to create a Language Service

4. Select “Custom text classification & extraction” and click “Continue to Create your resource”

5. Provide required details

6. Provide name for storage account or select one from dropdown if you would like to choose existing

7. Select the acknowledgement boxes (there are two).
8. Click on “Review + Create” and then “Create” to create the resource.

Assign managed identity permissions to cognitive service.

  1. Enable system managed identity on newly created function app as shown below.

2. Assign “Cognitive Service User” role for newly created cognitive service to the managed identity created above, as shown below.

3. Assign “Storage Queue Data Contributor” and “Storage Blob Data Contributor” role to system managed identity of function app in storage account created for cognitive app above.

4. Create a new container is storage account as well, which we will use as “blob trigger” for function app.

Write Function App

  1. Open Visual Studio and create a select function app template after selecting to create a new project.

2. Select “Blob Trigger” as trigger as shown below and provide a setting name and name of container the function app should keep looking for change.

3. Once solution loads open the nuget package manager and update all the packages to the latest and some more.

4. Update the code as below.

5. Publish the newly project to function app created above.

Configure Function App

  1. Create two new application settings is function app with following properties
<CONNECTION_NAME_PREFIX>__blobServiceUri -https://<storage_account_name>.blob.core.windows.net
<CONNECTION_NAME_PREFIX>__queueServiceUri -https://<storage_account_name>.queue.core.windows.net

Test Function App

1. Open the test window for our function and expand the logs window there.

2. Upload a file to the container referred in our function.

3. Once the upload is complete you’d see the logs as below to show the sentiment analysis.

This concludes our testing of Azure Cognitive Services for Language Service Text Analytics for Sentiment Analysis.

Please find the source code here.

Conclusion

In this demonstration, I used the Azure PaaS services to implement the functionality considering Security, Cost, Scalability for best practices.

--

--