Azure AI Sentiment Analysis Tutorial

Learn how to implement sentiment analysis using Azure Cognitive Services.

Introduction to Sentiment Analysis

Sentiment analysis, also known as opinion mining, is the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer's attitude towards a particular topic, product, etc., is positive, negative, or neutral.

Azure Cognitive Services provides a powerful and easy-to-use Text Analytics API that includes sentiment analysis as a core feature. This tutorial will guide you through the steps to integrate this capability into your applications.

Prerequisites

Step 1: Create an Azure Text Analytics Resource

1

Navigate to the Azure Portal

Log in to your Azure account at portal.azure.com.

2

Create a Text Analytics Resource

Click on "Create a resource", search for "Text Analytics", and select it. Click "Create".

Fill in the required details such as Subscription, Resource group, Region, and Name. Choose a pricing tier (e.g., F0 for free tier testing).

3

Get Endpoint and Key

Once the resource is deployed, navigate to its overview page. You will find your "Endpoint" and one of the "Keys" listed under "Resource Management". Keep these secure as they are required for authentication.

Step 2: Implement Sentiment Analysis with Python

This example uses the Azure Text Analytics client library for Python.

Install the SDK

pip install azure-ai-textanalytics

Python Code Example

from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import TextAnalyticsClient endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT" key = "YOUR_TEXT_ANALYTICS_KEY" text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) documents = [ {"id": 1, "text": "This is a wonderful library!"}, {"id": 2, "text": "I am not satisfied with the service."}, {"id": 3, "text": "The weather is neutral today."}, {"id": 4, "text": "I love this product, it's amazing and so easy to use."} ] try: response = text_analytics_client.analyze_sentiment(documents=documents, show_opinion_mining=True) for doc in response: print(f"Document ID: {doc.id}") print(f"Overall Sentiment: {doc.sentiment}") print(f"Confidence Scores: Positive={doc.confidence_scores.positive:.2f}, Neutral={doc.confidence_scores.neutral:.2f}, Negative={doc.confidence_scores.negative:.2f}") if doc.mined_opinions: print("Mined Opinions:") for opinion in doc.mined_opinions: print(f" - Target: {opinion.target.text}") print(f" Sentiment: {opinion.sentiment}") print(f" Assessments: {', '.join([a.text for a in opinion.assessments])}") print("-" * 20) except Exception as err: print(f"Encountered an exception: {err}")

Remember to replace YOUR_TEXT_ANALYTICS_ENDPOINT and YOUR_TEXT_ANALYTICS_KEY with your actual credentials.

Note: The show_opinion_mining=True parameter enables more granular analysis, extracting sentiments associated with specific targets (e.g., product features).

Understanding the Output

The sentiment analysis result for each document typically includes:

Sentiment Scoring

The API returns a sentiment score, which can be interpreted as follows:

Best Practices and Further Exploration

Tip: For production applications, store your Text Analytics keys securely using Azure Key Vault. Avoid hardcoding them directly in your code.

Conclusion

You have successfully learned how to perform sentiment analysis using Azure Cognitive Services. This powerful feature can provide valuable insights into customer feedback, social media monitoring, and more. Continue exploring the Azure AI platform to unlock the full potential of artificial intelligence.

For more detailed information, please refer to the official Azure Text Analytics documentation.