DEV Community

Mukul Bindal
Mukul Bindal

Posted on

Sentiment Analysis using Azure AI

What is Sentiment Analysis?

Sentiment analysis is a process in natural language processing (NLP) used to determine the emotional tone behind a body of text. It helps in understanding the sentiment (positive, negative, or neutral) of user opinions, social media comments, product reviews, etc. Companies leverage sentiment analysis to gauge customer feedback, monitor brand perception, and improve user experience.

Traditional Machine Learning Approach

Traditionally, sentiment analysis involves building machine learning models from scratch. This process requires several key steps:

  1. Data Collection: Large datasets are needed to train the model. These datasets must be labeled with sentiment classes (positive, negative, or neutral).

  2. Feature Extraction: Text data is transformed into numerical features through techniques like TF-IDF or word embeddings.

  3. Model Training: Machine learning models such as logistic regression, Naive Bayes, or deep learning models like LSTMs are trained on the prepared data.

  4. Evaluation and Tuning: The model needs to be evaluated and fine-tuned regularly for better accuracy.

However, this approach has its disadvantages:

  • High Computational Resources: Building and training models from scratch can be computationally expensive.

  • Need for Large Datasets: High-quality, labeled datasets are needed, which can be hard to obtain.

  • Time-Consuming: The model development process, from data preparation to tuning, is time-intensive.

  • Low Accuracy: Without a large dataset and correct model, accuracy is not good and often leads to less reliability.

Advantages of Azure AI Over Traditional Approach

Azure AI provides a robust, pre-trained sentiment analysis model as part of its Language Service. Key benefits include:

  • Scalability: Azure AI allows you to scale up your applications without needing your own infrastructure.

  • Pre-trained Models: No need for large datasets or model training—Azure’s pre-trained models are ready to use.

  • Cost-Effective: By using a cloud-based service, the need for high computational power and resources is minimized.

  • Faster Time to Market: Azure’s API can be integrated with your application easily, significantly reducing development time.

Prerequisites

Before using Azure AI for sentiment analysis, you will need:

  • An Azure subscription.

  • Azure AI Language Service enabled in your account.

  • Familiarity with Python or another programming language.

Steps to Implement Sentiment Analysis using Azure AI

1. Create Azure Language Service

a. Go to the Azure portal and create a Language Service resource.

b. Select your subscription, resource group, and region.

c. Once the resource is created, you’ll receive an endpoint and API key, which will be used for API calls.

2. Install Azure SDK

Install the azure-ai-textanalytics package:

pip install azure-ai-textanalytics

3. Connect to the Service

a. Go to your Language Service Resource > Keys and Endpoints and copy the credentials

b. Use the API key and endpoint from your Language Service resource to authenticate your requests.

4. Send Text for Sentiment Analysis

Pass the raw text you want to analyze to the API for sentiment detection, without any need of preprocessing.

Sample Code

Below is an example of how one can use Azure AI services for NLP:

#!/usr/bin/env python3

# Import required packages
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

# Add the Key and Endpoint (Should be in env variables)
KEY='YOUR_LANGUAGE_SERVICE_KEY'
ENDPOINT='YOUR_LANGUAGE_SERVICE_ENDPOINT'

# Create a text analytics client
text_analytics_client = TextAnalyticsClient(ENDPOINT, AzureKeyCredential(KEY))

# Load your data
documents = ["I am thrilled to announce that our team has exceeded all our quarterly targets!",
"I was really disappointed with the service at the restaurant last night",
"The meeting is scheduled for 3 PM tomorrow in the main conference room.",
"She couldn’t stop smiling after receiving the surprise gift from her friends.",
"He felt a deep sense of loss when he heard about the passing of his childhood pet."]

# Call the Azure Service to detect sentiment
response = text_analytics_client.analyze_sentiment(documents=documents)

# Print output
for i, resp in enumerate(response):
    print(f"Text: {documents[i]}\t Sentiment: {resp.sentiment}")

Enter fullscreen mode Exit fullscreen mode

Output:

Text: I am thrilled to announce that our team has exceeded all our quarterly targets!    Sentiment: positive
Text: I was really disappointed with the service at the restaurant last night    Sentiment: negative
Text: The meeting is scheduled for 3 PM tomorrow in the main conference room.    Sentiment: neutral
Text: She couldn’t stop smiling after receiving the surprise gift from her friends.    Sentiment: positive
Text: He felt a deep sense of loss when he heard about the passing of his childhood pet.     Sentiment: negative
Enter fullscreen mode Exit fullscreen mode

You can observe that this is a much simplified code than the traditional approach and with better accuracy and minimum time investment.

Conclusion

Azure AI simplifies sentiment analysis with its pre-trained, ready-to-use models. By removing the complexity of traditional machine learning approaches, Azure allows developers to focus more on delivering value to their applications without worrying about computational resources, data preparation, or model training. This makes sentiment analysis, or any other NLP task faster, scalable, and more accessible.

Top comments (0)