(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

Sentiment analysis in Azure AI Language: step by step

João Barros 05 de July de 2026 4 min read

Understanding whether customer comments are positive or negative stops being slow, manual work once you use sentiment analysis in Azure AI Language. With just a few lines of Python you can classify any text as positive, negative, neutral or mixed and get a confidence score for every sentence. Let's build that classifier from scratch, with a practical example you can adapt to your own business.

Prerequisites

  • An active Azure subscription — the service has a free tier to get started.
  • Python 3.8 or later installed on your machine.
  • Permissions to create resources in the Azure portal.
  • Basic comfort with the command line and a code editor.

Step 1: Create the Language resource in the Azure portal

Start by creating the service that will do the analysis. In the Azure portal, click Create a resource and search for Language service. Choose the subscription, a resource group, the region closest to your users and the free pricing tier (F0), perfect for learning. Once the resource is ready, open the Keys and Endpoint section and copy the endpoint and one of the keys: these two values authenticate your code.

Step 2: Install the Python SDK

Create a folder for the project and install the official library. A virtual environment keeps dependencies isolated; on Windows, activate it with the activate script inside the .venv folder.

python -m venv .venv
source .venv/bin/activate
pip install azure-ai-textanalytics

Step 3: Authenticate the client

Never write the key straight into the code. Store the endpoint and key in environment variables and read them with the os module — that way you never expose secrets when you share or publish the project.

import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

endpoint = os.environ["LANGUAGE_ENDPOINT"]
key = os.environ["LANGUAGE_KEY"]

client = TextAnalyticsClient(
    endpoint=endpoint,
    credential=AzureKeyCredential(key),
)

Step 4: Analyze the sentiment of a text

The analyze_sentiment method takes a list of documents and returns, for each one, the overall sentiment and the confidence in each class. As an example we use two real Portuguese comments — the service detects the language automatically.

documents = [
    "O atendimento foi excelente e a entrega chegou antes do previsto.",
    "O produto veio partido e ninguém respondeu ao meu email.",
]

response = client.analyze_sentiment(documents=documents)

for doc in response:
    print("Sentimento:", doc.sentiment)
    print("Confiança:", doc.confidence_scores)

Each doc.sentiment will be positive, negative, neutral or mixed. Meanwhile confidence_scores holds three values between 0 and 1 — positive, neutral and negative — that add up to 1.

Step 5: Drill down to the sentence level

A single comment often mixes praise and criticism. The result includes a sentences list, which lets you see sentiment sentence by sentence and pinpoint exactly where the problem is.

for doc in response:
    print("Documento:", doc.sentiment)
    for sentence in doc.sentences:
        print(" -", sentence.sentiment, "->", sentence.text)
Tip: enable show_opinion_mining=True in the call to link each opinion to the target it refers to (for example, "entrega" or "atendimento").

Check the result

Save the file as sentimento.py and run it with python sentimento.py. The first sentence should return positive with high confidence and the second negative. If you get a 401 error, double-check the key and endpoint; if it's a 404, make sure you copied the full endpoint. Then swap the texts for real comments from your business to confirm the classification makes sense.

Conclusion

In five steps you connected Python to Azure AI Language and turned free text into measurable sentiment — a solid foundation for satisfaction dashboards or automatic alerts for unhappy customers. From here you can explore key phrase extraction, entity recognition or process hundreds of comments at once. Which text source will you analyze first: emails, reviews or social media?