Skip to content

Commit 4e68bb7

Browse files
authored
Merge pull request #103862 from suhas92/patch-27
[Text Analytics] Update Quickstart - JS
2 parents bb7d1be + 7ab00a5 commit 4e68bb7

File tree

1 file changed

+72
-51
lines changed
  • articles/cognitive-services/text-analytics/includes/quickstarts

1 file changed

+72
-51
lines changed

articles/cognitive-services/text-analytics/includes/quickstarts/nodejs-sdk.md

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ npm init
5151
```
5252
### Install the client library
5353

54+
#### [Version 3.0-preview](#tab/version-3)
55+
56+
Install the `@azure/ai-text-analytics` NPM packages:
57+
58+
```console
59+
npm install --save @azure/ai-text-analytics
60+
```
61+
62+
#### [Version 2.1](#tab/version-2)
63+
5464
Install the `@azure/cognitiveservices-textanalytics` NPM packages:
5565

5666
```console
@@ -61,11 +71,27 @@ Your app's `package.json` file will be updated with the dependencies.
6171

6272
Create a file named `index.js` and add the following libraries:
6373

74+
---
75+
76+
Your app's `package.json` file will be updated with the dependencies.
77+
Create a file named `index.js` and add the following libraries:
78+
79+
#### [Version 3.0-preview](#tab/version-3)
80+
81+
```javascript
82+
"use strict";
83+
84+
const { TextAnalyticsClient, TextAnalyticsApiKeyCredential } = require("@azure/ai-text-analytics");
85+
```
86+
87+
#### [Version 2.1](#tab/version-2)
88+
6489
```javascript
6590
"use strict";
6691

6792
const { TextAnalyticsClient, CognitiveServicesCredential } = require("@azure/cognitiveservices-textanalytics");
6893
```
94+
---
6995

7096
Create variables for your resource's Azure endpoint and key.
7197

@@ -100,7 +126,7 @@ The response object is a list containing the analysis information for each docum
100126
Create a new `TextAnalyticsClient` object with your key and endpoint as parameters.
101127

102128
```javascript
103-
const client = new TextAnalyticsClient(endpoint, new CognitiveServicesCredential(key));
129+
const textAnalyticsClient = new TextAnalyticsClient(endpoint, new TextAnalyticsApiKeyCredential(key));
104130
```
105131

106132
#### [Version 2.1](#tab/version-2)
@@ -122,19 +148,19 @@ async function sentimentAnalysis(client){
122148

123149
const sentimentInput = [
124150
"I had the best day of my life. I wish you were there with me."
125-
]
126-
151+
];
127152
const sentimentResult = await client.analyzeSentiment(sentimentInput);
128-
result.forEach(document => {
153+
154+
sentimentResult.forEach(document => {
129155
console.log(`ID: ${document.id}`);
130156
console.log(`\tDocument Sentiment: ${document.sentiment}`);
131157
console.log(`\tDocument Scores:`);
132-
console.log(`\t\tPositive: ${document.documentScores.positive.toFixed(2)} \tNegative: ${document.documentScores.negative.toFixed(2)} \tNeutral: ${document.documentScores.neutral.toFixed(2)}`);
158+
console.log(`\t\tPositive: ${document.sentimentScores.positive.toFixed(2)} \tNegative: ${document.sentimentScores.negative.toFixed(2)} \tNeutral: ${document.sentimentScores.neutral.toFixed(2)}`);
133159
console.log(`\tSentences Sentiment(${document.sentences.length}):`);
134160
document.sentences.forEach(sentence => {
135161
console.log(`\t\tSentence sentiment: ${sentence.sentiment}`)
136162
console.log(`\t\tSentences Scores:`);
137-
console.log(`\t\tPositive: ${sentence.sentenceScores.positive.toFixed(2)} \tNegative: ${sentence.sentenceScores.negative.toFixed(2)} \tNeutral: ${sentence.sentenceScores.neutral.toFixed(2)}`);
163+
console.log(`\t\tPositive: ${sentence.sentimentScores.positive.toFixed(2)} \tNegative: ${sentence.sentimentScores.negative.toFixed(2)} \tNeutral: ${sentence.sentimentScores.neutral.toFixed(2)}`);
138164
console.log(`\t\tLength: ${sentence.length}, Offset: ${sentence.offset}`);
139165
})
140166
});
@@ -150,7 +176,7 @@ Run your code with `node index.js` in your console window.
150176
ID: 0
151177
Document Sentiment: positive
152178
Document Scores:
153-
Positive: 0.61 Negative: 0.01 Neutral: 0.39
179+
Positive: 1.00 Negative: 0.00 Neutral: 0.00
154180
Sentences Sentiment(2):
155181
Sentence sentiment: positive
156182
Sentences Scores:
@@ -185,22 +211,18 @@ Run your code with `node index.js` in your console window.
185211

186212
#### [Version 3.0-preview](#tab/version-3)
187213

188-
Create an array of strings containing the document you want to analyze. Call the client's `detectLanguages()` method and get the returned `DetectLanguageResult`. Then iterate through the results, and print each document's ID, with respective primary and detected language.
214+
Create an array of strings containing the document you want to analyze. Call the client's `detectLanguage()` method and get the returned `DetectLanguageResultCollection`. Then iterate through the results, and print each document's ID with respective primary language.
189215

190216
```javascript
191217
async function languageDetection(client) {
192218

193219
const languageInputArray = [
194220
"Ce document est rédigé en Français."
195-
]
196-
197-
const languageResult = await client.detectLanguages(languageInputArray);
221+
];
222+
const languageResult = await client.detectLanguage(languageInputArray);
198223

199-
result.forEach(document => {
224+
languageResult.forEach(document => {
200225
console.log(`ID: ${document.id}`);
201-
document.detectedLanguages.forEach(language =>
202-
console.log(`\tDetected Language ${language.name}`)
203-
);
204226
console.log(`\tPrimary Language ${document.primaryLanguage.name}`)
205227
});
206228
}
@@ -213,7 +235,6 @@ Run your code with `node index.js` in your console window.
213235

214236
```console
215237
ID: 0
216-
Detected Language French
217238
Primary Language French
218239
```
219240

@@ -253,13 +274,12 @@ async function entityRecognition(client){
253274
"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800",
254275
"La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle."
255276
];
256-
257277
const entityResults = await client.recognizeEntities(entityInputs);
258278

259279
entityResults.forEach(document => {
260280
console.log(`Document ID: ${document.id}`);
261281
document.entities.forEach(entity => {
262-
console.log(`\tName: ${entity.text} \tType: ${entity.type} \tSub Type: ${entity.subtype != "" ? entity.subtype : "N/A"}`);
282+
console.log(`\tName: ${entity.text} \tCategory: ${entity.category} \tSubcategory: ${entity.subCategory ? entity.subCategory : "N/A"}`);
263283
console.log(`\tOffset: ${entity.offset}, Length: ${entity.length} \tScore: ${entity.score}`);
264284
});
265285
});
@@ -273,23 +293,26 @@ Run your code with `node index.js` in your console window.
273293

274294
```console
275295
Document ID: 0
276-
Name: Microsoft Type: Organization Sub Type: N/A
296+
Name: Microsoft Category: Organization Subcategory: N/A
277297
Offset: 0, Length: 9 Score: 1
278-
Name: Bill Gates Type: Person Sub Type: N/A
279-
Offset: 25, Length: 10 Score: 0.999786376953125
280-
Name: Paul Allen Type: Person Sub Type: N/A
281-
Offset: 40, Length: 10 Score: 0.9988105297088623
282-
Name: April 4, 1975 Type: DateTime Sub Type: Date
298+
Name: Bill Gates Category: Person Subcategory: N/A
299+
Offset: 25, Length: 10 Score: 0.67
300+
Name: Paul Allen Category: Person Subcategory: N/A
301+
Offset: 40, Length: 10 Score: 0.81
302+
Name: April 4, 1975 Category: DateTime Subcategory: Date
283303
Offset: 54, Length: 13 Score: 0.8
284-
Name: Altair Type: Organization Sub Type: N/A
285-
Offset: 116, Length: 6 Score: 0.7996330857276917
286-
Name: 8800 Type: Quantity Sub Type: Number
304+
Name: interpreters Category: PersonType Subcategory: N/A
305+
Offset: 95, Length: 12 Score: 0.6
306+
Name: 8800 Category: Quantity Subcategory: Number
287307
Offset: 123, Length: 4 Score: 0.8
288308
Document ID: 1
289-
Name: Microsoft Type: Organization Sub Type: N/A
290-
Offset: 21, Length: 9 Score: 0.9837456345558167
291-
Name: 21 Type: Quantity Sub Type: Number
309+
Name: Microsoft Category: Organization Subcategory: N/A
310+
Offset: 21, Length: 9 Score: 0.96
311+
Name: Redmond Category: Location Subcategory: GPE
312+
Offset: 60, Length: 7 Score: 0.09
313+
Name: 21 Category: Quantity Subcategory: Number
292314
Offset: 71, Length: 2 Score: 0.8
315+
Name: Seattle Category: Location Subcategory: GPE
293316
```
294317

295318
## Using NER to detect personal information
@@ -302,13 +325,13 @@ async function entityPiiRecognition(client){
302325

303326
const entityPiiInput = [
304327
"Insurance policy for SSN on file 123-12-1234 is here by approved."
305-
]
306-
const entityResults = await client.recognizePiiEntities(entityPiiInput);
328+
];
329+
const entityPiiResults = await client.recognizePiiEntities(entityPiiInput);
307330

308-
result.forEach(document => {
331+
entityPiiResults.forEach(document => {
309332
console.log(`Document ID: ${document.id}`);
310333
document.entities.forEach(entity => {
311-
console.log(`\tName: ${entity.text} \tType: ${entity.type} \tSub Type: ${entity.subtype != "" ? entity.subtype : "N/A"}`);
334+
console.log(`\tName: ${entity.text} \tCategory: ${entity.category} \tSubcategory: ${entity.subCategory ? entity.subCategory : "N/A"}`);
312335
console.log(`\tOffset: ${entity.offset}, Length: ${entity.length} \tScore: ${entity.score}`);
313336
});
314337
});
@@ -322,7 +345,7 @@ Run your code with `node index.js` in your console window.
322345

323346
```console
324347
Document ID: 0
325-
Name: 123-12-1234 Type: U.S. Social Security Number (SSN) Sub Type: N/A
348+
Name: 123-12-1234 Category: U.S. Social Security Number (SSN) Subcategory: N/A
326349
Offset: 33, Length: 11 Score: 0.85
327350
```
328351

@@ -335,7 +358,7 @@ async function linkedEntityRecognition(client){
335358

336359
const linkedEntityInput = [
337360
"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800. During his career at Microsoft, Gates held the positions of chairman, chief executive officer, president and chief software architect, while also being the largest individual shareholder until May 2014."
338-
]
361+
];
339362
const entityResults = await client.recognizeLinkedEntities(linkedEntityInput);
340363

341364
entityResults.forEach(document => {
@@ -346,7 +369,7 @@ async function linkedEntityRecognition(client){
346369
entity.matches.forEach(match => {
347370
console.log(`\t\tText: ${match.text}`);
348371
console.log(`\t\tOffset: ${match.offset}, Length: ${match.length} \tScore: ${match.score.toFixed(3)}`);
349-
})
372+
});
350373
});
351374
});
352375
}
@@ -362,31 +385,31 @@ Document ID: 0
362385
Name: Altair 8800 ID: Altair 8800 URL: https://en.wikipedia.org/wiki/Altair_8800 Data Source: Wikipedia
363386
Matches:
364387
Text: Altair 8800
365-
Offset: 116, Length: 11 Score: 0.650
388+
Offset: 116, Length: 11 Score: 0.777
366389
Name: Bill Gates ID: Bill Gates URL: https://en.wikipedia.org/wiki/Bill_Gates Data Source: Wikipedia
367390
Matches:
368391
Text: Bill Gates
369-
Offset: 25, Length: 10 Score: 0.243
392+
Offset: 25, Length: 10 Score: 0.555
370393
Text: Gates
371-
Offset: 161, Length: 5 Score: 0.243
394+
Offset: 161, Length: 5 Score: 0.555
372395
Name: Paul Allen ID: Paul Allen URL: https://en.wikipedia.org/wiki/Paul_Allen Data Source: Wikipedia
373396
Matches:
374397
Text: Paul Allen
375-
Offset: 40, Length: 10 Score: 0.174
398+
Offset: 40, Length: 10 Score: 0.533
376399
Name: Microsoft ID: Microsoft URL: https://en.wikipedia.org/wiki/Microsoft Data Source: Wikipedia
377400
Matches:
378401
Text: Microsoft
379-
Offset: 0, Length: 9 Score: 0.196
402+
Offset: 0, Length: 9 Score: 0.469
380403
Text: Microsoft
381-
Offset: 150, Length: 9 Score: 0.196
404+
Offset: 150, Length: 9 Score: 0.469
382405
Name: April 4 ID: April 4 URL: https://en.wikipedia.org/wiki/April_4 Data Source: Wikipedia
383406
Matches:
384407
Text: April 4
385-
Offset: 54, Length: 7 Score: 0.137
408+
Offset: 54, Length: 7 Score: 0.248
386409
Name: BASIC ID: BASIC URL: https://en.wikipedia.org/wiki/BASIC Data Source: Wikipedia
387410
Matches:
388411
Text: BASIC
389-
Offset: 89, Length: 5 Score: 0.052
412+
Offset: 89, Length: 5 Score: 0.281
390413
```
391414

392415
#### [Version 2.1](#tab/version-2)
@@ -443,12 +466,10 @@ async function keyPhraseExtraction(client){
443466

444467
const keyPhrasesInput = [
445468
"My cat might need to see a veterinarian.",
446-
]
447-
448-
const result = await client.extractKeyPhrases(keyPhrasesInput)
449-
450-
451-
result.forEach(document => {
469+
];
470+
const keyPhraseResult = await client.extractKeyPhrases(keyPhrasesInput);
471+
472+
keyPhraseResult.forEach(document => {
452473
console.log(`ID: ${document.id}`);
453474
console.log(`\tDocument Key Phrases: ${document.keyPhrases}`);
454475
});

0 commit comments

Comments
 (0)