Azure Text Analytics

Azure Text Analytics is a cloud-based service that provides advanced natural language processing (NLP) capabilities. It enables you to analyze unstructured text to gain insights into its content, such as sentiment, key phrases, entities, and language. This service is part of Azure Cognitive Services, offering powerful AI tools accessible via REST APIs and client libraries.

Key Features

Getting Started

To use Azure Text Analytics, you need an Azure subscription and a Text Analytics resource.

  1. Create an Azure Account: If you don't have one, sign up for a free Azure account.
  2. Create a Text Analytics Resource: Navigate to the Azure portal, search for "Text Analytics," and create a new resource. You will obtain an endpoint and an API key.
  3. Make API Calls: Use the provided endpoint and API key to send your text data to the service.
Important: Keep your API keys secure and do not expose them in client-side code. Use them only on your backend servers.

API Reference

Analyze Text API

The Analyze Text API allows you to perform multiple text analytics tasks in a single request.

Endpoint:

https://YOUR_TEXT_ANALYTICS_ENDPOINT/text/analytics/v3.2-preview.1/analyze

Request Body (JSON):


{
    "documents": [
        {
            "id": "1",
            "language": "en",
            "text": "This is a sample document for text analytics."
        },
        {
            "id": "2",
            "language": "es",
            "text": "Este es otro documento de ejemplo."
        }
    ],
    "tasks": {
        "sentimentAnalysis": [{}],
        "keyPhraseExtraction": [{}],
        "entityRecognition": [{}],
        "languageDetection": [{}]
    }
}
            

Parameters:

Parameter Type Description
documents Array of objects A list of documents to process. Each document must have a unique id, language, and the text content.
tasks Object Defines the operations to perform on the documents. Supports sentimentAnalysis, keyPhraseExtraction, entityRecognition, and languageDetection.

Response Body (JSON Example):


{
    "results": {
        "documents": [
            {
                "id": "1",
                "sentiment": "neutral",
                "confidenceScores": {
                    "positive": 0.1,
                    "neutral": 0.8,
                    "negative": 0.1
                },
                "sentences": [
                    {
                        "sentiment": "neutral",
                        "confidenceScores": {
                            "positive": 0.1,
                            "neutral": 0.8,
                            "negative": 0.1
                        },
                        "offset": 0,
                        "length": 39,
                        "text": "This is a sample document for text analytics."
                    }
                ],
                "warnings": []
            },
            {
                "id": "2",
                "sentiment": "positive",
                "confidenceScores": {
                    "positive": 0.9,
                    "neutral": 0.05,
                    "negative": 0.05
                },
                "sentences": [
                    {
                        "sentiment": "positive",
                        "confidenceScores": {
                            "positive": 0.9,
                            "neutral": 0.05,
                            "negative": 0.05
                        },
                        "offset": 0,
                        "length": 36,
                        "text": "Este es otro documento de ejemplo."
                    }
                ],
                "warnings": []
            }
        ],
        "errors": [],
        "modelVersion": "2023-04-01"
    }
}
            

Code Examples

Python Example


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

endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT"
key = "YOUR_TEXT_ANALYTICS_API_KEY"

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

documents = [
    "I love the new features in Azure Text Analytics!",
    "The service was down for maintenance.",
    "This is a neutral statement."
]

try:
    response = text_analytics_client.analyze_sentiment(documents)
    for idx, doc in enumerate(response):
        if not doc.is_error:
            print(f"Document {idx+1}:")
            print(f"  Sentiment: {doc.sentiment}")
            print(f"  Positive score: {doc.confidence_scores.positive:.2f}")
            print(f"  Neutral score: {doc.confidence_scores.neutral:.2f}")
            print(f"  Negative score: {doc.confidence_scores.negative:.2f}")
        else:
            print(f"Document {idx+1} had an error: {doc.kind}")
except Exception as err:
    print(f"Encountered an exception: {err}")
                

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

endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT"
key = "YOUR_TEXT_ANALYTICS_API_KEY"

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

documents = [
    "Microsoft Corporation is headquartered in Redmond, Washington.",
    "The Eiffel Tower is a landmark in Paris, France."
]

try:
    response = text_analytics_client.recognize_entities(documents)
    for idx, doc in enumerate(response):
        if not doc.is_error:
            print(f"Document {idx+1}:")
            for entity in doc.entities:
                print(f"  - Entity: {entity.text}, Type: {entity.category}, Confidence: {entity.confidence_score:.2f}")
        else:
            print(f"Document {idx+1} had an error: {doc.kind}")
except Exception as err:
    print(f"Encountered an exception: {err}")
                

JavaScript Example (Node.js)


const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");

const endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT";
const key = "YOUR_TEXT_ANALYTICS_API_KEY";

const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(key));

async function analyzeSentiment(documents) {
    const results = await client.analyzeSentiment(documents);
    results.forEach(doc => {
        if (!doc.error) {
            console.log(`Document ID: ${doc.id}`);
            console.log(`  Sentiment: ${doc.sentiment}`);
            console.log(`  Confidence Scores: Positive=${doc.confidenceScores.positive.toFixed(2)}, Neutral=${doc.confidenceScores.neutral.toFixed(2)}, Negative=${doc.confidenceScores.negative.toFixed(2)}`);
        } else {
            console.log(`Document ${doc.id} had an error: ${doc.error.message}`);
        }
    });
}

const docs = [
    { id: "1", language: "en", text: "I love the new features in Azure Text Analytics!" },
    { id: "2", language: "en", text: "The service was down for maintenance." },
    { id: "3", language: "en", text: "This is a neutral statement." }
];

analyzeSentiment(docs).catch(err => console.error(`Error: ${err}`));
                

const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");

const endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT";
const key = "YOUR_TEXT_ANALYTICS_API_KEY";

const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(key));

async function recognizeEntities(documents) {
    const results = await client.recognizeEntities(documents);
    results.forEach(doc => {
        if (!doc.error) {
            console.log(`Document ID: ${doc.id}`);
            doc.entities.forEach(entity => {
                console.log(`  - Entity: ${entity.text}, Category: ${entity.category}, Confidence: ${entity.confidenceScore.toFixed(2)}`);
            });
        } else {
            console.log(`Document ${doc.id} had an error: ${doc.error.message}`);
        }
    });
}

const docs = [
    { id: "1", language: "en", text: "Microsoft Corporation is headquartered in Redmond, Washington." },
    { id: "2", language: "en", text: "The Eiffel Tower is a landmark in Paris, France." }
];

recognizeEntities(docs).catch(err => console.error(`Error: ${err}`));
                

Pricing and Quotas

Azure Text Analytics offers a free tier for experimentation and pay-as-you-go pricing for production workloads. Refer to the Azure Text Analytics pricing page for the most up-to-date information on costs and available tiers. Limits and quotas apply to API requests, which can be monitored and adjusted in the Azure portal.

Further Reading