-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpaging.js
145 lines (130 loc) · 5.11 KB
/
paging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* This sample shows how to control paging for the results of `beginAnalyzeActions`.
* Mainly, shows how to specify the maximum number of processed documents in
* each page and how to store references to specific pages of results.
*
* @summary controls paging for the results of `beginAnalyzeActions`
*/
const { TextAnalysisClient, AzureKeyCredential } = 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("== Paging Sample ==");
const client = new TextAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
let nextContinuationToken = undefined;
let continuationToken = undefined;
let stopOverwrite = false;
const poller = await client.beginAnalyzeBatch(
[
{
kind: "EntityRecognition",
modelVersion: "latest",
},
],
documents,
"en",
{
onResponse: (_rawResponse, response) => {
const nextLink = response.nextLink;
if (!stopOverwrite && nextLink) {
continuationToken = nextContinuationToken;
nextContinuationToken = nextLink;
}
},
}
);
poller.onProgress(() => {
console.log(
`Number of actions still in progress: ${poller.getOperationState().actionInProgressCount}`
);
});
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 actionResults = await poller.pollUntilDone();
for await (const page of actionResults.byPage({
maxPageSize: 1,
})) {
console.log("Starting a page --------------------------------------------");
for (const actionResult of page) {
if (actionResult.error) {
const { code, message } = actionResult.error;
throw new Error(`Unexpected error (${code}): ${message}`);
}
switch (actionResult.kind) {
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) {
if (entity.text === "veterinarian") {
stopOverwrite = true;
}
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}`);
}
}
}
}
console.log("Starting looping from page#3");
/**
* Start looping from a specific page using a specific continuationToken
*/
for await (const page of actionResults.byPage({
maxPageSize: 1,
continuationToken,
})) {
console.log("Starting a page --------------------------------------------");
for (const actionResult of page) {
if (actionResult.error) {
const { code, message } = actionResult.error;
throw new Error(`Unexpected error (${code}): ${message}`);
}
switch (actionResult.kind) {
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;
}
default: {
throw new Error(`Unexpected action results: ${actionResult.kind}`);
}
}
}
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
module.exports = { main };