Skip to content

Latest commit

 

History

History
93 lines (70 loc) · 2.27 KB

readme.md

File metadata and controls

93 lines (70 loc) · 2.27 KB

Text Analytics Low Level Client

Install

npm install --save https://github.com/joheredi/ta-lowlevel-prototype
npm install --save @azure/identity

Initialize a client

import TextAnalytics from "@azure/textanalytics-lowlevel";
import { DefaultAzureCredential } from "@azure/identity";

const endpoint = "https://<accountName>.cognitiveservices.azure.com"

const client = TextAnalytics(
    new DefaultAzureCredential(),
    endpoint
  );

Call an endpoint

import TextAnalytics from "@azure/textanalytics-lowlevel";
import { DefaultAzureCredential } from "@azure/identity";

const endpoint = "https://<accountName>.cognitiveservices.azure.com"

async function analyzeLanguage() {
const client = TextAnalytics(
    new DefaultAzureCredential(),
    endpoint
  );

  const languagesResult = await client.request("POST /languages", {
    body: { documents: [{ id: "1", text: "This is a test text" }] },
  });

  if (languagesResult.status === 200) {
    for (const result of languagesResult.body.documents) {
      console.log(
        `Sentence with Id: '${result.id}' detected language: ${
          result.detectedLanguage.name
        } with ${result.detectedLanguage.confidenceScore * 100}% confidence`
      );
    }
  } else {
    throw languagesResult.body.error;
  }
}

analyzeLanguage().catch(console.error)

Call an arbitrary endpoint with requestUnchecked

import TextAnalytics from "@azure/textanalytics-lowlevel";
import { DefaultAzureCredential } from "@azure/identity";

const endpoint = "https://<accountName>.cognitiveservices.azure.com"

async function analyzeLanguage() {
const client = TextAnalytics(
    new DefaultAzureCredential(),
    endpoint
  );

  const languagesResult = await client.requestUnchecked("POST /languages", {
    body: { documents: [{ id: "1", text: "This is a test text" }] },
  });

  if (languagesResult.status === 200) {
    for (const result of languagesResult.body.documents) {
      console.log(
        `Sentence with Id: '${result.id}' detected language: ${
          result.detectedLanguage.name
        } with ${result.detectedLanguage.confidenceScore * 100}% confidence`
      );
    }
  } else {
    throw languagesResult.body.error;
  }
}

analyzeLanguage().catch(console.error)