Creates an instance of TextAnalysisClient with the endpoint of a Language resource and an authentication method such as an API key or AAD.
The API key and endpoint can be found in the Language resource page in the Azure portal. They will be located in the resource's Keys and Endpoint page, under Resource Management.
import { TextAnalysisClient, AzureKeyCredential } from "@azure/ai-language-text";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new AzureKeyCredential("<api key>");
const client = new TextAnalysisClient(endpoint, credential);
The URL to the endpoint of a Cognitive Language Service resource
Key credential to be used to authenticate requests to the service.
Optional
options: TextAnalysisClientOptionsUsed to configure the TextAnalytics client.
Creates an instance of TextAnalysisClient with the endpoint of a Language resource and an authentication method such as an API key or AAD.
The API key and endpoint can be found in the Language resource page in the Azure portal. They will be located in the resource's Keys and Endpoint page, under Resource Management.
See the @azure/identity
package for more information about authenticating with Azure Active Directory.
import { TextAnalysisClient } from "@azure/ai-language-text";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new DefaultAzureCredential();
const client = new TextAnalysisClient(endpoint, credential);
The URL to the endpoint of a Cognitive Language Service resource
Token credential to be used to authenticate requests to the service.
Optional
options: TextAnalysisClientOptionsUsed to configure the TextAnalytics client.
Runs a predictive model to determine the language that the passed-in input strings are written in, and returns, for each one, the detected language as well as a score indicating the model's confidence that the inferred language is correct. Scores close to 1 indicate high certainty in the result. 120 languages are supported.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const documents = [<input strings>];
const countryHint = "us";
const results = await client.analyze("LanguageDetection", documents, countryHint);
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.error) {
// a document has an error instead of results
} else {
const { name, confidenceScore, iso6391Name } = result.primaryLanguage;
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/language-detection/overview for more information on language detection.
an array of results where each element contains the primary language for the corresponding input document.
the name of the action to be performed on the input documents, see $AnalyzeActionName
the input documents to be analyzed
Optional
options: AnalyzeActionParameters<ActionName> & TextAnalysisOperationOptionsoptional action parameters and settings for the operation
Runs a predictive model to determine the language that the passed-in input strings are written in, and returns, for each one, the detected language as well as a score indicating the model's confidence that the inferred language is correct. Scores close to 1 indicate high certainty in the result. 120 languages are supported.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const documents = [<input strings>];
const countryHint = "us";
const results = await client.analyze("LanguageDetection", documents, countryHint);
for (const result of results) {
if (result.error) {
// a document has an error instead of results
} else {
const { name, confidenceScore, iso6391Name } = result.primaryLanguage;
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/language-detection/overview for more information on language detection.
an array of results where each element contains the primary language for the corresponding input document.
the name of the action to be performed on the input documents, see $AnalyzeActionName
the input documents to be analyzed
Optional
countryHint: stringIndicates the country of origin for all of
the input strings to assist the model in predicting the language they are
written in. If unspecified, this value will be set to the default
country hint in TextAnalysisClientOptions
. If set to an empty string,
or the string "none", the service will apply a model where the country is
explicitly unset. The same country hint is applied to all strings in the
input collection.
Optional
options: AnalyzeActionParameters<ActionName> & TextAnalysisOperationOptionsoptional action parameters and settings for the operation
Runs a predictive model to perform the action of choice on the input documents. See $AnalyzeActionName for a list of supported actions.
The layout of each item in the results array depends on the action chosen.
For example, each PIIEntityRecognition document result consists of both
entities
and redactedText
where the former is a list of all Pii entities
in the text and the latter is the original text after all such Pii entities
have been redacted from it.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const documents = [{
id: "1",
text: "The food and service aren't the best",
language: "en"
}];
const results = await client.analyze("SentimentAnalysis", documents, {
includeOpinionMining: true,
});
for (const result of results) {
if (result.error) {
// a document has an error instead of results
} else {
const { sentiment, confidenceScores, sentences } = result;
for (const { sentiment, confidenceScores, opinions } of sentences) {
for (const { target, assessments } of opinions) {
const { text, sentiment, confidenceScores } = target;
for (const { text, sentiment } of assessments) {
// Do something
}
}
}
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/sentiment-opinion-mining/overview for more information on opinion mining.
const documents = [<input documents>];
const categoriesFilter = [KnownPiiCategory.USSocialSecurityNumber];
const domainFilter = KnownPiiDomain.Phi;
const results = await client.analyze("PiiEntityRecognition", documents, {
domainFilter, categoriesFilter
});
for (const result of results) {
if (result.error) {
// a document has an error instead of results
} else {
const { entities, redactedText } = result;
for (const { text, category, confidenceScore, length, offset } of entities) {
// Do something
}
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/personally-identifiable-information/overview for more information on personally identifiable information.
an array of results corresponding to the input documents
the name of the action to be performed on the input documents, see $AnalyzeActionName
the input documents to be analyzed
Optional
options: AnalyzeActionParameters<ActionName> & TextAnalysisOperationOptionsoptional action parameters and settings for the operation
Runs a predictive model to perform the action of choice on the input strings. See $AnalyzeActionName for a list of supported actions.
The layout of each item in the results array depends on the action chosen.
For example, each PIIEntityRecognition document result consists of both
entities
and redactedText
where the former is a list of all Pii entities
in the text and the latter is the original text after all such Pii entities
have been redacted from it.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const documents = ["The food and service aren't the best"];
const results = await client.analyze("SentimentAnalysis", documents, {
includeOpinionMining: true,
});
for (const result of results) {
if (result.error) {
// a document has an error instead of results
} else {
const { sentiment, confidenceScores, sentences } = result;
for (const { sentiment, confidenceScores, opinions } of sentences) {
for (const { target, assessments } of opinions) {
const { text, sentiment, confidenceScores } = target;
for (const { text, sentiment } of assessments) {
// Do something
}
}
}
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/sentiment-opinion-mining/overview for more information on opinion mining.
const documents = [<input strings>];
const languageHint = "en";
const categoriesFilter = [KnownPiiCategory.USSocialSecurityNumber];
const domainFilter = KnownPiiDomain.Phi;
const results = await client.analyze("PiiEntityRecognition", documents, languageHint, {
domainFilter, categoriesFilter
});
for (const result of results) {
if (result.error) {
// a document has an error instead of results
} else {
const { entities, redactedText } = result;
for (const { text, category, confidenceScore, length, offset } of entities) {
// Do something
}
}
}
See https://docs.microsoft.com//azure/cognitive-services/language-service/personally-identifiable-information/overview for more information on personally identifiable information.
an array of results corresponding to the input documents
the name of the action to be performed on the input documents, see $AnalyzeActionName
the input documents to be analyzed
Optional
languageCode: stringthe code of the language that all the input strings are
written in. If unspecified, this value will be set to the default
language in TextAnalysisClientOptions
. If set to an empty string,
the service will apply a model where the language is explicitly set to
"None". Language support varies per action, for example, more information
about the languages supported for Entity Recognition actions can be
found in https://docs.microsoft.com//azure/cognitive-services/language-service/named-entity-recognition/language-support.
If set to "auto", the service will automatically infer the language from
the input text. If that process fails, the value in the defaultLanguage
option will be used.
Optional
options: AnalyzeActionParameters<ActionName> & TextAnalysisOperationOptionsoptional action parameters and settings for the operation
Performs an array (batch) of actions on the input documents. Each action has
a kind
field that specifies the nature of the action. See $AnalyzeBatchActionNames
for a list of supported actions. In addition to kind
, actions could also
have other parameters such as disableServiceLogs
and modelVersion
.
The results array contains the results for those input actions where each
item also has a kind
field that specifies the type of the results.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const poller = await client.beginAnalyzeBatch(
[{ kind: "KeyPhraseExtraction" }, { kind: "PiiEntityRecognition" }],
documents
);
const actionResults = await poller.pollUntilDone();
for await (const actionResult of actionResults) {
if (actionResult.error) {
throw new Error(`Unexpected error`);
}
switch (actionResult.kind) {
case "KeyPhraseExtraction": {
for (const doc of actionResult.results) {
// do something
}
break;
}
case "PiiEntityRecognition": {
for (const doc of actionResult.results) {
// do something
}
break;
}
}
}
an array of results corresponding to the input actions
an array of actions that will be run on the input documents
the input documents to be analyzed
Optional
languageCode: stringthe code of the language that all the input strings are
written in. If unspecified, this value will be set to the default
language in TextAnalysisClientOptions
. If set to an empty string,
the service will apply a model where the language is explicitly set to
"None". Language support varies per action, for example, more information
about the languages supported for Entity Recognition actions can be
found in https://docs.microsoft.com//azure/cognitive-services/language-service/named-entity-recognition/language-support.
If set to "auto", the service will automatically infer the language from
the input text. If that process fails, the value in the defaultLanguage
option will be used.
Optional
options: BeginAnalyzeBatchOptionsoptional settings for the operation
Performs an array (batch) of actions on the input documents. Each action has
a kind
field that specifies the nature of the action. See $AnalyzeBatchActionNames
for a list of supported actions. In addition to kind
, actions could also
have other parameters such as disableServiceLogs
and modelVersion
.
The results array contains the results for those input actions where each
item also has a kind
field that specifies the type of the results.
See https://docs.microsoft.com//azure/cognitive-services/language-service/concepts/data-limits for data limits.
const poller = await client.beginAnalyzeBatch(
[{ kind: "KeyPhraseExtraction" }, { kind: "PiiEntityRecognition" }],
documents
);
const actionResults = await poller.pollUntilDone();
for await (const actionResult of actionResults) {
if (actionResult.error) {
throw new Error(`Unexpected error`);
}
switch (actionResult.kind) {
case "KeyPhraseExtraction": {
for (const doc of actionResult.results) {
// do something
}
break;
}
case "PiiEntityRecognition": {
for (const doc of actionResult.results) {
// do something
}
break;
}
}
}
an array of results corresponding to the input actions
an array of actions that will be run on the input documents
the input documents to be analyzed
Optional
options: BeginAnalyzeBatchOptionsoptional settings for the operation
Creates a poller from the serialized state of another poller. This can be useful when you want to create pollers on a different host or a poller needs to be constructed after the original one is not in scope.
the serialized state of another poller. It is the
result of poller.toString()
Optional
options: RestoreAnalyzeBatchPollerOptionsoptional settings for the operation
client.beginAnalyzeBatch
returns a promise that will resolve to a poller.
The state of the poller can be serialized and used to create another as follows:
const serializedState = poller.toString();
const rehydratedPoller = await client.createAnalyzeBatchPoller(serializedState);
const actionResults = await rehydratedPoller.pollUntilDone();
Generated using TypeDoc
A client for interacting with the text analysis features in Azure Cognitive Language Service.
The client needs the endpoint of a Language resource and an authentication method such as an API key or AAD. The API key and endpoint can be found in the Language resource page in the Azure portal. They will be located in the resource's Keys and Endpoint page, under Resource Management.
Examples for authentication:
API Key
Azure Active Directory
See the
@azure/identity
package for more information about authenticating with Azure Active Directory.