DashboardPart 4Analyse & Translate Text
💬 Part 4 · Topic 1 of 3

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.

🐍 Python — every line explained ⏱️ ~50 min 🔥 High exam weight

NLP terms — defined before code

Natural Language Processing (NLP)

AI techniques for understanding and generating human language. Covers tasks like determining sentiment, extracting key information, classifying text, and translating between languages.

Sentiment Analysis

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.

Named Entity Recognition (NER)

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.

Key Phrase Extraction

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.

Language Detection

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.

PII Detection

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.

Entity Linking

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.

Opinion Mining (Aspect-Based Sentiment)

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.

Azure AI Language (service name)

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 AI Translator

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.

Transliteration

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.

Document Translation

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

📖
Analogy — A very fast editorial intern

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.

😊
Sentiment Analysis

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.

🏷️
Named Entity Recognition (NER)

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.

🔑
Key Phrase Extraction

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.

🌍
Language Detection

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.

🔒
PII Detection & Redaction

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.

🔗
Entity Linking

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.

📊
Opinion Mining (Aspect Sentiment)

Extension of sentiment analysis. Identifies target (aspect) + assessment (opinion word) + sentiment. "The price is reasonable but the warranty is terrible" → {price: positive}, {warranty: negative}.

🎯
Exam Tip — Choosing the Right Language Feature

"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.

⚠️
Key Exam Distinction — Language vs Translator endpoints

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

CapabilityWhat it doesAPI endpoint
Text TranslationTranslate one or more text strings to one or more target languages in a single call/translate
Language DetectionDetect the language of input text (similar to Language service — but use Translator when you're also translating)/detect
TransliterationConvert text from one script to another — e.g. Arabic to Latin without translating meaning/transliterate
Dictionary LookupFind alternative translations for a word with part-of-speech information/dictionary/lookup
Document TranslationAsync batch translation of whole documents (PDF, DOCX) stored in Azure Blob StorageSeparate 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.

1
Upload source documents to 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.

2
Submit a batch translation job

POST to the Document Translation batch endpoint with source URL, target URL, and target languages. Returns a job ID.

3
Poll for completion and download results

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

⚠️
Top 5 Tested Facts from This Topic

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

Topic 1 Quiz1 / 5
Scenario: A bank processes thousands of customer complaint emails daily. They need to automatically determine whether each email expresses frustration, satisfaction, or is neutral — and additionally identify which specific banking services (loan, card, mobile app) are being criticised or praised.

Which Azure AI Language feature addresses the second requirement (identifying which specific services are being praised or criticised)?

💡 Explanation

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.

A company needs to redact personally identifiable information from documents before storing them in a data lake for analysis. Which Azure AI Language feature should they use?

💡 Explanation

B is correct. PII Detection is specifically designed for this use case. It not only identifies PII entities (name, email, phone, credit card, SSN, address) but also returns a redacted_text property with the PII already replaced by asterisks. This makes it directly usable for compliance workflows. NER (A) finds entities including persons and locations but does not focus on PII categories and doesn't provide redacted text. Key Phrase Extraction (C) extracts topics, not PII. Content Safety (D) detects harmful content categories like violence/hate — not PII.

A global company receives product reviews in 15 different languages. Before analysis, they need to identify the language of each review so it can be routed to the correct regional processing pipeline. Which service and feature is most appropriate?

💡 Explanation

C is correct. Both Azure AI Language and Azure AI Translator offer language detection. Since the company already uses Azure AI Language for review analysis (sentiment, NER, etc.), it makes sense to use detect_language() there — one service, one integration, lower complexity. Both are valid technically. GPT-4o (D) could detect language but is significantly more expensive and slower for a high-volume classification task that prebuilt services handle reliably.

Scenario: A legal firm needs to translate 2,000 PDF contracts from English to French, German, and Spanish for a multinational deal. The formatted documents must be delivered to the client. The translation must preserve all formatting including tables, signatures, and headers.

Which Translator feature should be used?

💡 Explanation

B is correct. Document Translation is specifically designed for translating entire document files while preserving formatting. You upload source PDFs to Azure Blob Storage, submit a batch translation job, and translated documents are written to a target container — tables, headers, and formatting intact. Text Translation (A) only handles plain text strings — you'd need to extract text, translate it, then reassemble formatting manually (complex and lossy). Transliteration (C) converts script, not language — it doesn't translate meaning. Dictionary Lookup (D) translates individual words, not entire documents.

Which is the correct endpoint pattern for Azure AI Translator (not Azure AI Language)?

💡 Explanation

B is correct. Azure AI Translator uses a global endpoint: https://api.cognitive.microsofttranslator.com. Unlike most Azure AI services which use resource-specific endpoints (your-name.cognitiveservices.azure.com), the Translator global endpoint is the same for all customers. You authenticate per-request using Ocp-Apim-Subscription-Key and Ocp-Apim-Subscription-Region headers. Option A shows the Azure AI Language endpoint structure. Option C is an Azure OpenAI endpoint. Option D doesn't match any real Azure endpoint pattern.

Mark this topic as complete