Use the Azure Text Analytics Python API for Sentiment Analysis


Azure Text Analytics is a cloud-based text analytics service provided by Microsoft Azure that enables users to extract insights from unstructured text data. It offers a range of features, including sentiment analysis, key phrase extraction, language detection, named entity recognition, and part-of-speech tagging. Azure Text Analytics can be accessed through a REST API, which allows users to make requests to the service and receive responses in a variety of formats, including JSON.

To use the Azure Text Analytics API in Python, you will need to do the following:

  • Set up an Azure account and create a Text Analytics resource: You can do this through the Azure portal.
  • Install the Azure Python SDK: You can install the Azure Python SDK by running the following command: pip install azure-ai-textanalytics
  • Authenticate your Python script with Azure: To authenticate your Python script with Azure, you will need to provide your Azure subscription key and endpoint. You can find your subscription key and endpoint in the Azure portal.
  • Make requests to the Azure Text Analytics API: Once you have authenticated your Python script with Azure, you can make requests to the Text Analytics API using the TextAnalyticsClient class. For example, to analyze the sentiment of a piece of text, you can use the sentiment method of the TextAnalyticsClient class.

Here is an example of how you might use the Azure Text Analytics API in Python:

from import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

# Set up your Azure credentials
subscription_key = “your_subscription_key”
endpoint = “your_endpoint”
credential = AzureKeyCredential(subscription_key)

# Create a TextAnalyticsClient object
client = TextAnalyticsClient(endpoint=endpoint, credential=credential)

# Define the text to analyze
text = “This is a very positive review of your product.”

# Analyze the sentiment of the text
sentiment_result = client.sentiment(documents=[text])
sentiment_score = sentiment_result[0].sentiment_score

# Print the sentiment score

This code snippet will print out the sentiment score for the given text, which ranges from 0 (negative) to 1 (positive). You can find more information on using the Azure Text Analytics API in Python in the Azure Text Analytics documentation.