Text and Speech Solutions in Foundry · Topic 1 of 2
Text Analysis Applications
AI-901 text analysis app: the Azure Language Python client, endpoint and credential, sentiment, key phrases, entities, summaries and per document errors.
- 5 min
- The package is
azure-ai-textanalyticsand the client isTextAnalyticsClient, created from an endpoint and a credential. - Features take a list of documents and return a list of results in the same order.
show_opinion_mining=Trueadds aspect level sentiment.
What it is
Azure Language, one of the Foundry Tools, offers ready made text analysis: sentiment, key phrases, entities, personal data detection, language detection and summarisation. A text analysis application sends text to the service with a client library and reads the structured results.
Because these features are pre built, you write very little code and need no model training.
How it works
Setting up
The Python package is azure-ai-textanalytics. The client needs the endpoint of your Language resource and a credential.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["LANGUAGE_ENDPOINT"]
key = os.environ["LANGUAGE_KEY"]
client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
The endpoint and key come from environment variables, not from the source file. Microsoft recommends Microsoft Entra ID authentication with managed identities for production, so there is no key to leak.
Calling a feature
Every feature takes a list of documents, where each document is a string, and returns a list of results in the same order.
documents = [
"The food and service were unacceptable. The concierge was nice, however.",
"Check in was quick and the room was spotless.",
]
result = client.analyze_sentiment(documents, show_opinion_mining=True)
for doc in result:
if doc.is_error:
print("Error:", doc.error)
continue
print(doc.sentiment)
print(doc.confidence_scores.positive, doc.confidence_scores.neutral, doc.confidence_scores.negative)
| Method | What it returns |
|---|---|
analyze_sentiment | Overall sentiment (positive, neutral, negative or mixed), sentence level sentiment and confidence scores. With show_opinion_mining=True, the sentiment attached to particular aspects |
extract_key_phrases | The main talking points of each document |
recognize_entities | Named entities, such as people, places, organisations and dates, with a category |
recognize_pii_entities | Personal data, such as phone numbers and emails |
detect_language | The language of each document |
begin_analyze_actions | Longer running actions, including extractive and abstractive summaries |
Reading results and errors
- The results line up with the input documents, so the first result belongs to the first document.
- Each result has an
is_errorproperty. If a single document is invalid, for example it is empty, its result is an error entry withis_errorset toTrue, while the other documents are still scored normally. The list keeps its length. - Always check
is_errorbefore reading the other properties of a result. - Confidence scores run from 0 to 1 and the three sentiment scores add up to 1.
Choosing between a dedicated service and a language model
You can also do text analysis by prompting a language model, for example "give the sentiment of this review as one word". The two approaches differ.
| Azure Language | A prompted language model | |
|---|---|---|
| Output | Fixed structured results with scores | Free text that you may need to parse |
| Consistency | Same input gives the same output format | Varies unless you constrain it |
| Cost and speed | Built for this task | Per token, often higher |
| Flexibility | The defined features | Any analysis you can describe |
Good practice
- Send documents in batches, which is more efficient than one call per text.
- Keep each document within the service's size limits, and split long text if needed.
- Do not log personal data. Use PII detection to redact it before storing text.
- Handle failures and rate limits with retries.
A worked scenario
A retailer wants a daily report of customer feedback. A Python script reads yesterday's reviews and sends them in a list to analyze_sentiment with opinion mining. It skips any result with is_error set, counts positive and negative reviews, and uses extract_key_phrases on the negative ones to see the common complaints. A batch of 25 reviews contains one empty string. The service scores the other 24 normally and returns an error entry for the empty one, so the script still has 25 results, and it logs the failed document. The key is read from an environment variable.
What to remember for the exam
- The package is
azure-ai-textanalyticsand the client isTextAnalyticsClient, created from an endpoint and a credential. - Features take a list of documents and return a list of results in the same order.
show_opinion_mining=Trueadds aspect level sentiment.- An invalid document gives an error entry with
is_errortrue, and the other documents are still processed. - Read keys from environment variables or Key Vault, or use Entra ID.
A scenario with "one empty string in a batch" is asking about is_error. The call does not fail, and the list keeps its length.
How the exam asks about this
- "What does a Python app supply to create a TextAnalyticsClient?" A resource endpoint and a credential.
- "A batch of 25 reviews contains one empty string. What does the app see?" The other 24 are scored and the empty one has is_error set to True.
- "Which setting adds sentiment for specific aspects?" show_opinion_mining.
- "Which feature finds phone numbers and email addresses?" PII detection.
Common mistakes
Assuming one bad document fails the whole call. The service returns an error entry for that document only.
Reading properties without checking is_error. An error entry has no sentiment.
Hard coding the key. Use environment variables, Key Vault or Entra ID.
Treating summaries as a sentiment feature. Summarisation is a separate action.
Check yourself
What two values create a TextAnalyticsClient?
The Language resource endpoint and a credential.
What does is_error indicate?
That the result for that document is an error, for example because the text was empty.
What does opinion mining add?
Sentiment linked to specific aspects, such as the food or the service.
Topic 1 of 2 in Text and Speech Solutions in Foundry
Test yourself on Text and Speech Solutions in Foundry
The full Microsoft AI-901 question bank has 511 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.