Analyse & Translate Text
Azure AI Language provides prebuilt NLP capabilities — sentiment analysis, NER, key phrase extraction, language detection, PII redaction. Azure AI Translator handles text and document translation across 100+ languages. Both are high-exam-weight services.
NLP terms — defined before code
AI techniques for understanding and generating human language. Covers tasks like determining sentiment, extracting key information, classifying text, and translating between languages.
Determining whether text expresses a positive, negative, neutral, or mixed opinion. Azure AI Language returns a sentiment label + confidence scores for each. Also provides sentence-level sentiment within a document.
Identifying and categorising real-world objects in text — people (PersonType), locations (Location), organisations (Organisation), dates (DateTime), phone numbers, etc. The model labels each entity with its category and confidence score.
Identifying the main concepts or topics in a document — the key phrases that capture the document's essence. Returns a list of phrases (not individual words). Useful for document summarisation and search indexing.
Identifying what language a piece of text is written in. Returns a language name, ISO 639-1 code (e.g. "en", "fr", "de"), and a confidence score. Handles mixed-language documents by returning the dominant language.
Personally Identifiable Information detection — finding and optionally redacting sensitive information: names, email addresses, phone numbers, credit card numbers, social security numbers, etc. Returns entity locations and redacted text.
Matching entities found in text to their entries in a knowledge base (Wikipedia). Disambiguates entities — "Mars" in a space article links to the planet; "Mars" in a food article links to the chocolate bar company.
A more granular form of sentiment analysis that identifies the sentiment towards specific aspects of a product or service. "The battery life is great but the camera is poor" → battery=positive, camera=negative.
The Azure service providing prebuilt NLP capabilities. Uses the cognitiveservices.azure.com endpoint. Replaced older services: Text Analytics (now part of Language), LUIS (replaced by CLU), QnA Maker (replaced by Custom Q&A).
Azure service for translating text between 100+ languages. Separate from Azure AI Language — it does translation only. Endpoint: api.cognitive.microsofttranslator.com. Can detect source language automatically.
Converting text from one script/alphabet to another while preserving pronunciation — e.g. Japanese kanji to Latin alphabet. Different from translation (which converts meaning). Translator API supports transliteration.
Translating entire document files (PDF, DOCX, XLSX, PPTX) while preserving formatting. An async batch operation — you submit documents in Azure Blob Storage and Translator processes them. Different from the text translation API.
What the Language service can do
Imagine hiring an intern who can instantly read any document and tell you: "This review is negative." "The key topics are: pricing, delivery, packaging." "There are 4 people and 2 company names mentioned." "This contains a credit card number — should I redact it?" That's what Azure AI Language does — it's a fast, scalable editorial layer over text.
Returns: positive / negative / neutral / mixed at document level + per-sentence level. Each with a confidence score 0–1. Use for: customer reviews, support tickets, social media monitoring.
Returns a list of entities with: text (the actual text), category (Person, Location, Organisation, DateTime, etc.), sub-category, confidence score, and offset (position in text). Use for: extracting structured info from unstructured text.
Returns a list of key phrases representing the main topics. "The food was excellent and the service was prompt" → ["excellent food", "prompt service"]. Use for: document tagging, search indexing, summarisation.
Returns: detected language name, ISO code, confidence score. "Bonjour" → French (fr), 1.0. Handles 120+ languages. Use for: routing to correct language model, deciding which translator to use.
Returns: PII entities found + redacted text. "Contact John at john@email.com" → redacted: "Contact *** at ***". Categories: Name, Email, Phone, CreditCard, SSN, Address. Use for: GDPR compliance, data anonymisation.
Links entities to Wikipedia. Disambiguates between entities with the same name. Returns: entity name, Wikipedia URL, Bing entity ID, confidence score. Use for: knowledge graph construction, content enrichment.
Extension of sentiment analysis. Identifies target (aspect) + assessment (opinion word) + sentiment. "The price is reasonable but the warranty is terrible" → {price: positive}, {warranty: negative}.
"Company wants to know if customer reviews are positive or negative." → Sentiment Analysis
"Company wants to extract product names and dates mentioned in emails." → Named Entity Recognition
"Company wants to automatically tag documents with their main topics." → Key Phrase Extraction
"Company must redact personal information from documents before storage." → PII Detection
"Company receives multi-language support tickets and needs to route them." → Language Detection
Calling the Language service from Python
Install: pip install azure-ai-textanalytics azure-core
The endpoint format for Azure AI Language is: https://your-name.cognitiveservices.azure.com/
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# ── Create the client ─────────────────────────────────────────────────
# TextAnalyticsClient is the main client for Azure AI Language
# endpoint: your Language resource endpoint (cognitiveservices.azure.com)
# credential: your API key wrapped in AzureKeyCredential
client = TextAnalyticsClient(
endpoint = os.environ.get("LANGUAGE_ENDPOINT"),
credential = AzureKeyCredential(os.environ.get("LANGUAGE_KEY"))
)
# ── Documents to analyse ──────────────────────────────────────────────
# The API accepts a LIST of documents — batch processing in one call
# Each document is a string. IDs are assigned automatically.
documents = [
"The new laptop is absolutely fantastic — best purchase I've made all year!",
"Delivery was painfully slow and the packaging was completely destroyed.",
"The product itself is fine but the customer service is outstanding."
]
# ════════════════════════════════════════════════════════════════════
# 1. SENTIMENT ANALYSIS
# ════════════════════════════════════════════════════════════════════
print("--- Sentiment Analysis ---")
# analyze_sentiment() sends all documents at once and returns results in the same order
# show_opinion_mining=True enables aspect-based sentiment (Opinion Mining)
sentiment_results = client.analyze_sentiment(
documents,
show_opinion_mining=True
)
# Iterate through results — one result per document
for i, result in enumerate(sentiment_results):
if result.is_error: # Always check for errors
print(f"Doc {i}: Error — {result.error}")
continue
# result.sentiment: "positive", "negative", "neutral", or "mixed"
print(f"Doc {i}: {result.sentiment} "
f"(pos={result.confidence_scores.positive:.2f}, "
f"neg={result.confidence_scores.negative:.2f})")
# result.sentences: per-sentence sentiment
for sentence in result.sentences:
print(f" Sentence: '{sentence.text[:40]}' → {sentence.sentiment}")
# ════════════════════════════════════════════════════════════════════
# 2. NAMED ENTITY RECOGNITION
# ════════════════════════════════════════════════════════════════════
print("\n--- Named Entity Recognition ---")
ner_docs = ["Sarah Johnson from Microsoft called about the meeting on March 15th in Seattle."]
# recognize_entities() identifies named entities in text
ner_results = client.recognize_entities(ner_docs)
for result in ner_results:
if not result.is_error:
for entity in result.entities:
# entity.text: the actual text found ("Sarah Johnson")
# entity.category: the type ("Person", "Location", "DateTime", "Organization")
# entity.confidence_score: how confident (0-1)
print(f" Entity: '{entity.text}' | Category: {entity.category} | "
f"Confidence: {entity.confidence_score:.2f}")
# ════════════════════════════════════════════════════════════════════
# 3. KEY PHRASE EXTRACTION
# ════════════════════════════════════════════════════════════════════
print("\n--- Key Phrase Extraction ---")
# extract_key_phrases() returns the main topics of each document
kp_results = client.extract_key_phrases(documents)
for i, result in enumerate(kp_results):
if not result.is_error:
# result.key_phrases: a list of strings
print(f"Doc {i}: {', '.join(result.key_phrases)}")
# ════════════════════════════════════════════════════════════════════
# 4. LANGUAGE DETECTION
# ════════════════════════════════════════════════════════════════════
print("\n--- Language Detection ---")
mixed_docs = ["Hello, how are you?", "Bonjour, comment allez-vous?", "Hola, ¿cómo estás?"]
# detect_language() identifies the language of each document
lang_results = client.detect_language(mixed_docs)
for i, result in enumerate(lang_results):
if not result.is_error:
# result.primary_language.name: "English", "French", "Spanish"
# result.primary_language.iso6391_name: "en", "fr", "es"
# result.primary_language.confidence_score: 0-1
print(f"Doc {i}: {result.primary_language.name} "
f"({result.primary_language.iso6391_name}) — "
f"confidence {result.primary_language.confidence_score:.2f}")
# ════════════════════════════════════════════════════════════════════
# 5. PII DETECTION & REDACTION
# ════════════════════════════════════════════════════════════════════
print("\n--- PII Detection ---")
pii_docs = ["Please contact John Smith at john.smith@email.com or call 416-555-0123."]
# recognize_pii_entities() detects and redacts PII
pii_results = client.recognize_pii_entities(pii_docs)
for result in pii_results:
if not result.is_error:
# result.redacted_text: original text with PII replaced by asterisks
print(f"Redacted: {result.redacted_text}")
for entity in result.entities:
print(f" PII found: '{entity.text}' | Category: {entity.category}")
Translating text between languages
Azure AI Translator is a separate service from Azure AI Language — it does translation only, but does it very well across 100+ languages. The API uses a different endpoint format and authentication pattern.
Azure AI Language: https://your-name.cognitiveservices.azure.com/ — all NLP text analysis features.
Azure AI Translator: https://api.cognitive.microsofttranslator.com/ — this is a global endpoint (not resource-specific). You pass your key in the Ocp-Apim-Subscription-Key header and your region in Ocp-Apim-Subscription-Region.
Translator capabilities
| Capability | What it does | API endpoint |
|---|---|---|
| Text Translation | Translate one or more text strings to one or more target languages in a single call | /translate |
| Language Detection | Detect the language of input text (similar to Language service — but use Translator when you're also translating) | /detect |
| Transliteration | Convert text from one script to another — e.g. Arabic to Latin without translating meaning | /transliterate |
| Dictionary Lookup | Find alternative translations for a word with part-of-speech information | /dictionary/lookup |
| Document Translation | Async batch translation of whole documents (PDF, DOCX) stored in Azure Blob Storage | Separate batch endpoint |
Python — Text Translation
Install: pip install requests (Translator uses REST directly — no dedicated SDK is required for simple calls)
import os, requests, json
# Azure AI Translator uses a GLOBAL endpoint — not resource-specific
# You authenticate with a subscription key in the request header
TRANSLATOR_ENDPOINT = "https://api.cognitive.microsofttranslator.com"
TRANSLATOR_KEY = os.environ.get("TRANSLATOR_KEY")
TRANSLATOR_REGION = os.environ.get("TRANSLATOR_REGION") # e.g. "eastus"
# ── Text Translation ──────────────────────────────────────────────────
# Translate text to one or more target languages in a single request
# URL: base endpoint + /translate + API version
url = f"{TRANSLATOR_ENDPOINT}/translate?api-version=3.0"
# Headers contain authentication — different from other Azure AI services
# Ocp-Apim-Subscription-Key: your Translator resource key
# Ocp-Apim-Subscription-Region: the region your Translator resource is in
headers = {
"Ocp-Apim-Subscription-Key" : TRANSLATOR_KEY,
"Ocp-Apim-Subscription-Region": TRANSLATOR_REGION,
"Content-Type" : "application/json"
}
# Body: a list of text objects to translate
# You can translate multiple strings and to multiple languages in one call
body = [
{"text": "Hello, how are you today?"},
{"text": "Thank you for your business."}
]
# Params: specify target languages with "to" — can pass multiple "to" values
# from_param is optional — if omitted, language is auto-detected
params = {
"to": ["fr", "es", "de"] # Translate to French, Spanish, and German simultaneously
}
# Make the POST request
response = requests.post(url, headers=headers, json=body, params=params)
# Parse the JSON response
translations = response.json()
# Process results — one result per input text
for i, result in enumerate(translations):
print(f"\nOriginal ({i+1}): {body[i]['text']}")
# result["translations"] is a list — one entry per target language
for t in result["translations"]:
print(f" {t['to'].upper()}: {t['text']}")
# ── Language Detection (via Translator) ───────────────────────────────
# Use when you need to detect language AND are using the Translator service
detect_url = f"{TRANSLATOR_ENDPOINT}/detect?api-version=3.0"
detect_response = requests.post(
detect_url,
headers = headers,
json = [{"text": "Guten Morgen, wie geht es Ihnen?"}]
)
detect_result = detect_response.json()
# result[0]["language"] → ISO code e.g. "de"
# result[0]["score"] → confidence 0-1
print(f"\nDetected: {detect_result[0]['language']} "
f"(confidence: {detect_result[0]['score']:.2f})")
Document Translation
For translating entire documents (PDFs, Word files, PowerPoint), use the Document Translation feature — an async batch operation using Azure Blob Storage.
Store your PDF, DOCX, XLSX, or PPTX files in a source container in Azure Blob Storage. Generate a SAS token for access.
POST to the Document Translation batch endpoint with source URL, target URL, and target languages. Returns a job ID.
Poll the job status endpoint. When complete, translated documents are in the target Blob container — same filename structure, translated content, formatting preserved.
Analyse & Translate — what the exam tests
1. Azure AI Language endpoint = cognitiveservices.azure.com — Not openai.azure.com (that's Azure OpenAI). Know both endpoint patterns.
2. Translator endpoint = api.cognitive.microsofttranslator.com (global) — Unlike other Azure AI services which use resource-specific endpoints. Key header: Ocp-Apim-Subscription-Key + Ocp-Apim-Subscription-Region.
3. Document Translation is async and uses Blob Storage — Not the same as text translation API. Source documents go in, translated documents come out. Asynchronous — poll for completion.
4. PII Detection returns both entities AND redacted text — result.redacted_text has asterisks replacing PII. Useful for compliance scenarios.
5. Sentiment can be document-level AND sentence-level — result.sentiment = document, result.sentences = per-sentence. Opinion Mining gives aspect-level (e.g. battery=positive, camera=negative).
Test your understanding
Which Azure AI Language feature addresses the second requirement (identifying which specific services are being praised or criticised)?
B is correct. Opinion Mining (also called aspect-based sentiment analysis, enabled via show_opinion_mining=True) identifies specific aspects (targets) within a document and the sentiment towards each. "The loan process is painfully slow but the mobile app is great" → {loan process: negative}, {mobile app: positive}. Standard Sentiment Analysis (A) only gives document/sentence level — it won't tell you which service is being criticised. NER (C) finds entity names but not the sentiment towards them. Key Phrase Extraction (D) extracts phrases but doesn't associate sentiment with them.