-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Language Text] Update links (#22875)
- Loading branch information
1 parent
900347d
commit 5002e8c
Showing
44 changed files
with
3,239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- javascript | ||
products: | ||
- azure | ||
- azure-cognitive-services | ||
- language-service | ||
urlFragment: ai-language-text-javascript-beta | ||
--- | ||
|
||
# Azure Cognitive Language Service client library samples for JavaScript (Beta) | ||
|
||
These sample programs show how to use the JavaScript client libraries for Azure Cognitive Language Service in some common scenarios. | ||
|
||
| **File Name** | **Description** | | ||
| --------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | | ||
| [entityLinking.js][entitylinking] | detects entities that have links to more information on the web | | ||
| [entityRecognition.js][entityrecognition] | detects entities in a piece of text | | ||
| [keyPhraseExtraction.js][keyphraseextraction] | extracts key phrases from a piece of text | | ||
| [languageDetection.js][languagedetection] | detects the language of a piece of text | | ||
| [piiEntityRecognition.js][piientityrecognition] | detects personally-identifiable information | | ||
| [sentimentAnalysis.js][sentimentanalysis] | analyzes the sentiment of a piece of text | | ||
| [stats.js][stats] | access statistics about documents and transactions | | ||
| [customEntityRecognition.js][customentityrecognition] | detects custom text in a piece of text | | ||
| [customMultiLabelClassification.js][custommultilabelclassification] | multi-label classification of pieces of text | | ||
| [customSingleLabelClassification.js][customsinglelabelclassification] | single-label classification of pieces of text | | ||
| [extractiveSummarization.js][extractivesummarization] | extracts a summary from an article | | ||
| [healthcare.js][healthcare] | detects healthcare entities in a piece of text and creates an FHIR representation | | ||
| [opinionMining.js][opinionmining] | analyzes the sentiment of a piece of text and mine opinions about different targets | | ||
| [authenticationMethods.js][authenticationmethods] | authenticates a service client using both Azure Active Directory and an API key | | ||
| [batching.js][batching] | applies multiple Text Analytics actions per document | | ||
| [modelVersion.js][modelversion] | shows how to choose model versions for pre-built models. | | ||
| [paging.js][paging] | controls paging for the results of `beginAnalyzeActions` | | ||
| [rehydratePolling.js][rehydratepolling] | creates a poller using the serialized state of another | | ||
|
||
## Prerequisites | ||
|
||
The sample programs are compatible with [LTS versions of Node.js](https://nodejs.org/about/releases/). | ||
|
||
You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: | ||
|
||
- [Azure Cognitive Services instance][createinstance_azurecognitiveservicesinstance] | ||
|
||
Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. | ||
|
||
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. | ||
|
||
## Setup | ||
|
||
To run the samples using the published version of the package: | ||
|
||
1. Install the dependencies using `npm`: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically. | ||
|
||
3. Run whichever samples you like (note that some samples may require additional setup, see the table above): | ||
|
||
```bash | ||
node entityLinking.js | ||
``` | ||
|
||
Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): | ||
|
||
```bash | ||
npx cross-env ENDPOINT="<endpoint>" LANGUAGE_API_KEY="<language api key>" node entityLinking.js | ||
``` | ||
|
||
## Next Steps | ||
|
||
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. | ||
|
||
[entitylinking]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/entityLinking.js | ||
[entityrecognition]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/entityRecognition.js | ||
[keyphraseextraction]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/keyPhraseExtraction.js | ||
[languagedetection]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/languageDetection.js | ||
[piientityrecognition]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/piiEntityRecognition.js | ||
[sentimentanalysis]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/sentimentAnalysis.js | ||
[stats]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/stats.js | ||
[customentityrecognition]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/customEntityRecognition.js | ||
[custommultilabelclassification]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/customMultiLabelClassification.js | ||
[customsinglelabelclassification]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/customSingleLabelClassification.js | ||
[extractivesummarization]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/extractiveSummarization.js | ||
[healthcare]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/healthcare.js | ||
[opinionmining]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/opinionMining.js | ||
[authenticationmethods]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/authenticationMethods.js | ||
[batching]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/batching.js | ||
[modelversion]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/modelVersion.js | ||
[paging]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/paging.js | ||
[rehydratepolling]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/rehydratePolling.js | ||
[apiref]: https://docs.microsoft.com/javascript/api/@azure/ai-language-text | ||
[freesub]: https://azure.microsoft.com/free/ | ||
[createinstance_azurecognitiveservicesinstance]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account | ||
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cognitivelanguage/ai-language-text/README.md |
71 changes: 71 additions & 0 deletions
71
sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/authenticationMethods.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* This sample demonstrates how to use either an Azure Active Directory (RBAC) | ||
* or an API Key to authenticate a TextAnalysisClient. | ||
* | ||
* @summary authenticates a service client using both Azure Active Directory | ||
* and an API key | ||
*/ | ||
|
||
// To use an API Key, import `AzureKeyCredential` from the Text Analytics package | ||
const { TextAnalysisClient, AzureKeyCredential } = require("@azure/ai-language-text"); | ||
|
||
// To use Azure AD, import `DefaultAzureCredential` from the `@azure/identity` package | ||
const { DefaultAzureCredential } = require("@azure/identity"); | ||
|
||
// Load the .env file if it exists | ||
require("dotenv").config(); | ||
|
||
// You will need to set this environment variables or edit the following values | ||
const endpoint = process.env["ENDPOINT"] || "<cognitive language service endpoint>"; | ||
|
||
async function useAad() { | ||
console.log("-- Azure Active Directory --"); | ||
|
||
// DefaultAzureCredential expects the following three environment variables: | ||
// - AZURE_TENANT_ID: The tenant ID in Azure Active Directory | ||
// - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant | ||
// - AZURE_CLIENT_SECRET: The client secret for the registered application | ||
const credential = new DefaultAzureCredential(); | ||
|
||
const client = new TextAnalysisClient(endpoint, credential); | ||
|
||
const [result] = await client.analyze("LanguageDetection", ["hello world"]); | ||
|
||
if (!result.error) { | ||
console.log(`Primary language detected as ${result.primaryLanguage.name}`); | ||
} | ||
} | ||
|
||
async function useApiKey() { | ||
console.log("-- API Key --"); | ||
|
||
// If using an API Key, you will need to set this environment variable | ||
const apiKey = process.env["LANGUAGE_API_KEY"] || "<api key>"; | ||
|
||
const credential = new AzureKeyCredential(apiKey); | ||
|
||
const client = new TextAnalysisClient(endpoint, credential); | ||
|
||
const [result] = await client.analyze("LanguageDetection", ["hello world"]); | ||
|
||
if (!result.error) { | ||
console.log(`Primary language detected as ${result.primaryLanguage.name}`); | ||
} | ||
} | ||
|
||
async function main() { | ||
console.log("== Client Authentication Methods Sample =="); | ||
|
||
await useAad(); | ||
|
||
await useApiKey(); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); | ||
|
||
module.exports = { main }; |
121 changes: 121 additions & 0 deletions
121
sdk/cognitivelanguage/ai-language-text/samples/v1-beta/javascript/batching.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* This sample extracts key phrases, entities, and pii entities from several documents | ||
* using a long-running operation. This functionality uses the generic analysis | ||
* endpoint, which provides a way to group several different Text Analytics actions | ||
* into a single request. | ||
* | ||
* @summary applies multiple Text Analytics actions per document | ||
*/ | ||
|
||
const { AzureKeyCredential, TextAnalysisClient } = require("@azure/ai-language-text"); | ||
|
||
// Load the .env file if it exists | ||
require("dotenv").config(); | ||
|
||
// You will need to set these environment variables or edit the following values | ||
const endpoint = process.env["ENDPOINT"] || "<cognitive language service endpoint>"; | ||
const apiKey = process.env["LANGUAGE_API_KEY"] || "<api key>"; | ||
|
||
const documents = [ | ||
"Microsoft was founded by Bill Gates and Paul Allen.", | ||
"Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle.", | ||
"I need to take my cat to the veterinarian.", | ||
"The employee's SSN is 555-55-5555.", | ||
"We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore the spot! They provide marvelous food and they have a great menu. The chief cook happens to be the owner (I think his name is John Doe) and he is super nice, coming out of the kitchen and greeted us all. We enjoyed very much dining in the place! The Sirloin steak I ordered was tender and juicy, and the place was impeccably clean. You can even pre-order from their online menu at www.contososteakhouse.com, call 312-555-0176 or send email to [email protected]! The only complaint I have is the food didn't come fast enough. Overall I highly recommend it!", | ||
]; | ||
|
||
async function main() { | ||
console.log("== Batch Sample =="); | ||
|
||
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(apiKey)); | ||
const actions = [ | ||
{ | ||
kind: "EntityRecognition", | ||
modelVersion: "latest", | ||
}, | ||
{ | ||
kind: "PiiEntityRecognition", | ||
modelVersion: "latest", | ||
}, | ||
{ | ||
kind: "KeyPhraseExtraction", | ||
modelVersion: "latest", | ||
}, | ||
]; | ||
const poller = await client.beginAnalyzeBatch(actions, documents, "en"); | ||
|
||
poller.onProgress(() => { | ||
console.log( | ||
`Number of actions still in progress: ${poller.getOperationState().actionInProgressCount}` | ||
); | ||
}); | ||
|
||
console.log(`The operation was created on ${poller.getOperationState().createdOn}`); | ||
|
||
console.log(`The operation results will expire on ${poller.getOperationState().expiresOn}`); | ||
|
||
const actionResults = await poller.pollUntilDone(); | ||
|
||
for await (const actionResult of actionResults) { | ||
if (actionResult.error) { | ||
const { code, message } = actionResult.error; | ||
throw new Error(`Unexpected error (${code}): ${message}`); | ||
} | ||
switch (actionResult.kind) { | ||
case "KeyPhraseExtraction": { | ||
for (const doc of actionResult.results) { | ||
console.log(`- Document ${doc.id}`); | ||
if (!doc.error) { | ||
console.log("\tKey phrases:"); | ||
for (const phrase of doc.keyPhrases) { | ||
console.log(`\t- ${phrase}`); | ||
} | ||
} else { | ||
console.error("\tError:", doc.error); | ||
} | ||
} | ||
break; | ||
} | ||
case "EntityRecognition": { | ||
for (const doc of actionResult.results) { | ||
console.log(`- Document ${doc.id}`); | ||
if (!doc.error) { | ||
console.log("\tEntities:"); | ||
for (const entity of doc.entities) { | ||
console.log(`\t- Entity ${entity.text} of type ${entity.category}`); | ||
} | ||
} else { | ||
console.error("\tError:", doc.error); | ||
} | ||
} | ||
break; | ||
} | ||
case "PiiEntityRecognition": { | ||
for (const doc of actionResult.results) { | ||
console.log(`- Document ${doc.id}`); | ||
if (!doc.error) { | ||
console.log("\tPii Entities:"); | ||
for (const entity of doc.entities) { | ||
console.log(`\t- Entity ${entity.text} of type ${entity.category}`); | ||
} | ||
} else { | ||
console.error("\tError:", doc.error); | ||
} | ||
} | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unexpected action results: ${actionResult.kind}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); | ||
|
||
module.exports = { main }; |
Oops, something went wrong.