Skip to content

Commit 4bf624b

Browse files
authored
[Text Analytics] Update Quickstart - JS
1 parent 17c2dc3 commit 4bf624b

File tree

1 file changed

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

1 file changed

+66
-51
lines changed

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

Lines changed: 66 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,6 +71,16 @@ 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+
#### [Version 3.0-preview](#tab/version-3)
75+
76+
```javascript
77+
"use strict";
78+
79+
const { TextAnalyticsClient, TextAnalyticsApiKeyCredential } = require("@azure/ai-text-analytics");
80+
```
81+
82+
#### [Version 2.1](#tab/version-2)
83+
6484
```javascript
6585
"use strict";
6686

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

102122
```javascript
103-
const client = new TextAnalyticsClient(endpoint, new CognitiveServicesCredential(key));
123+
const client = new TextAnalyticsClient(endpoint, new TextAnalyticsApiKeyCredential(key));
104124
```
105125

106126
#### [Version 2.1](#tab/version-2)
@@ -122,19 +142,19 @@ async function sentimentAnalysis(client){
122142

123143
const sentimentInput = [
124144
"I had the best day of my life. I wish you were there with me."
125-
]
126-
145+
];
127146
const sentimentResult = await client.analyzeSentiment(sentimentInput);
128-
result.forEach(document => {
147+
148+
sentimentResult.forEach(document => {
129149
console.log(`ID: ${document.id}`);
130150
console.log(`\tDocument Sentiment: ${document.sentiment}`);
131151
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)}`);
152+
console.log(`\t\tPositive: ${document.sentimentScores.positive.toFixed(2)} \tNegative: ${document.sentimentScores.negative.toFixed(2)} \tNeutral: ${document.sentimentScores.neutral.toFixed(2)}`);
133153
console.log(`\tSentences Sentiment(${document.sentences.length}):`);
134154
document.sentences.forEach(sentence => {
135155
console.log(`\t\tSentence sentiment: ${sentence.sentiment}`)
136156
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)}`);
157+
console.log(`\t\tPositive: ${sentence.sentimentScores.positive.toFixed(2)} \tNegative: ${sentence.sentimentScores.negative.toFixed(2)} \tNeutral: ${sentence.sentimentScores.neutral.toFixed(2)}`);
138158
console.log(`\t\tLength: ${sentence.length}, Offset: ${sentence.offset}`);
139159
})
140160
});
@@ -150,7 +170,7 @@ Run your code with `node index.js` in your console window.
150170
ID: 0
151171
Document Sentiment: positive
152172
Document Scores:
153-
Positive: 0.61 Negative: 0.01 Neutral: 0.39
173+
Positive: 1.00 Negative: 0.00 Neutral: 0.00
154174
Sentences Sentiment(2):
155175
Sentence sentiment: positive
156176
Sentences Scores:
@@ -185,22 +205,18 @@ Run your code with `node index.js` in your console window.
185205

186206
#### [Version 3.0-preview](#tab/version-3)
187207

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.
208+
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.
189209

190210
```javascript
191211
async function languageDetection(client) {
192212

193213
const languageInputArray = [
194214
"Ce document est rédigé en Français."
195-
]
196-
197-
const languageResult = await client.detectLanguages(languageInputArray);
215+
];
216+
const languageResult = await client.detectLanguage(languageInputArray);
198217

199-
result.forEach(document => {
218+
languageResult.forEach(document => {
200219
console.log(`ID: ${document.id}`);
201-
document.detectedLanguages.forEach(language =>
202-
console.log(`\tDetected Language ${language.name}`)
203-
);
204220
console.log(`\tPrimary Language ${document.primaryLanguage.name}`)
205221
});
206222
}
@@ -213,7 +229,6 @@ Run your code with `node index.js` in your console window.
213229

214230
```console
215231
ID: 0
216-
Detected Language French
217232
Primary Language French
218233
```
219234

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

259273
entityResults.forEach(document => {
260274
console.log(`Document ID: ${document.id}`);
261275
document.entities.forEach(entity => {
262-
console.log(`\tName: ${entity.text} \tType: ${entity.type} \tSub Type: ${entity.subtype != "" ? entity.subtype : "N/A"}`);
276+
console.log(`\tName: ${entity.text} \tCategory: ${entity.category} \tSubcategory: ${entity.subCategory ? entity.subCategory : "N/A"}`);
263277
console.log(`\tOffset: ${entity.offset}, Length: ${entity.length} \tScore: ${entity.score}`);
264278
});
265279
});
@@ -273,23 +287,26 @@ Run your code with `node index.js` in your console window.
273287

274288
```console
275289
Document ID: 0
276-
Name: Microsoft Type: Organization Sub Type: N/A
290+
Name: Microsoft Category: Organization Subcategory: N/A
277291
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
292+
Name: Bill Gates Category: Person Subcategory: N/A
293+
Offset: 25, Length: 10 Score: 0.67
294+
Name: Paul Allen Category: Person Subcategory: N/A
295+
Offset: 40, Length: 10 Score: 0.81
296+
Name: April 4, 1975 Category: DateTime Subcategory: Date
283297
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
298+
Name: interpreters Category: PersonType Subcategory: N/A
299+
Offset: 95, Length: 12 Score: 0.6
300+
Name: 8800 Category: Quantity Subcategory: Number
287301
Offset: 123, Length: 4 Score: 0.8
288302
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
303+
Name: Microsoft Category: Organization Subcategory: N/A
304+
Offset: 21, Length: 9 Score: 0.96
305+
Name: Redmond Category: Location Subcategory: GPE
306+
Offset: 60, Length: 7 Score: 0.09
307+
Name: 21 Category: Quantity Subcategory: Number
292308
Offset: 71, Length: 2 Score: 0.8
309+
Name: Seattle Category: Location Subcategory: GPE
293310
```
294311

295312
## Using NER to detect personal information
@@ -302,13 +319,13 @@ async function entityPiiRecognition(client){
302319

303320
const entityPiiInput = [
304321
"Insurance policy for SSN on file 123-12-1234 is here by approved."
305-
]
306-
const entityResults = await client.recognizePiiEntities(entityPiiInput);
322+
];
323+
const entityPiiResults = await client.recognizePiiEntities(entityPiiInput);
307324

308-
result.forEach(document => {
325+
entityPiiResults.forEach(document => {
309326
console.log(`Document ID: ${document.id}`);
310327
document.entities.forEach(entity => {
311-
console.log(`\tName: ${entity.text} \tType: ${entity.type} \tSub Type: ${entity.subtype != "" ? entity.subtype : "N/A"}`);
328+
console.log(`\tName: ${entity.text} \tCategory: ${entity.category} \tSubcategory: ${entity.subCategory ? entity.subCategory : "N/A"}`);
312329
console.log(`\tOffset: ${entity.offset}, Length: ${entity.length} \tScore: ${entity.score}`);
313330
});
314331
});
@@ -322,7 +339,7 @@ Run your code with `node index.js` in your console window.
322339

323340
```console
324341
Document ID: 0
325-
Name: 123-12-1234 Type: U.S. Social Security Number (SSN) Sub Type: N/A
342+
Name: 123-12-1234 Category: U.S. Social Security Number (SSN) Subcategory: N/A
326343
Offset: 33, Length: 11 Score: 0.85
327344
```
328345

@@ -335,7 +352,7 @@ async function linkedEntityRecognition(client){
335352

336353
const linkedEntityInput = [
337354
"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-
]
355+
];
339356
const entityResults = await client.recognizeLinkedEntities(linkedEntityInput);
340357

341358
entityResults.forEach(document => {
@@ -346,7 +363,7 @@ async function linkedEntityRecognition(client){
346363
entity.matches.forEach(match => {
347364
console.log(`\t\tText: ${match.text}`);
348365
console.log(`\t\tOffset: ${match.offset}, Length: ${match.length} \tScore: ${match.score.toFixed(3)}`);
349-
})
366+
});
350367
});
351368
});
352369
}
@@ -362,31 +379,31 @@ Document ID: 0
362379
Name: Altair 8800 ID: Altair 8800 URL: https://en.wikipedia.org/wiki/Altair_8800 Data Source: Wikipedia
363380
Matches:
364381
Text: Altair 8800
365-
Offset: 116, Length: 11 Score: 0.650
382+
Offset: 116, Length: 11 Score: 0.777
366383
Name: Bill Gates ID: Bill Gates URL: https://en.wikipedia.org/wiki/Bill_Gates Data Source: Wikipedia
367384
Matches:
368385
Text: Bill Gates
369-
Offset: 25, Length: 10 Score: 0.243
386+
Offset: 25, Length: 10 Score: 0.555
370387
Text: Gates
371-
Offset: 161, Length: 5 Score: 0.243
388+
Offset: 161, Length: 5 Score: 0.555
372389
Name: Paul Allen ID: Paul Allen URL: https://en.wikipedia.org/wiki/Paul_Allen Data Source: Wikipedia
373390
Matches:
374391
Text: Paul Allen
375-
Offset: 40, Length: 10 Score: 0.174
392+
Offset: 40, Length: 10 Score: 0.533
376393
Name: Microsoft ID: Microsoft URL: https://en.wikipedia.org/wiki/Microsoft Data Source: Wikipedia
377394
Matches:
378395
Text: Microsoft
379-
Offset: 0, Length: 9 Score: 0.196
396+
Offset: 0, Length: 9 Score: 0.469
380397
Text: Microsoft
381-
Offset: 150, Length: 9 Score: 0.196
398+
Offset: 150, Length: 9 Score: 0.469
382399
Name: April 4 ID: April 4 URL: https://en.wikipedia.org/wiki/April_4 Data Source: Wikipedia
383400
Matches:
384401
Text: April 4
385-
Offset: 54, Length: 7 Score: 0.137
402+
Offset: 54, Length: 7 Score: 0.248
386403
Name: BASIC ID: BASIC URL: https://en.wikipedia.org/wiki/BASIC Data Source: Wikipedia
387404
Matches:
388405
Text: BASIC
389-
Offset: 89, Length: 5 Score: 0.052
406+
Offset: 89, Length: 5 Score: 0.281
390407
```
391408

392409
#### [Version 2.1](#tab/version-2)
@@ -443,12 +460,10 @@ async function keyPhraseExtraction(client){
443460

444461
const keyPhrasesInput = [
445462
"My cat might need to see a veterinarian.",
446-
]
447-
448-
const result = await client.extractKeyPhrases(keyPhrasesInput)
449-
450-
451-
result.forEach(document => {
463+
];
464+
const keyPhraseResult = await client.extractKeyPhrases(keyPhrasesInput);
465+
466+
keyPhraseResult.forEach(document => {
452467
console.log(`ID: ${document.id}`);
453468
console.log(`\tDocument Key Phrases: ${document.keyPhrases}`);
454469
});

0 commit comments

Comments
 (0)