-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Text Analytics] Add samples for Custom Text #17822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
deyaaeldeen
merged 5 commits into
Azure:textanalytics/v3.2-preview.2
from
deyaaeldeen:textanalytics/v3.2-preview.2-sample
Oct 11, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb58ab1
[Text Analytics] Add samples for Custom Text
deyaaeldeen a8d23ef
edit summary
deyaaeldeen 175da08
Update sdk/textanalytics/ai-text-analytics/samples-dev/customText.ts
deyaaeldeen eb0aed8
revert samples
deyaaeldeen bfec432
unpublish
deyaaeldeen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
sdk/textanalytics/ai-text-analytics/samples-dev/customText.ts
This file contains hidden or 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,135 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| /** | ||
| * This sample applies actions for customer-trained models to 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 Custom Text Analytics actions per document | ||
| * @azsdk-weight 40 | ||
| */ | ||
|
|
||
| import { | ||
| TextAnalyticsClient, | ||
| AzureKeyCredential, | ||
| TextAnalyticsActions | ||
| } from "@azure/ai-text-analytics"; | ||
|
|
||
| // Load the .env file if it exists | ||
| import * as dotenv from "dotenv"; | ||
| dotenv.config(); | ||
|
|
||
| // You will need to set these environment variables or edit the following values | ||
| const endpoint = process.env["ENDPOINT"] || "<cognitive services endpoint>"; | ||
| const apiKey = process.env["TEXT_ANALYTICS_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 order@contososteakhouse.com! The only complaint I have is the food didn't come fast enough. Overall I highly recommend it!" | ||
| ]; | ||
|
|
||
| export async function main() { | ||
| console.log("== Analyze Sample =="); | ||
|
|
||
| const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(apiKey)); | ||
|
|
||
| const actions: TextAnalyticsActions = { | ||
| recognizeCustomEntitiesActions: [ | ||
| { | ||
| projectName: process.env["RECOGNIZE_CUSTOM_ENTITIES_PROJECT_NAME"]!, | ||
| deploymentName: process.env["RECOGNIZE_CUSTOM_ENTITIES_DEPLOYMENT_NAME"]! | ||
| } | ||
| ], | ||
| classifyDocumentSingleCategoryActions: [ | ||
| { | ||
| projectName: process.env["CLASSIFY_DOCUMENT_SINGLE_CATEGORY_PROJECT_NAME"]!, | ||
| deploymentName: process.env["CLASSIFY_DOCUMENT_SINGLE_CATEGORY_DEPLOYMENT_NAME"]! | ||
| } | ||
| ], | ||
| classifyDocumentMultiCategoriesActions: [ | ||
| { | ||
| projectName: process.env["CLASSIFY_DOCUMENT_MULTIPLE_CATEGORY_PROJECT_NAME"]!, | ||
| deploymentName: process.env["CLASSIFY_DOCUMENT_MULTIPLE_CATEGORY_DEPLOYMENT_NAME"]! | ||
| } | ||
| ] | ||
| }; | ||
| const poller = await client.beginAnalyzeActions(documents, actions, "en", { | ||
| includeStatistics: true | ||
| }); | ||
|
|
||
| poller.onProgress(() => { | ||
| console.log( | ||
| `Number of actions still in progress: ${poller.getOperationState().actionsInProgressCount}` | ||
| ); | ||
| }); | ||
|
|
||
| console.log(`The analyze actions operation created on ${poller.getOperationState().createdOn}`); | ||
|
|
||
| console.log( | ||
| `The analyze actions operation results will expire on ${poller.getOperationState().expiresOn}` | ||
| ); | ||
|
|
||
| const resultPages = await poller.pollUntilDone(); | ||
|
|
||
| for await (const page of resultPages) { | ||
| const customEntitiesAction = page.recognizeCustomEntitiesResults[0]; | ||
| if (!customEntitiesAction.error) { | ||
| for (const doc of customEntitiesAction.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); | ||
| } | ||
| } | ||
| console.log("Action statistics: "); | ||
| console.log(JSON.stringify(customEntitiesAction.results.statistics, null, 2)); | ||
| } | ||
|
|
||
| const singleCatClassificationAction = page.classifyDocumentSingleCategoryResults[0]; | ||
| if (!singleCatClassificationAction.error) { | ||
| for (const doc of singleCatClassificationAction.results) { | ||
| console.log(`- Document ${doc.id}`); | ||
| if (!doc.error) { | ||
| console.log( | ||
| `\t- Detected Category ${doc.classification.category} with confidence score ${doc.classification.confidenceScore}` | ||
| ); | ||
| } else { | ||
| console.error("\tError:", doc.error); | ||
| } | ||
| } | ||
| console.log("Action statistics: "); | ||
| console.log(JSON.stringify(singleCatClassificationAction.results.statistics)); | ||
| } | ||
|
|
||
| const multipleCatClassificationAction = page.classifyDocumentMultiCategoriesResults[0]; | ||
| if (!multipleCatClassificationAction.error) { | ||
| for (const doc of multipleCatClassificationAction.results) { | ||
| console.log(`- Document ${doc.id}`); | ||
| if (!doc.error) { | ||
| console.log("\tCategories:"); | ||
| for (const category of doc.classifications) { | ||
| console.log( | ||
| `\t- Category ${category.category} with confidence score ${category.confidenceScore}` | ||
| ); | ||
| } | ||
| } else { | ||
| console.error("\tError:", doc.error); | ||
| } | ||
| } | ||
| console.log("Action statistics: "); | ||
| console.log(JSON.stringify(multipleCatClassificationAction.results.statistics)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error("The sample encountered an error:", err); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In FR samples we usually show adding an initial
onProgresshandler through the options bag. Based on the rest of the sample, you must poll the operation once before the onProgress handler can be registered, so the handler won't run on the first state change this way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The begin operation already gives you a poller that has already been polled once. See here.