Unleash the Power of AI in Your Projects: Build a Sentiment Analysis App with Azure Cognitive Services
Shaghik Amirian
AI Researcher ∣ ML Engineer ∣ Research Fellow ∣ Data Science & ML ∣ Graph Optimization ∣ Blockchain & Smart Contract Development | Procurement Digutalization Expert
Want to add intelligence to your applications?
Look no further than Azure Cognitive Services! This powerful suite offers pre-built AI functionalities that you can effortlessly integrate into your projects.
In this article, we'll embark on a journey to create a basic sentiment analysis application using Azure Cognitive Services. This application will analyze the sentiment of any text you provide, giving you a taste of how AI can revolutionize your work.
Prerequisites:
Step-by-Step Guide:
Step 1: Setting Up Azure Cognitive Service
Step 2: Installing the Azure AI SDK Open your terminal or command prompt and install the Azure AI Text Analytics client library for Python with pip:
pip install azure-ai-textanalytics --pre
pip install azure-ai-textanalytics --pre
Step 3: Writing the Sentiment Analysis Code Create a new Python file in your IDE and import the necessary libraries:
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
Set up the client using your obtained endpoint and key:
领英推荐
key = "your_cognitive_services_key"
endpoint = "your_cognitive_services_endpoint"
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
Design a function to perform sentiment analysis:
def analyze_sentiment(text_input):
response = text_analytics_client.analyze_sentiment(documents=[text_input])[0]
print("Document Sentiment: {}".format(response.sentiment))
print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
response.confidence_scores.positive,
response.confidence_scores.neutral,
response.confidence_scores.negative,
))
Step 4:Test Your AI Creation!
Put your application to the test with some sample text:
text_input = "Hey Shaghik! Azure Cognitive Services makes it easy to integrate AI into your applications."
analyze_sentiment(text_input)
Run your Python script. You should see the sentiment analysis results printed to the console, indicating whether the sentiment is positive, neutral, or negative, along with the confidence scores.
Congratulations! You've successfully built and executed your first AI application with Azure Cognitive Services. This is just the beginning!
Expand Your Horizons:
Remember, the key to mastering Azure AI is exploration and practice. Don't hesitate to dive deeper into the resources and tutorials offered by Microsoft.
Happy coding, and happy innovating with Mathematics!