@@ -51,6 +51,16 @@ npm init
51
51
```
52
52
### Install the client library
53
53
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
+
54
64
Install the ` @azure/cognitiveservices-textanalytics ` NPM packages:
55
65
56
66
``` console
@@ -61,6 +71,16 @@ Your app's `package.json` file will be updated with the dependencies.
61
71
62
72
Create a file named ` index.js ` and add the following libraries:
63
73
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
+
64
84
``` javascript
65
85
" use strict" ;
66
86
@@ -100,7 +120,7 @@ The response object is a list containing the analysis information for each docum
100
120
Create a new ` TextAnalyticsClient ` object with your key and endpoint as parameters.
101
121
102
122
``` javascript
103
- const client = new TextAnalyticsClient (endpoint, new CognitiveServicesCredential (key));
123
+ const client = new TextAnalyticsClient (endpoint, new TextAnalyticsApiKeyCredential (key));
104
124
```
105
125
106
126
#### [ Version 2.1] ( #tab/version-2 )
@@ -122,19 +142,19 @@ async function sentimentAnalysis(client){
122
142
123
143
const sentimentInput = [
124
144
" I had the best day of my life. I wish you were there with me."
125
- ]
126
-
145
+ ];
127
146
const sentimentResult = await client .analyzeSentiment (sentimentInput);
128
- result .forEach (document => {
147
+
148
+ sentimentResult .forEach (document => {
129
149
console .log (` ID: ${ document .id } ` );
130
150
console .log (` \t Document Sentiment: ${ document .sentiment } ` );
131
151
console .log (` \t Document Scores:` );
132
- console .log (` \t\t Positive: ${ document .documentScores .positive .toFixed (2 )} \t Negative: ${ document .documentScores .negative .toFixed (2 )} \t Neutral: ${ document .documentScores .neutral .toFixed (2 )} ` );
152
+ console .log (` \t\t Positive: ${ document .sentimentScores .positive .toFixed (2 )} \t Negative: ${ document .sentimentScores .negative .toFixed (2 )} \t Neutral: ${ document .sentimentScores .neutral .toFixed (2 )} ` );
133
153
console .log (` \t Sentences Sentiment(${ document .sentences .length } ):` );
134
154
document .sentences .forEach (sentence => {
135
155
console .log (` \t\t Sentence sentiment: ${ sentence .sentiment } ` )
136
156
console .log (` \t\t Sentences Scores:` );
137
- console .log (` \t\t Positive: ${ sentence .sentenceScores .positive .toFixed (2 )} \t Negative: ${ sentence .sentenceScores .negative .toFixed (2 )} \t Neutral: ${ sentence .sentenceScores .neutral .toFixed (2 )} ` );
157
+ console .log (` \t\t Positive: ${ sentence .sentimentScores .positive .toFixed (2 )} \t Negative: ${ sentence .sentimentScores .negative .toFixed (2 )} \t Neutral: ${ sentence .sentimentScores .neutral .toFixed (2 )} ` );
138
158
console .log (` \t\t Length: ${ sentence .length } , Offset: ${ sentence .offset } ` );
139
159
})
140
160
});
@@ -150,7 +170,7 @@ Run your code with `node index.js` in your console window.
150
170
ID: 0
151
171
Document Sentiment: positive
152
172
Document Scores:
153
- Positive: 0.61 Negative: 0.01 Neutral: 0.39
173
+ Positive: 1.00 Negative: 0.00 Neutral: 0.00
154
174
Sentences Sentiment(2):
155
175
Sentence sentiment: positive
156
176
Sentences Scores:
@@ -185,22 +205,18 @@ Run your code with `node index.js` in your console window.
185
205
186
206
#### [ Version 3.0-preview] ( #tab/version-3 )
187
207
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.
189
209
190
210
``` javascript
191
211
async function languageDetection (client ) {
192
212
193
213
const languageInputArray = [
194
214
" 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);
198
217
199
- result .forEach (document => {
218
+ languageResult .forEach (document => {
200
219
console .log (` ID: ${ document .id } ` );
201
- document .detectedLanguages .forEach (language =>
202
- console .log (` \t Detected Language ${ language .name } ` )
203
- );
204
220
console .log (` \t Primary Language ${ document .primaryLanguage .name } ` )
205
221
});
206
222
}
@@ -213,7 +229,6 @@ Run your code with `node index.js` in your console window.
213
229
214
230
``` console
215
231
ID: 0
216
- Detected Language French
217
232
Primary Language French
218
233
```
219
234
@@ -253,13 +268,12 @@ async function entityRecognition(client){
253
268
" Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800" ,
254
269
" La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle."
255
270
];
256
-
257
271
const entityResults = await client .recognizeEntities (entityInputs);
258
272
259
273
entityResults .forEach (document => {
260
274
console .log (` Document ID: ${ document .id } ` );
261
275
document .entities .forEach (entity => {
262
- console .log (` \t Name: ${ entity .text } \t Type : ${ entity .type } \t Sub Type : ${ entity .subtype != " " ? entity .subtype : " N/A" } ` );
276
+ console .log (` \t Name: ${ entity .text } \t Category : ${ entity .category } \t Subcategory : ${ entity .subCategory ? entity .subCategory : " N/A" } ` );
263
277
console .log (` \t Offset: ${ entity .offset } , Length: ${ entity .length } \t Score: ${ entity .score } ` );
264
278
});
265
279
});
@@ -273,23 +287,26 @@ Run your code with `node index.js` in your console window.
273
287
274
288
``` console
275
289
Document ID: 0
276
- Name: Microsoft Type : Organization Sub Type : N/A
290
+ Name: Microsoft Category : Organization Subcategory : N/A
277
291
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
283
297
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
287
301
Offset: 123, Length: 4 Score: 0.8
288
302
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
292
308
Offset: 71, Length: 2 Score: 0.8
309
+ Name: Seattle Category: Location Subcategory: GPE
293
310
```
294
311
295
312
## Using NER to detect personal information
@@ -302,13 +319,13 @@ async function entityPiiRecognition(client){
302
319
303
320
const entityPiiInput = [
304
321
" 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);
307
324
308
- result .forEach (document => {
325
+ entityPiiResults .forEach (document => {
309
326
console .log (` Document ID: ${ document .id } ` );
310
327
document .entities .forEach (entity => {
311
- console .log (` \t Name: ${ entity .text } \t Type : ${ entity .type } \t Sub Type : ${ entity .subtype != " " ? entity .subtype : " N/A" } ` );
328
+ console .log (` \t Name: ${ entity .text } \t Category : ${ entity .category } \t Subcategory : ${ entity .subCategory ? entity .subCategory : " N/A" } ` );
312
329
console .log (` \t Offset: ${ entity .offset } , Length: ${ entity .length } \t Score: ${ entity .score } ` );
313
330
});
314
331
});
@@ -322,7 +339,7 @@ Run your code with `node index.js` in your console window.
322
339
323
340
``` console
324
341
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
326
343
Offset: 33, Length: 11 Score: 0.85
327
344
```
328
345
@@ -335,7 +352,7 @@ async function linkedEntityRecognition(client){
335
352
336
353
const linkedEntityInput = [
337
354
" 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
+ ];
339
356
const entityResults = await client .recognizeLinkedEntities (linkedEntityInput);
340
357
341
358
entityResults .forEach (document => {
@@ -346,7 +363,7 @@ async function linkedEntityRecognition(client){
346
363
entity .matches .forEach (match => {
347
364
console .log (` \t\t Text: ${ match .text } ` );
348
365
console .log (` \t\t Offset: ${ match .offset } , Length: ${ match .length } \t Score: ${ match .score .toFixed (3 )} ` );
349
- })
366
+ });
350
367
});
351
368
});
352
369
}
@@ -362,31 +379,31 @@ Document ID: 0
362
379
Name: Altair 8800 ID: Altair 8800 URL: https://en.wikipedia.org/wiki/Altair_8800 Data Source: Wikipedia
363
380
Matches:
364
381
Text: Altair 8800
365
- Offset: 116, Length: 11 Score: 0.650
382
+ Offset: 116, Length: 11 Score: 0.777
366
383
Name: Bill Gates ID: Bill Gates URL: https://en.wikipedia.org/wiki/Bill_Gates Data Source: Wikipedia
367
384
Matches:
368
385
Text: Bill Gates
369
- Offset: 25, Length: 10 Score: 0.243
386
+ Offset: 25, Length: 10 Score: 0.555
370
387
Text: Gates
371
- Offset: 161, Length: 5 Score: 0.243
388
+ Offset: 161, Length: 5 Score: 0.555
372
389
Name: Paul Allen ID: Paul Allen URL: https://en.wikipedia.org/wiki/Paul_Allen Data Source: Wikipedia
373
390
Matches:
374
391
Text: Paul Allen
375
- Offset: 40, Length: 10 Score: 0.174
392
+ Offset: 40, Length: 10 Score: 0.533
376
393
Name: Microsoft ID: Microsoft URL: https://en.wikipedia.org/wiki/Microsoft Data Source: Wikipedia
377
394
Matches:
378
395
Text: Microsoft
379
- Offset: 0, Length: 9 Score: 0.196
396
+ Offset: 0, Length: 9 Score: 0.469
380
397
Text: Microsoft
381
- Offset: 150, Length: 9 Score: 0.196
398
+ Offset: 150, Length: 9 Score: 0.469
382
399
Name: April 4 ID: April 4 URL: https://en.wikipedia.org/wiki/April_4 Data Source: Wikipedia
383
400
Matches:
384
401
Text: April 4
385
- Offset: 54, Length: 7 Score: 0.137
402
+ Offset: 54, Length: 7 Score: 0.248
386
403
Name: BASIC ID: BASIC URL: https://en.wikipedia.org/wiki/BASIC Data Source: Wikipedia
387
404
Matches:
388
405
Text: BASIC
389
- Offset: 89, Length: 5 Score: 0.052
406
+ Offset: 89, Length: 5 Score: 0.281
390
407
```
391
408
392
409
#### [ Version 2.1] ( #tab/version-2 )
@@ -443,12 +460,10 @@ async function keyPhraseExtraction(client){
443
460
444
461
const keyPhrasesInput = [
445
462
" 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 => {
452
467
console .log (` ID: ${ document .id } ` );
453
468
console .log (` \t Document Key Phrases: ${ document .keyPhrases } ` );
454
469
});
0 commit comments