Azure Text Analytics SDK for JavaScript

The Azure Text Analytics client library for JavaScript enables you to perform natural language processing tasks on unstructured text. This SDK provides a convenient way to integrate powerful AI-driven text analysis capabilities into your JavaScript applications, whether they run in the browser or on Node.js.

Key features include:

  • Sentiment Analysis: Determine the overall sentiment (positive, negative, neutral, mixed) of a document or sentence.
  • Key Phrase Extraction: Identify the main talking points within a document.
  • Language Detection: Detect the language of the input text.
  • Entity Recognition: Recognize and categorize entities (people, places, organizations, etc.) within text.
  • Named Entity Recognition (NER): A more advanced form of entity recognition for specific types of entities.
  • Personally Identifiable Information (PII) Detection: Identify and redact sensitive personal information.

Prerequisites

Before you can use the Azure Text Analytics SDK, you'll need:

  • An Azure subscription.
  • A Text Analytics resource created in your Azure subscription. You can create one via the Azure portal.
  • A Text Analytics API key and endpoint. These can be found in your Text Analytics resource's "Keys and Endpoint" section.
  • Node.js installed (if targeting Node.js).

Installation

To install the SDK for Node.js:

npm install @azure/cognitiveservices-textanalytics

For browser-based applications, you can use a CDN or bundle the library with your project.

Core Concepts

The SDK is built around the concept of a TextAnalyticsClient. You'll instantiate this client with your API key and endpoint to interact with the Text Analytics service.

Getting Started

Here's a basic example of how to use the SDK to detect the language of a text document.

Language Detection Example


// Import the TextAnalyticsClient and TextDocumentInput
import { TextAnalyticsClient, TextDocumentInput } from "@azure/cognitiveservices-textanalytics";
import { AzureKeyCredential } from "@azure/core-auth";

// Replace with your actual subscription key and endpoint
const subscriptionKey = "YOUR_TEXT_ANALYTICS_SUBSCRIPTION_KEY";
const endpoint = "YOUR_TEXT_ANALYTICS_ENDPOINT";

// Authenticate the client
const auth = new AzureKeyCredential(subscriptionKey);
const client = new TextAnalyticsClient(endpoint, auth);

// Prepare the document(s) for analysis
const documents: TextDocumentInput[] = [
    { id: "1", text: "Hello world!" },
    { id: "2", text: "Bonjour le monde!" }
];

async function detectLanguage() {
    try {
        const results = await client.detectLanguage(documents);
        console.log("Language Detection Results:");
        for (const doc of results) {
            if (!doc.error) {
                console.log(`  Document ID: ${doc.id}, Detected Language: ${doc.primaryLanguage.name} (ISO6391: ${doc.primaryLanguage.iso6391Name})`);
            } else {
                console.error(`  Document ID: ${doc.id}, Error: ${doc.error.message}`);
            }
        }
    } catch (error) {
        console.error("An error occurred during language detection:", error);
    }
}

detectLanguage();
                    

API Reference

TextAnalyticsClient

The primary client for interacting with the Text Analytics service.

constructor(endpoint: string, credential: AzureKeyCredential)
Initializes a new instance of the TextAnalyticsClient.
endpoint
The endpoint URL of your Text Analytics resource.
credential
An instance of AzureKeyCredential with your API key.

detectLanguage(documents: TextDocumentInput[], options?: TextAnalyticsActionsOptions)

Detects the language of the input documents.

async detectLanguage(documents: TextDocumentInput[], options?: TextAnalyticsActionsOptions): Promise<DetectLanguageResult[]>
documents
An array of TextDocumentInput objects, each containing an id and the text to analyze.
options
Optional configuration for the request, such as model version or logging.
Promise<DetectLanguageResult[]>
A promise that resolves to an array of DetectLanguageResult objects, corresponding to each input document.

recognizeEntities(documents: TextDocumentInput[], options?: TextAnalyticsActionsOptions)

Recognizes entities in the input documents.

async recognizeEntities(documents: TextDocumentInput[], options?: TextAnalyticsActionsOptions): Promise<RecognizeEntitiesResult[]>
documents
An array of TextDocumentInput objects.
options
Optional configuration.
Promise<RecognizeEntitiesResult[]>
A promise that resolves to an array of RecognizeEntitiesResult objects.

Explore the full API reference for more details on all available methods such as recognizePiiEntities, analyzeSentiment, and extractKeyPhrases.